{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# New Features in MI Scripting Toolkit v1.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deleting records (DeleteOrWithdrawIfLatestRecordVersion)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Let's start by creating a new record (Note: added receiveTimeout to the MISession)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Imported record \"STK_1_5_Import_Test_2021-03-23 16:43:08\" to table \"Tensile Test Data\"\n"
     ]
    }
   ],
   "source": [
    "import GRANTA_MIScriptingToolkit as gdl\n",
    "from time import strftime\n",
    "import datetime\n",
    "\n",
    "session = gdl.GRANTA_MISession('http://localhost/mi_servicelayer/',autoLogon=True, receiveTimeout=5000)\n",
    "\n",
    "raw_record_name = 'STK_1_5_Import_Test'\n",
    "rec_name = raw_record_name + '_' + strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "table_name = 'Tensile Test Data'\n",
    "subset_name = table_name\n",
    "db_key = 'MI_Training'\n",
    "\n",
    "import_service = session.dataImportService\n",
    "browse_service = session.browseService\n",
    "export_service = session.dataExportService\n",
    "\n",
    "test_table_ref = gdl.TableReference(name=table_name, DBKey=db_key)\n",
    "partial_table_ref = gdl.PartialTableReference(tableName=table_name)\n",
    "subset_ref = gdl.SubsetReference(DBKey=db_key, partialTableReference=partial_table_ref, name=subset_name)\n",
    "table_root_ref = browse_service.GetRootNode(gdl.GetRootNode(table=test_table_ref)).rootNode.recordReference\n",
    "\n",
    "import_record = gdl.ImportRecord(recordName=rec_name, importAttributeValues=None, isFolder=False,\n",
    "                                 existingRecord=table_root_ref, subsetReferences=[subset_ref])\n",
    "\n",
    "import_request = gdl.SetRecordAttributesRequest(importRecords=[import_record])\n",
    "imported_records = import_service.SetRecordAttributes(import_request).recordsImported\n",
    "imported_record_ref = imported_records[0].recordReference\n",
    "print('Imported record \"{}\" to table \"{}\"'.format(imported_records[0].longName, table_name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now let's delete it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Record STK_1_5_Import_Test_2021-03-23 16:43:08 was deleted\n"
     ]
    }
   ],
   "source": [
    "delete_record = gdl.DeleteOrWithdrawRecord(recordReference=imported_record_ref)\n",
    "records_to_delete = [delete_record]\n",
    "\n",
    "delete_records_request = gdl.DeleteOrWithdrawIfLatestRecordVersionRequest(deleteOrWithdrawRecords=records_to_delete)\n",
    "delete_records_response = import_service.DeleteOrWithdrawIfLatestRecordVersion(delete_records_request)\n",
    "print('Record {} was deleted'.format(imported_records[0].longName))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ResolveReferences"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Let's see if we can modify a record..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes, we can edit this record.\n"
     ]
    }
   ],
   "source": [
    "modfied_record_guid = '4269f1bc-df6b-4a6d-870c-ef931728f37b'\n",
    "modified_record_ref = gdl.RecordReference(DBKey=db_key, recordGUID=modfied_record_guid)\n",
    "\n",
    "resolve_refs_request = gdl.ResolveReferencesRequest(entities=[modified_record_ref])\n",
    "\n",
    "can_modify_int = browse_service.ResolveReferences(resolve_refs_request).entityResolutions[0].canWrite\n",
    "if can_modify_int == 0:\n",
    "    print('Yes, we can edit this record.')\n",
    "else:\n",
    "    print(\"No, we don't have permission to edit this record.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## lastModifier and last modifiedDate pseudo-attributes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### If we can modify the record, let's make a small change to it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Updated record \"MTS-615723\"\n"
     ]
    }
   ],
   "source": [
    "if can_modify_int == 0:\n",
    "    attribute_project_notes = 'Project Notes'\n",
    "    attribute_val = 'Project was updated: {0}'.format(datetime.datetime.now())\n",
    "    attribute_ref = gdl.AttributeReference(name=attribute_project_notes, DBKey=db_key, partialTableReference=partial_table_ref)\n",
    "    import_attribute_val = gdl.ImportAttributeValue(longTextDataValue=gdl.LongTextDataType(attribute_val),\n",
    "                                                    attributeReference=attribute_ref) \n",
    "    update_record = gdl.ImportRecord(recordName=None,\n",
    "                                     importRecordMode='Update',\n",
    "                                     importAttributeValues=[import_attribute_val],\n",
    "                                     existingRecord=modified_record_ref)\n",
    "\n",
    "    update_request = gdl.SetRecordAttributesRequest(importRecords=[update_record])\n",
    "    updated_records = import_service.SetRecordAttributes(update_request).recordsImported\n",
    "    updated_record_ref = updated_records[0].recordReference\n",
    "    updated_record_name = updated_records[0].longName\n",
    "    print('Updated record \"{}\"'.format(updated_record_name))\n",
    "else:\n",
    "    print(\"Nothing to do - we can't modify the record!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now let's retrieve the record creator, createdDate, lastModifier and modifiedDate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"Ansys Granta 1\" imported the record on 2018-06-07\n",
      "The record was last modified on 2021-03-23 by \"DESKTOP\\User\"\n"
     ]
    }
   ],
   "source": [
    "pseudo_creator = gdl.AttributeReference(pseudoAttribute=gdl.AttributeReference.MIPseudoAttributeReference.creator,\n",
    "                                        DBKey=db_key, partialTableReference=partial_table_ref)\n",
    "pseudo_created_date = gdl.AttributeReference(pseudoAttribute=gdl.AttributeReference.MIPseudoAttributeReference.createdDate,\n",
    "                                             DBKey=db_key, partialTableReference=partial_table_ref)\n",
    "pseudo_last_modifier = gdl.AttributeReference(pseudoAttribute=gdl.AttributeReference.MIPseudoAttributeReference.lastModifier,\n",
    "                                              DBKey=db_key, partialTableReference=partial_table_ref)\n",
    "pseudo_modified_date = gdl.AttributeReference(pseudoAttribute=gdl.AttributeReference.MIPseudoAttributeReference.modifiedDate,\n",
    "                                              DBKey=db_key, partialTableReference=partial_table_ref)\n",
    "attribute_refs = [pseudo_creator, pseudo_created_date, pseudo_last_modifier, pseudo_modified_date]\n",
    "\n",
    "export_request = gdl.GetRecordAttributesByRefRequest(recordReferences=[modified_record_ref],\n",
    "                                                     attributeReferences=attribute_refs)\n",
    "\n",
    "exported_data = export_service.GetRecordAttributesByRef(export_request).recordData\n",
    "for record in exported_data:\n",
    "    creator = [a.shortTextDataType.value for a in record.attributeValues if a.attributeName == '[creator]'][0]\n",
    "    created_date = [a.dateDataType.value for a in record.attributeValues if a.attributeName == '[createdDate]'][0]\n",
    "    modifier = [a.shortTextDataType.value for a in record.attributeValues if a.attributeName == '[lastModifier]'][0]\n",
    "    modified_date = [a.dateDataType.value for a in record.attributeValues if a.attributeName == '[modifiedDate]'][0]\n",
    "    print('\"{}\" imported the record on {}'.format(creator, created_date))\n",
    "    print('The record was last modified on {} by \"{}\"'.format(modified_date, modifier))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GetUnitConversions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### For the above record, let's export the Young's Modulus in the Metric system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Young's modulus = 174.0004272460938 GPa\n"
     ]
    }
   ],
   "source": [
    "attribute_name = \"Young's Modulus (11-axis)\"\n",
    "unit_context_metric = gdl.UnitConversionContext(unitSystem='Metric')\n",
    "\n",
    "modulus_attribute_ref = gdl.AttributeReference(name=attribute_name, DBKey=db_key, partialTableReference=partial_table_ref)\n",
    "export_request = gdl.GetRecordAttributesByRefRequest(recordReferences=[modified_record_ref],\n",
    "                                                     attributeReferences=[modulus_attribute_ref])\n",
    "\n",
    "exported_data = export_service.GetRecordAttributesByRef(export_request).recordData\n",
    "modulus_value = [[a.pointDataType.points[0].value for a in r.attributeValues] for r in exported_data][0][0]\n",
    "modulus_metric_symbol = [[a.pointDataType.unitSymbol for a in r.attributeValues] for r in exported_data][0][0]\n",
    "print(\"Young's modulus = {} {}\".format(modulus_value, modulus_metric_symbol))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now let's convert its value with all available conversions in Granta MI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "</style><table id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090a\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >factor</th>        <th class=\"col_heading level0 col1\" >offset</th>        <th class=\"col_heading level0 col2\" >equation</th>        <th class=\"col_heading level0 col3\" >converted result</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row0\" class=\"row_heading level0 row0\" >10^6 psi</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow0_col0\" class=\"data row0 col0\" >0.145038</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow0_col1\" class=\"data row0 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow0_col2\" class=\"data row0 col2\" >174.0004272460938 * 0.14503772518547858 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow0_col3\" class=\"data row0 col3\" >25.236626</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row1\" class=\"row_heading level0 row1\" >ksi</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow1_col0\" class=\"data row1 col0\" >145.037725</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow1_col1\" class=\"data row1 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow1_col2\" class=\"data row1 col2\" >174.0004272460938 * 145.0377251854786 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow1_col3\" class=\"data row1 col3\" >25236.626149</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row2\" class=\"row_heading level0 row2\" >psi</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow2_col0\" class=\"data row2 col0\" >145037.725185</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow2_col1\" class=\"data row2 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow2_col2\" class=\"data row2 col2\" >174.0004272460938 * 145037.72518547857 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow2_col3\" class=\"data row2 col3\" >25236626.149075</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row3\" class=\"row_heading level0 row3\" >MGO</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow3_col0\" class=\"data row3 col0\" >125663.706106</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow3_col1\" class=\"data row3 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow3_col2\" class=\"data row3 col2\" >174.0004272460938 * 125663.70610560982 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow3_col3\" class=\"data row3 col3\" >21865538.551704</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row4\" class=\"row_heading level0 row4\" >Pa</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow4_col0\" class=\"data row4 col0\" >1000000000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow4_col1\" class=\"data row4 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow4_col2\" class=\"data row4 col2\" >174.0004272460938 * 1000000000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow4_col3\" class=\"data row4 col3\" >174000427246.093811</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row5\" class=\"row_heading level0 row5\" >MPa</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow5_col0\" class=\"data row5 col0\" >1000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow5_col1\" class=\"data row5 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow5_col2\" class=\"data row5 col2\" >174.0004272460938 * 1000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow5_col3\" class=\"data row5 col3\" >174000.427246</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row6\" class=\"row_heading level0 row6\" >J/m^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow6_col0\" class=\"data row6 col0\" >1000000000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow6_col1\" class=\"data row6 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow6_col2\" class=\"data row6 col2\" >174.0004272460938 * 1000000000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow6_col3\" class=\"data row6 col3\" >174000427246.093811</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row7\" class=\"row_heading level0 row7\" >MJ/m^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow7_col0\" class=\"data row7 col0\" >1000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow7_col1\" class=\"data row7 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow7_col2\" class=\"data row7 col2\" >174.0004272460938 * 1000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow7_col3\" class=\"data row7 col3\" >174000.427246</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row8\" class=\"row_heading level0 row8\" >erg/cm^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow8_col0\" class=\"data row8 col0\" >10000000000.000002</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow8_col1\" class=\"data row8 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow8_col2\" class=\"data row8 col2\" >174.0004272460938 * 10000000000.000002 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow8_col3\" class=\"data row8 col3\" >1740004272460.938477</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row9\" class=\"row_heading level0 row9\" >ft.lbf/in^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow9_col0\" class=\"data row9 col0\" >12086.477099</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow9_col1\" class=\"data row9 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow9_col2\" class=\"data row9 col2\" >174.0004272460938 * 12086.477098789885 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow9_col3\" class=\"data row9 col3\" >2103052.179090</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row10\" class=\"row_heading level0 row10\" >kJ/m^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow10_col0\" class=\"data row10 col0\" >1000000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow10_col1\" class=\"data row10 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow10_col2\" class=\"data row10 col2\" >174.0004272460938 * 1000000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow10_col3\" class=\"data row10 col3\" >174000427.246094</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row11\" class=\"row_heading level0 row11\" >inHg</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow11_col0\" class=\"data row11 col0\" >295299.714445</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow11_col1\" class=\"data row11 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow11_col2\" class=\"data row11 col2\" >174.0004272460938 * 295299.7144451761 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow11_col3\" class=\"data row11 col3\" >51382276.479110</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row12\" class=\"row_heading level0 row12\" >Ba</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow12_col0\" class=\"data row12 col0\" >10000000000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow12_col1\" class=\"data row12 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow12_col2\" class=\"data row12 col2\" >174.0004272460938 * 10000000000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow12_col3\" class=\"data row12 col3\" >1740004272460.937988</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row13\" class=\"row_heading level0 row13\" >hPa</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow13_col0\" class=\"data row13 col0\" >10000000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow13_col1\" class=\"data row13 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow13_col2\" class=\"data row13 col2\" >174.0004272460938 * 10000000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow13_col3\" class=\"data row13 col3\" >1740004272.460938</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row14\" class=\"row_heading level0 row14\" >mb</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow14_col0\" class=\"data row14 col0\" >10000000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow14_col1\" class=\"data row14 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow14_col2\" class=\"data row14 col2\" >174.0004272460938 * 10000000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow14_col3\" class=\"data row14 col3\" >1740004272.460938</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row15\" class=\"row_heading level0 row15\" >bar</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow15_col0\" class=\"data row15 col0\" >10000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow15_col1\" class=\"data row15 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow15_col2\" class=\"data row15 col2\" >174.0004272460938 * 10000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow15_col3\" class=\"data row15 col3\" >1740004.272461</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row16\" class=\"row_heading level0 row16\" >atm</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow16_col0\" class=\"data row16 col0\" >9869.232667</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow16_col1\" class=\"data row16 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow16_col2\" class=\"data row16 col2\" >174.0004272460938 * 9869.232667160128 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow16_col3\" class=\"data row16 col3\" >1717250.700677</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row17\" class=\"row_heading level0 row17\" >torr</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow17_col0\" class=\"data row17 col0\" >7500637.554192</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow17_col1\" class=\"data row17 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow17_col2\" class=\"data row17 col2\" >174.0004272460938 * 7500637.554192106 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow17_col3\" class=\"data row17 col3\" >1305114139.047523</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row18\" class=\"row_heading level0 row18\" >lbf/ft^2</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow18_col0\" class=\"data row18 col0\" >20885432.426709</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow18_col1\" class=\"data row18 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow18_col2\" class=\"data row18 col2\" >174.0004272460938 * 20885432.42670891 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow18_col3\" class=\"data row18 col3\" >3634074165.466772</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row19\" class=\"row_heading level0 row19\" >HV</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow19_col0\" class=\"data row19 col0\" >101.971621</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow19_col1\" class=\"data row19 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow19_col2\" class=\"data row19 col2\" >174.0004272460938 * 101.97162129779282 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow19_col3\" class=\"data row19 col3\" >17743.105673</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row20\" class=\"row_heading level0 row20\" >kgf/mm^2</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow20_col0\" class=\"data row20 col0\" >101.971621</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow20_col1\" class=\"data row20 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow20_col2\" class=\"data row20 col2\" >174.0004272460938 * 101.97162129779282 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow20_col3\" class=\"data row20 col3\" >17743.105673</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row21\" class=\"row_heading level0 row21\" >dyn/cm^2</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow21_col0\" class=\"data row21 col0\" >10000000000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow21_col1\" class=\"data row21 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow21_col2\" class=\"data row21 col2\" >174.0004272460938 * 10000000000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow21_col3\" class=\"data row21 col3\" >1740004272460.937988</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row22\" class=\"row_heading level0 row22\" >ft.lbf/ft^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow22_col0\" class=\"data row22 col0\" >20885432.426709</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow22_col1\" class=\"data row22 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow22_col2\" class=\"data row22 col2\" >174.0004272460938 * 20885432.42670891 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow22_col3\" class=\"data row22 col3\" >3634074165.466772</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row23\" class=\"row_heading level0 row23\" >in.lbf/in^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow23_col0\" class=\"data row23 col0\" >145037.725185</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow23_col1\" class=\"data row23 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow23_col2\" class=\"data row23 col2\" >174.0004272460938 * 145037.72518547857 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow23_col3\" class=\"data row23 col3\" >25236626.149075</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row24\" class=\"row_heading level0 row24\" >J/cm^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow24_col0\" class=\"data row24 col0\" >1000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow24_col1\" class=\"data row24 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow24_col2\" class=\"data row24 col2\" >174.0004272460938 * 1000.0000000000001 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow24_col3\" class=\"data row24 col3\" >174000.427246</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row25\" class=\"row_heading level0 row25\" >kN/cm^2</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow25_col0\" class=\"data row25 col0\" >100.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow25_col1\" class=\"data row25 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow25_col2\" class=\"data row25 col2\" >174.0004272460938 * 100.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow25_col3\" class=\"data row25 col3\" >17400.042725</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row26\" class=\"row_heading level0 row26\" >Msi</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow26_col0\" class=\"data row26 col0\" >0.145038</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow26_col1\" class=\"data row26 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow26_col2\" class=\"data row26 col2\" >174.0004272460938 * 0.14503772518547858 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow26_col3\" class=\"data row26 col3\" >25.236626</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row27\" class=\"row_heading level0 row27\" >N/mm^2</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow27_col0\" class=\"data row27 col0\" >1000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow27_col1\" class=\"data row27 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow27_col2\" class=\"data row27 col2\" >174.0004272460938 * 1000.0 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow27_col3\" class=\"data row27 col3\" >174000.427246</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090alevel0_row28\" class=\"row_heading level0 row28\" >N.mm/mm^3</th>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow28_col0\" class=\"data row28 col0\" >1000.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow28_col1\" class=\"data row28 col1\" >0.000000</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow28_col2\" class=\"data row28 col2\" >174.0004272460938 * 1000.0000000000001 + 0.0</td>\n",
       "                        <td id=\"T_da9b869c_8bf6_11eb_80eb_4889e77d090arow28_col3\" class=\"data row28 col3\" >174000.427246</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x1d6ff468400>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas\n",
    "\n",
    "unit_conversions_request = gdl.GetUnitConversionsRequest(DBKey=db_key, unitSymbols=[modulus_metric_symbol])\n",
    "\n",
    "source_units = browse_service.GetUnitConversions(unit_conversions_request).sourceUnits\n",
    "conversion_targets = [c.conversions for c in source_units]\n",
    "factors_and_offsets = [[(c.factor, c.offset) for c in c_target] for c_target in conversion_targets][0]\n",
    "symbols = [[c.targetSymbol for c in c_target] for c_target in conversion_targets][0]\n",
    "results = [modulus_value * factor + offset for factor, offset in factors_and_offsets]\n",
    "equations = ['{} * {} + {}'.format(modulus_value, factor, offset) for factor, offset in factors_and_offsets]\n",
    "\n",
    "factors, offsets = zip(*factors_and_offsets)\n",
    "df = pandas.DataFrame([factors, offsets, equations, results])\n",
    "df_flipped = df.transpose()\n",
    "df_flipped.columns = ['factor', 'offset', 'equation', 'converted result']\n",
    "df_flipped.index = symbols\n",
    "df_flipped.style"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
