I have been working on a code to help automate one of the processes in the lab I work at. To summarize the entire purpose of the code, I am creating a way to take experiment data, create a file of data, and send it to the website host that retains all of this information. I'm having some trouble figuring out how to create a dictionary that will fill in sample ID's from a certain, start and end date. Here is the section of the code I'm having issues with:
def get_vcf(run_name, start_date, end_date, auth_token):
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': auth_token,
}
params = {
'format': 'json',
'name': run_name,
'start_date': start_date,
'end_date': end_date,
}
response = requests.get('https://ionreporter.thermofisher.com/api/v1/getvcf', params=params, headers=headers,
verify=False)
# todo this will return everything in the date range, we will want a dictionary for each of the the sampleIDs
# todo paired with the VCFs it goes with that we will send to XXX, or some csv like list of the samples-
vcfs = response.json()
# todo dictionary construction
vcf_dict = {0: vcfs[0]}
return vcfs, vcf_dict
A sample record from the result of the web query looks like this:
[
{
"data_links": "ionreporter.thermofisher.com/api/v1/download? filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample /Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip",
"name": "Sample_c150_2016-04-29-14-16-534",
"id": "ff808181545d90790154613336be0008"
}
]
At the bottom, I began creating the dictionary. I understand that this would only return the first response from vcfs. I'd like for it to fill with whatever the amount of sample information we have for the date range, since it will vary. Thanks in advance!
CodePudding user response:
The code you supply suggests that you want a dictionary with numeric keys that start from 0
. If so, here's how to get such a dictionary, where the values are taken directly from the result of the web query:
r = {i: v for i, v in enumerate(vcfs)}
pprint(r)
A result of three items would look like this:
{0: {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0008',
'name': 'Sample_c150_2016-04-29-14-16-534'},
1: {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0009',
'name': 'Sample_c150_2016-04-29-14-16-534'},
2: {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0010',
'name': 'Sample_c150_2016-04-29-14-16-534'}}
This is not very useful because you can already look up values by the same index in the original list of records. Say instead, you wanted the id
field of each record to be the key in the dictionary. Then you'd do this:
r = {entry['id']: entry for entry in vcfs}
pprint(r)
which would give a result like this:
{'ff808181545d90790154613336be0008': {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0008',
'name': 'Sample_c150_2016-04-29-14-16-534'},
'ff808181545d90790154613336be0009': {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0009',
'name': 'Sample_c150_2016-04-29-14-16-534'},
'ff808181545d90790154613336be0010': {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0010',
'name': 'Sample_c150_2016-04-29-14-16-534'}}
To get this data, I took the data record you provided, made two additional copies of it, and slightly changed the id
of each of the copies to provide unique ids.