Append the list of dictnary - python

I am writing a Python program with restart capabilities. I wanted to store the state of the program execution in a JSON format, so that during re-start it could query the Json and restart from the failed point.
JSON will be something like this:
{
"job_name": xxxxx,
"job_start_time": xxxxx,
"site": xxxxxx,
"tasks": [
{
"id": <unique id to look-up on restart>
"task_start_time":
"task_end_time":
"runtime":
"successful": <true or false>
"error_message":<if successful is false>
}
]
}
When a stage completes successfully, it appends a task dict to the list of tasks.
My question is how to append the task dictionary, while the entire python object remains.
Is it possible in JSON?

My question is how to append the task dictionary, while the entire python object remains.
You can use the update method of dictionaries to modify the object. Here is an example:
d = {'inventory': [{'Color': 'Brown', 'Model': 'Camry', 'Year': 2018},
{'Model': 'Corolla', 'Year': 2017}],
'name': 'Toyota'}
d['inventory'][0].update({'Doors': 4})
print(d)
{'inventory': [{'Color': 'Brown', 'Doors': 4, 'Model': 'Camry', 'Year': 2018},
{'Model': 'Corolla', 'Year': 2017}],
'name': 'Toyota'}

You can pass parameters in input json to the next job which is going to be the output from the current job. For ex:-
{
"job_name": xxxxx_2,
"job_start_time": xxxxx,
"site": xxxxxx,
"tasks": [
{
"id": <unique id to look-up on restart>
"task_start_time":
"task_end_time":
"runtime":
"successful": <true or false>
"error_message":<if successful is false>,
"parameters":{
"--Dict":{dict_values}
}
}
]
}

Is it a dictionary? If so...
dictionary["tasks"].append(whatever)

Related

MongoDB watch() aggregation match by field value

When I use the watch() function on my collection, I am passing a aggregation to filter what comes through. I was able to get operationType to work correctly, but I also only want to include documents in which the city field is equal to Vancouver. The current syntax I am using does not work:
change_stream = client.mydb.mycollection.watch([
{
'$match': {
'operationType': { '$in': ['replace', 'insert'] },
'fullDocument': {'city': {'$eq': 'Vancouver'} }
}
}
])
And for reference, this is the what the dictionary that I'm aggregating looks like:
{'_id': {'_data': '825F...E0004'},
'clusterTime': Timestamp(1595565179, 2),
'documentKey': {'_id': ObjectId('70fc7871...')},
'fullDocument': {'_id': ObjectId('70fc7871...'),
'city': 'Vancouver',
'ns': {'coll': 'notification', 'db': 'pipeline'},
'operationType': 'replace'}
I found I just have to use a dot to access the nested dictionary:
change_stream = client.mydb.mycollection.watch([
{
'$match': {
'operationType': { '$in': ['replace', 'insert'] },
'fullDocument.city': 'Vancouver' }
}
}
])

How to create a Premium_LRS SSD disk resource (my problem is probably not python specific)

I'm trying to create a Premium SSD disk to attach to a VM in azure but can't seem to figure out how to specify that correctly - I keep ending up with a Standard HDD.
azure_client.compute_client.disks.create_or_update("my_resource_group", 'deleteme-' + str(disk_num), {
"location": "westus",
"disk_size_gb": 256,
'creation_data': {
'create_option': 'empty',
'sku': {
'name': 'Premium_LRS' # <=== What I want
}
},
'tags': {
"fake": "tags"
}
}).result().as_dict()
{
'id': '/subscriptions/5efe2633-26ac-4638-9f1f-6e24e494d9b4/resourceGroups/my_resource_group/providers/Microsoft.Compute/disks/deleteme-26',
'provisioning_state': 'Succeeded',
'name': 'deleteme-26',
'type': 'Microsoft.Compute/disks',
'time_created': '2019-02-05T00:37:41.907815Z',
'tags': {
'fake': 'tags'
},
'creation_data': {
'create_option': 'Empty'
},
'sku': {
'tier': 'Standard',
'name': 'Standard_LRS' # <== What I actually get
},
'location': 'westus',
'disk_size_gb': 256
}
I'm open to connecting the disk directly to the host at creation, but can't figure out the API for tagging the disk that way.
I've also tried also specifying 'tier': 'Premium' in the sku description - but no change. Here's the documentation I've found:
A bit embarrassing, but maybe someone else will do this in the future... I put the SKU in the wrong sub-dictionary. Azure doesn't yell at you if you put random stuff it doesn't understand in the creation_data section.
azure_client.compute_client.disks.create_or_update("my_resource_group", 'deleteme-' + str(disk_num), {
"location": "westus",
"disk_size_gb": 256,
'creation_data': {
'create_option': 'empty'
},
'sku': {
'name': 'Premium_LRS' # <=== Moved out of creation_data dict
}
'tags': {
"fake": "tags"
}
}).result().as_dict()

