Fetch Attribute Data in Bulk¶
Fetch large amounts of data from multiple records with bulk_fetch() for optimum performance.
Connect to MI¶
Specify a database and table.
[1]:
from GRANTA_MIScriptingToolkit import granta as mpy
mi = mpy.connect('http://localhost/mi_servicelayer', autologon=True)
db = mi.get_db(db_key='MI_Training')
db.set_unit_system('Metric', absolute_temperatures=True)
tab = db.get_table('MaterialUniverse')
Define a list of records to fetch¶
For example, by Record GUID.
[2]:
guids = ['00001192-000e-4fff-8fff-dd92ffff0000',
'00001194-000e-4fff-8fff-dd92ffff0000',
'0000119b-000e-4fff-8fff-dd92ffff0000',
'00000e38-000e-4fff-8fff-dd92ffff0000',
'00000e41-000e-4fff-8fff-dd92ffff0000',
'000009cb-000e-4fff-8fff-dd92ffff0000']
Get each record using its Record GUID, and combine the returned Record objects into a list.
[3]:
records = [tab.get_record_by_id(vguid=guid) for guid in guids]
records
[3]:
[<Record long name:Titanium, alpha-beta alloy, Ti-6Al-4V, aged>,
<Record long name:Titanium, alpha-beta alloy, Ti-6Al-4V, annealed, generic>,
<Record long name:Titanium, alpha-beta alloy, Ti-6Al-4V, solution treated & aged>,
<Record long name:Aluminum, 7075, wrought, T6>,
<Record long name:Aluminum, 7075, wrought, T73>,
<Record long name:Low alloy steel, AISI 4130, air melted, normalized>]
Fetch attribute data values¶
Fetch the Base, Density and Young’s modulus attribute values for each record, and print the results. Default units can be overridden before export using the table.set_display_unit() method.
[4]:
tab.set_display_unit("Young's modulus", "ksi")
tab.bulk_fetch(records=records, attributes=["Base", "Density", "Young's modulus"])
[5]:
density_unit = tab.attributes['Density'].unit
youngs_mod_unit = tab.attributes["Young's modulus"].unit
print('{:50.50} | {:^21} | {:^21} | {:^18}'.format('Name',
'Density / ' + density_unit,
"Young's modulus / " + youngs_mod_unit,
'Base'))
print('-'*120)
for record in records:
output = '{:50.50} | {:^10.2f}-{:^10.2f} | {:^10.2f}-{:^10.2f} | {:^20}'.format(record.name,
record.attributes['Density'].value['low'],
record.attributes['Density'].value['high'],
record.attributes["Young's modulus"].value['low'],
record.attributes["Young's modulus"].value['high'],
str(record.attributes['Base'].value))
print(output)
Name | Density / kg/m^3 | Young's modulus / ksi | Base
------------------------------------------------------------------------------------------------------------------------
Titanium, alpha-beta alloy, Ti-6Al-4V, aged | 4420.00 - 4430.00 | 16099.19 - 17259.49 | ['Ti (Titanium)']
Titanium, alpha-beta alloy, Ti-6Al-4V, annealed, g | 4406.64 - 4450.93 | 15954.15 - 17259.49 | ['Ti (Titanium)']
Titanium, alpha-beta alloy, Ti-6Al-4V, solution tr | 4406.64 - 4450.93 | 15954.15 - 16969.41 | ['Ti (Titanium)']
Aluminum, 7075, wrought, T6 | 2770.00 - 2830.00 | 10007.60 - 11022.87 | ['Al (Aluminum)']
Aluminum, 7075, wrought, T73 | 2781.69 - 2809.65 | 10007.60 - 10520.81 | ['Al (Aluminum)']
Low alloy steel, AISI 4130, air melted, normalized | 7794.24 - 7872.58 | 29007.55 - 30495.11 | ['Fe (Iron)']