Add value for key in dictionary which is inside the list in python

I have to prepare JSON files to send using POST, but I have faced the following format to handle:
offer {
"location":
{
"city": "Kharkov",
"address": "street"
}
"dates": [
{
"start_date": "2018-10-10 14:00",
"end_date": "2018-11-11 14:00"
}]
}
Before it, to set the Value for the city field I used the following implementation:
offer['location']['city'] = "Kharkov"
But now I can't figure out how to add the value to the key start_date since the dictionary is inside the list.
'dates' is a list. Use index to access the key inside.
Ex:
offer['dates'][0]["start_date"] = "NewDate"
print(offer)
Output:
{'dates': [{'start_date': 'NewDate', 'end_date': '2018-11-11 14:00'}], 'location': {'city': 'Kharkov', 'address': 'street'}}

A Python dictionary with repeated fields

I'm constructing a dictionary with Python to use with a SOAP API.
My SOAP API takes an input like this:
<dataArray>
<AccountingYearData>
<Handle>
<Year>string</Year>
</Handle>
<Year>string</Year>
<FromDate>dateTime</FromDate>
<ToDate>dateTime</ToDate>
<IsClosed>boolean</IsClosed>
</AccountingYearData>
<AccountingYearData>
<Handle>
<Year>string</Year>
</Handle>
<Year>string</Year>
<FromDate>dateTime</FromDate>
<ToDate>dateTime</ToDate>
<IsClosed>boolean</IsClosed>
</AccountingYearData>
</dataArray>
Se this for the full string
https://api.e-conomic.com/secure/api1/EconomicWebService.asmx?op=AccountingYear_CreateFromDataArray
Notice how the field appears multiple times.
How can I create a Python dict with this data?
If I do this:
data = {
'dataArray':{
'AccountingYearData':{
'Handle':{'Year':'2017'},
'Year':'2017',
'FromDate':'2017-01-01',
'ToDate':'2017-12-31',
'IsClosed':'False'
},
'AccountingYearData':{
'Handle':{'Year':'2017'},
'Year':'2017',
'FromDate':'2017-01-01',
'ToDate':'2017-12-31',
'IsClosed':'False'
}
}
}
I get:
>>> type (data)
<type 'dict'>
>>> data {
'dataArray': {
'AccountingYearData': {
'IsClosed': 'False',
'FromDate': '2017-01-01',
'Handle': {'Year': '2017'},
'ToDate': '2017-12-31',
'Year': '2017'
}
}
}
It's as expected I think, but now what I need.
Well, the answer seems obvious and is even hinted by the "dataArray" name: if you have a list of items, then you want to use a list to store them:
data = {
'dataArray':[
{
'AccountingYearData':{
'Handle':{'Year':'2017'},
'Year':'2017',
'FromDate':'2017-01-01',
'ToDate':'2017-12-31',
'IsClosed':'False'
},
},
{
'AccountingYearData':{
'Handle':{'Year':'2017'},
'Year':'2017',
'FromDate':'2017-01-01',
'ToDate':'2017-12-31',
'IsClosed':'False'
},
},
]
}

Accessing a json object nested in a json array with Python 3.x

Given the json payload below, how do I get the value of 'hotspot' using Python 3.x? The top level seems to be a a dict with one key value pair. 'Recs' is the key and the value is a Python list. I have loaded the json payload into the Python class using json.loads(payload).
json payload:
{
'Recs': [{
'eSrc': 'big-a1',
'reqPs': {
'srcIP': '11.111.11.111'
},
'a1': {
'a1Ver': '1.0',
'obj': {
'eTag': '38f028e',
'sz': 1217,
'seq': '02391D2',
'hotspot': 'web/acme/srv/dev/8dd'
},
'confId': 'acme-contains',
'pipe': {
'name': 'acme.dev',
'oId': {
'pId': 'BDAD'
}
}
}
}]
}
{ indicates a dict, [ indicates a list so hotspot is at:
my_json['Recs'][0]['a1']['obj']['hotspot']

Categories

Resources