remove repeated values in dictionary - python

I want to remove the repeated value in a dictionary after I extracted the needed data which is 'rate' and 'genre'
a=[{'movie': 'abc', 'rate': '9', 'origin': 'AU', 'genre': 'horror'},
{'movie': 'xyz', 'rate': '7', 'origin': 'NY', 'genre': 'romance'},
{'movie': 'jkl', 'rate': '9', 'origin': 'HK', 'genre': 'horror'},
{'movie': 'qwe', 'rate': '6', 'origin': 'HK', 'genre': 'comedy'},
{'movie': 'vbn', 'rate': '9', 'origin': 'BKK', 'genre': 'romance'}]
needed_data=[]
for test in a:
x={}
word=['rate','genre']
for key,value in test.items():
for words in word:
if key == words:
x[key] = value
needed_data.append(x)
results = {}
filters=[]
for yy in needed_data:
for key,value in yy.items():
if value not in results.values():
results[key] = value
filters.append(results)
print(filters)
the output from above code is
[{'rate': '9', 'genre': 'romance'},
{'rate': '9', 'genre': 'romance'},
{'rate': '9', 'genre': 'romance'},
{'rate': '9', 'genre': 'romance'},
{'rate': '9', 'genre': 'romance'}]
my desired output would be
[{'rate': '9', 'genre': 'horror'},
{'rate': '7', 'genre': 'romance'},
{'rate': '6', 'genre': 'comedy'},
{'rate': '9', 'genre': 'romance'}]

I would recommend to use pandas for data processing
import pandas as pd
df = pd.DataFrame(a)
df_dd= df[["genre", "rate"]].drop_duplicates()
new_a = df_dd.to_dict(orient="records")
print(new_a)
Output
[{'genre': 'horror', 'rate': '9.'},
{'genre': 'romance', 'rate': '7'},
{'genre': 'horror', 'rate': '9'},
{'genre': 'comedy', 'rate': '6'},
{'genre': 'romance', 'rate': '9'}]

Your data has strings '9.' and '9' Do you want it that way?
z = {f"{float(x['rate']):.2f}-{x['genre']}": x for x in needed_data}
list(z.values())
Output
[{'rate': '9', 'genre': 'horror'},
{'rate': '7', 'genre': 'romance'},
{'rate': '6', 'genre': 'comedy'},
{'rate': '9', 'genre': 'romance'}]

This is the easy way to do your task:
a=[{'movie': 'abc', 'rate': '9.', 'origin': 'AU', 'genre': 'horror'},
{'movie': 'xyz', 'rate': '7', 'origin': 'NY', 'genre': 'romance'},
{'movie': 'jkl', 'rate': '9', 'origin': 'HK', 'genre': 'horror'},
{'movie': 'qwe', 'rate': '6', 'origin': 'HK', 'genre': 'comedy'},
{'movie': 'vbn', 'rate': '9', 'origin': 'BKK', 'genre': 'romance'}]
c = []
for b in a:
c.append({'rate':b['rate'],'genre':b['genre'] })
print(c)
So the Output will be:
[{'rate': '9.', 'genre': 'horror'}, {'rate': '7', 'genre': 'romance'}, {'rate': '9', 'genre': 'horror'}, {'rate': '6', 'genre': 'comedy'}, {'rate': '9', 'genre': 'romance'}]

Related

How to desalinize json coming from dynamodb stream

event = event = {'Records': [{'eventID': '2339bc590c21035b84f8cc602b12c1d2', 'eventName': 'INSERT', 'eventVersion': '1.1', 'eventSource': 'aws:dynamodb', 'awsRegion': 'us-east-1', 'dynamodb': {'ApproximateCreationDateTime': 1595908037.0, 'Keys': {'id': {'S': '9'}}, 'NewImage': {'last_name': {'S': 'Hus'}, 'id': {'S': '9'}, 'age': {'S': '95'}}, 'SequenceNumber': '3100000000035684810908', 'SizeBytes': 23, 'StreamViewType': 'NEW_IMAGE'}, 'eventSourceARN': 'arn:aws:dynamodb:us-east-1:656441365658:table/glossary/stream/2020-07-28T00:26:55.462'}, {'eventID': 'bbd4073256ef3182b3c00f13ead09501', 'eventName': 'MODIFY', 'eventVersion': '1.1', 'eventSource': 'aws:dynamodb', 'awsRegion': 'us-east-1', 'dynamodb': {'ApproximateCreationDateTime': 1595908037.0, 'Keys': {'id': {'S': '2'}}, 'NewImage': {'last_name': {'S': 'JJ'}, 'id': {'S': '2'}, 'age': {'S': '5'}}, 'SequenceNumber': '3200000000035684810954', 'SizeBytes': 21, 'StreamViewType': 'NEW_IMAGE'}, 'eventSourceARN': 'arn:aws:dynamodb:us-east-1:656441365658:table/glossary/stream/2020-07-28T00:26:55.462'}, {'eventID': 'a9c90c0c4a5a4b64d0314c4557e94e28', 'eventName': 'INSERT', 'eventVersion': '1.1', 'eventSource': 'aws:dynamodb', 'awsRegion': 'us-east-1', 'dynamodb': {'ApproximateCreationDateTime': 1595908037.0, 'Keys': {'id': {'S': '10'}}, 'NewImage': {'last_name': {'S': 'Hus'}, 'id': {'S': '10'}, 'age': {'S': '95'}}, 'SequenceNumber': '3300000000035684810956', 'SizeBytes': 25, 'StreamViewType': 'NEW_IMAGE'}, 'eventSourceARN': 'arn:aws:dynamodb:us-east-1:656441365658:table/glossary/stream/2020-07-28T00:26:55.462'}, {'eventID': '288f4a424992e5917af0350b53f754dc', 'eventName': 'MODIFY', 'eventVersion': '1.1', 'eventSource': 'aws:dynamodb', 'awsRegion': 'us-east-1', 'dynamodb': {'ApproximateCreationDateTime': 1595908037.0, 'Keys': {'id': {'S': '1'}}, 'NewImage': {'last_name': {'S': 'V'}, 'id': {'S': '1'}, 'age': {'S': '2'}}, 'SequenceNumber': '3400000000035684810957', 'SizeBytes': 20, 'StreamViewType': 'NEW_IMAGE'}, 'eventSourceARN': 'arn:aws:dynamodb:us-east-1:656441365658:table/glossary/stream/2020-07-28T00:26:55.462'}]}
The above one coming from dynamodb stream. I need to extract the some value from above
Code is below nothing is returning
def deserialize(event):
data = {}
data["M"] = event
return extract_some(data)
def extract_some(event):
for key, value in list(event.items()):
if (key == "NULL"):
return None
if (key == "S" or key == "BOOL"):
return value
for record in event['Records']:
doc = deserialise(record['dynamodb']['NewImage'])
print (doc)
Expected Out
{'last_name': 'Hus', 'id': '9', 'age': '95'}
{'last_name': 'JJ', 'id': '2', 'age': '5'}
{'last_name': 'Hus', 'id': '10', 'age': '95'}
{'last_name': 'V', 'id': '1', 'age': '2'}
try this,
from pprint import pprint
result = []
for r in event['Records']:
tmp = {}
for k, v in r['dynamodb']['NewImage'].items():
if "S" in v.keys() or "BOOL" in v.keys():
tmp[k] = v.get('S', v.get('BOOL', False))
elif 'NULL' in v:
tmp[k] = None
result.append(tmp)
pprint(result)
[{'age': '95', 'id': '9', 'last_name': 'Hus'},
{'age': '5', 'id': '2', 'last_name': 'JJ'},
{'age': '95', 'id': '10', 'last_name': 'Hus'},
{'age': '2', 'id': '1', 'last_name': 'V'}]

Finding missing value in JSON using python

I am facing this problem, I want to separate the dataset that has completed and not complete.
So, I want to put flag like 'complete' in the JSON. Example as in output.
This is the data that i have
data=[{'id': 'abc001',
'demo':{'gender':'1',
'job':'6',
'area':'3',
'study':'3'},
'ex_data':{'fam':'small',
'scholar':'2'}},
{'id': 'abc002',
'demo':{'gender':'1',
'edu':'6',
'qual':'3',
'living':'3'},
'ex_data':{'fam':'',
'scholar':''}},
{'id': 'abc003',
'demo':{'gender':'1',
'edu':'6',
'area':'3',
'sal':'3'}
'ex_data':{'fam':'big',
'scholar':NaN}}]
Output
How can I put the flag and also detect NaN and NULL in JSON?
Output=[{'id': 'abc001',
'completed':'yes',
'demo':{'gender':'1',
'job':'6',
'area':'3',
'study':'3'},
'ex_data':{'fam':'small',
'scholar':'2'}},
{'id': 'abc002',
'completed':'no',
'demo':{'gender':'1',
'edu':'6',
'qual':'3',
'living':'3'},
'ex_data':{'fam':'',
'scholar':''}},
{'id': 'abc003',
'completed':'no',
'demo':{'gender':'1',
'edu':'6',
'area':'3',
'sal':'3'}
'ex_data':{'fam':'big',
'scholar':NaN}}]
Something like this should work for you:
data = [
{
'id': 'abc001',
'demo': {
'gender': '1',
'job': '6',
'area': '3',
'study': '3'},
'ex_data': {'fam': 'small',
'scholar': '2'}
},
{
'id': 'abc002',
'demo': {
'gender': '1',
'edu': '6',
'qual': '3',
'living': '3'},
'ex_data': {'fam': '',
'scholar': ''}},
{
'id': 'abc003',
'demo': {
'gender': '1',
'edu': '6',
'area': '3',
'sal': '3'},
'ex_data': {'fam': 'big',
'scholar': None}
}
]
def browse_dict(dico):
empty_values = 0
for key in dico:
if dico[key] is None or dico[key] == "":
empty_values += 1
if isinstance(dico[key], dict):
for k in dico[key]:
if dico[key][k] is None or dico[key][k] == "":
empty_values += 1
if empty_values == 0:
dico["completed"] = "yes"
else:
dico["completed"] = "no"
for d in data:
browse_dict(d)
print(d)
Output :
{'id': 'abc001', 'demo': {'gender': '1', 'job': '6', 'area': '3', 'study': '3'}, 'ex_data': {'fam': 'small', 'scholar': '2'}, 'completed': 'yes'}
{'id': 'abc002', 'demo': {'gender': '1', 'edu': '6', 'qual': '3', 'living': '3'}, 'ex_data': {'fam': '', 'scholar': ''}, 'completed': 'no'}
{'id': 'abc003', 'demo': {'gender': '1', 'edu': '6', 'area': '3', 'sal': '3'}, 'ex_data': {'fam': 'big', 'scholar': None}, 'completed': 'no'}
Note that I changed NaN to None, because here you are most likely showing a python dictionary, not a JSON file since you are using data =
In a dictionary, the NaN value would be changed for None.
If you have to convert your JSON to a dictionary, refer to the JSON module documentation.
Also please check your dictionary syntax. You missed several commas to separate data.
You should try
The Input is
data = [{'demo': {'gender': '1', 'job': '6', 'study': '3', 'area': '3'}, 'id': 'abc001', 'ex_data': {'scholar': '2', 'fam': 'small'}}, {'demo': {'living': '3', 'gender': '1', 'qual': '3', 'edu': '6'}, 'id': 'abc002', 'ex_data': {'scholar': '', 'fam': ''}}, {'demo': {'gender': '1', 'area': '3', 'sal': '3', 'edu': '6'}, 'id': 'abc003', 'ex_data': {'scholar': None, 'fam': 'big'}}]
Also, Nan will not work in Python. So, instead of Nan we have used None.
for item in data:
item["completed"] = 'yes'
for key in item.keys():
if isinstance(item[key],dict):
for inner_key in item[key].keys():
if (not item[key][inner_key]):
item["completed"] = "no"
break
else:
if (not item[key]):
item["completed"] = "no"
break
The Output will be
data = [{'demo': {'gender': '1', 'job': '6', 'study': '3', 'area': '3'}, 'completed': 'yes', 'id': 'abc001', 'ex_data': {'scholar': '2', 'fam': 'small'}}, {'demo': {'living': '3', 'edu': '6', 'qual': '3', 'gender': '1'}, 'completed': 'no', 'id': 'abc002', 'ex_data': {'scholar': '', 'fam': ''}}, {'demo': {'edu': '6', 'gender': '1', 'sal': '3', 'area': '3'}, 'completed': 'no', 'id': 'abc003', 'ex_data': {'scholar': None, 'fam': 'big'}}]

How would I slice a value of a key in a dictionary

hello I'm new to coding and I have to Define a function named filterByMonth with two parameters. The first argument passed to the function should be a list of dictionaries (the data), the second a number (representing a month, 1 through 12). This function must return a list of all the dictionaries from the input list whose 'issued' date is in the indicated month. 'slice' a string to more easily extract the relevant portion of a date from a string. Sample function call: filterByMonth(data,9)
With a list of dictionaries I need to slice the month which is a part of the value for the key 'issued' which is the third key in the dictionaries.
data = [
{'apno': 'FEND19-9487084', 'aptype': 'FENDRIVE', 'issued': '2019-09-05T00:00:00.000', 'stname': '129 PARKSIDE CT', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14214', 'applicant': 'PETER CICERO', 'fees': '150', 'value': '3500', 'plans': '0', 'sbl': '0795300004001000', 'landuse': '411', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-03-05T00:00:00.000', 'descofwork': 'REMOVE EXISTING DRIVEWAY AND REPLACE IN KIND WITH CONCRETE TO CODE ON SOUTH / RIGHT SIDE OF STRUCTURE TO CODE - SURVEY SCANNED', 'location_1': {'latitude': '42.95116080935555', 'longitude': '-78.83406536395538', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.95116080935555', 'longitude': '-78.83406536395538', 'council_district': 'UNIVERSITY', 'police_district': 'District D', 'census': '45', 'census_block_group': '1', 'census_block': '1010', 'neighborhood': 'UNKNOWN', ':#computed_region_fk4y_hpmh': '5', ':#computed_region_eziv_p4ck': '64', ':#computed_region_tmcg_v66k': '8', ':#computed_region_kwzn_pe6v': '18', ':#computed_region_uh5x_q5mi': '88', ':#computed_region_dwzh_dtk5': '1573', ':#computed_region_b3rm_3c8a': '37', ':#computed_region_xbxg_7ifr': '24', ':#computed_region_urdz_b6n8': '7'},
{'apno': 'SWIM19-9485898', 'aptype': 'SWIM POOL', 'issued': '2019-08-19T00:00:00.000', 'stname': '341 NORWALK', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14216', 'applicant': 'MS CHRISTINE SALAMONE', 'fees': '75', 'value': '500', 'plans': '0', 'sbl': '0785000006033000', 'landuse': '210', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-02-19T00:00:00.000', 'descofwork': 'INSTALLATION OF AN ABOVE GROUND SWIMMING POOL SUBMITTED THROUGH IDT', 'location_1': {'latitude': '42.95333872723409', 'longitude': '-78.85429233887896', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.95333872723409', 'longitude': '-78.85429233887896', 'council_district': 'DELAWARE', 'police_district': 'District D', 'census': '49', 'census_block_group': '1', 'census_block': '1000', 'neighborhood': 'UNKNOWN', ':#computed_region_fk4y_hpmh': '5', ':#computed_region_eziv_p4ck': '51', ':#computed_region_tmcg_v66k': '7', ':#computed_region_kwzn_pe6v': '5', ':#computed_region_uh5x_q5mi': '190', ':#computed_region_dwzh_dtk5': '944', ':#computed_region_b3rm_3c8a': '28', ':#computed_region_xbxg_7ifr': '25', ':#computed_region_urdz_b6n8': '2'},
]
def filterByMonth(dta,month):
result=[]
for x in dta:
for y in x:
for x['issued'] in y
if month== x[6]:
result.append(x)
return result
print(filterByMonth(data,9))
You could do this more easily with the datetime module like so
from datetime import datetime
data = [
{'apno': 'FEND19-9487084', 'aptype': 'FENDRIVE', 'issued': '2019-09-05T00:00:00.000', 'stname': '129 PARKSIDE CT', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14214', 'applicant': 'PETER CICERO', 'fees': '150', 'value': '3500', 'plans': '0', 'sbl': '0795300004001000', 'landuse': '411', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-03-05T00:00:00.000', 'descofwork': 'REMOVE EXISTING DRIVEWAY AND REPLACE IN KIND WITH CONCRETE TO CODE ON SOUTH / RIGHT SIDE OF STRUCTURE TO CODE - SURVEY SCANNED', 'location_1': {'latitude': '42.95116080935555', 'longitude': '-78.83406536395538', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.95116080935555', 'longitude': '-78.83406536395538', 'council_district': 'UNIVERSITY', 'police_district': 'District D', 'census': '45', 'census_block_group': '1', 'census_block': '1010', 'neighborhood': 'UNKNOWN', ':#computed_region_fk4y_hpmh': '5', ':#computed_region_eziv_p4ck': '64', ':#computed_region_tmcg_v66k': '8', ':#computed_region_kwzn_pe6v': '18', ':#computed_region_uh5x_q5mi': '88', ':#computed_region_dwzh_dtk5': '1573', ':#computed_region_b3rm_3c8a': '37', ':#computed_region_xbxg_7ifr': '24', ':#computed_region_urdz_b6n8': '7'},
{'apno': 'SWIM19-9485898', 'aptype': 'SWIM POOL', 'issued': '2019-08-19T00:00:00.000', 'stname': '341 NORWALK', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14216', 'applicant': 'MS CHRISTINE SALAMONE', 'fees': '75', 'value': '500', 'plans': '0', 'sbl': '0785000006033000', 'landuse': '210', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-02-19T00:00:00.000', 'descofwork': 'INSTALLATION OF AN ABOVE GROUND SWIMMING POOL SUBMITTED THROUGH IDT', 'location_1': {'latitude': '42.95333872723409', 'longitude': '-78.85429233887896', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.95333872723409', 'longitude': '-78.85429233887896', 'council_district': 'DELAWARE', 'police_district': 'District D', 'census': '49', 'census_block_group': '1', 'census_block': '1000', 'neighborhood': 'UNKNOWN', ':#computed_region_fk4y_hpmh': '5', ':#computed_region_eziv_p4ck': '51', ':#computed_region_tmcg_v66k': '7', ':#computed_region_kwzn_pe6v': '5', ':#computed_region_uh5x_q5mi': '190', ':#computed_region_dwzh_dtk5': '944', ':#computed_region_b3rm_3c8a': '28', ':#computed_region_xbxg_7ifr': '25', ':#computed_region_urdz_b6n8': '2'}
]
def filterByMonth(data, month):
result = []
for item in data:
datestring = item['issued']
dt = datetime.strptime(datestring, '%Y-%m-%dT%H:%M:%S.%f')
if dt.month == month:
result.append(item)
return result
print(filterByMonth(data, 9))
and a more pythonic way would be this
def filterByMonth(data, month):
return [item for item in data if datetime.strptime(item['issued'], '%Y-%m-%dT%H:%M:%S.%f').month == month]

How to get block storage info after creating it with softlayer Python API

Now I can use softlayer Python API to create the block storage, command like this:
client['SoftLayer_Product_Order'].placeOrder(orderData)
This command can be executed and response looks like this:
{'orderId': 11999815, 'placedOrder': {'status': 'PENDING_AUTO_APPROVAL', 'account': {'companyName': 'xxxxxx', 'id': 323716, 'brandId': 11246}, 'orderQuoteId': '', 'userRecordId': 426607, 'orderTypeId': 4, 'items': [{'itemId': 5936, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': 'Endurance Storage', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45064', 'children': [{'itemId': 5944, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': 'Block Storage', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45104', 'categoryCode': 'storage_block', 'parentId': 171180595, 'id': 171180597}, {'itemId': 5940, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': '2 IOPS per GB', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45084', 'categoryCode': 'storage_tier_level', 'parentId': 171180595, 'id': 171180599}, {'itemId': 5138, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '36.25', 'description': '250 GB Storage Space', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45254', 'categoryCode': 'performance_storage_space', 'parentId': 171180595, 'id': 171180601}, {'itemId': 6028, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '.36', 'description': '5 GB Storage Space (Snapshot Space)', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '46136', 'categoryCode': 'storage_snapshot_space', 'parentId': 171180595, 'id': 171180603}], 'categoryCode': 'storage_service_enterprise', 'parentId': '', 'id': 171180595}, {'itemId': 5944, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': 'Block Storage', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45104', 'categoryCode': 'storage_block', 'parentId': 171180595, 'id': 171180597}, {'itemId': 5940, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': '2 IOPS per GB', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45084', 'categoryCode': 'storage_tier_level', 'parentId': 171180595, 'id': 171180599}, {'itemId': 5138, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '36.25', 'description': '250 GB Storage Space', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45254', 'categoryCode': 'performance_storage_space', 'parentId': 171180595, 'id': 171180601}, {'itemId': 6028, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '.36', 'description': '5 GB Storage Space (Snapshot Space)', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '46136', 'categoryCode': 'storage_snapshot_space', 'parentId': 171180595, 'id': 171180603}], 'accountId': 323716, 'userRecord': {'username': 'xxxx', 'lastName': 'xxxx', 'id': 426607, 'firstName': 'xxxx', 'accountId': xxxx}, 'id': 11999815, 'presaleEventId': ''}, 'orderDate': '2017-01-04T00:42:21-08:00', 'orderDetails': {'preTaxSetup': '0', 'storageGroups': [], 'postTaxRecurring': '36.61', 'billingOrderItemId': '', 'presetId': '', 'prices': [{'itemId': 5936, 'setupFee': '0', 'recurringFee': '0', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '0', 'description': 'Endurance Storage', 'bundle': [], 'keyName': 'CODENAME_PRIME_STORAGE_SERVICE', 'units': 'N/A', 'id': 5936}, 'id': 45064, 'categories': [{'categoryCode': 'storage_service_enterprise', 'id': 394, 'name': 'Endurance'}]}, {'itemId': 5944, 'setupFee': '0', 'recurringFee': '0', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '0', 'description': 'Block Storage', 'bundle': [], 'keyName': 'BLOCK_STORAGE_2', 'units': 'N/A', 'id': 5944}, 'id': 45104, 'categories': [{'categoryCode': 'storage_block', 'id': 398, 'name': 'Block Storage'}]}, {'itemId': 5940, 'setupFee': '0', 'recurringFee': '0', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '0', 'description': '2 IOPS per GB', 'bundle': [], 'keyName': 'READHEAVY_TIER', 'units': 'N/A', 'id': 5940}, 'id': 45084, 'categories': [{'categoryCode': 'storage_tier_level', 'id': 396, 'name': 'Storage Tier Level'}]}, {'itemId': 5138, 'setupFee': '0', 'recurringFee': '36.25', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '250', 'description': '250 GB Storage Space', 'bundle': [], 'keyName': '250_GB_PERFORMANCE_STORAGE_SPACE', 'units': 'GB', 'id': 5138}, 'id': 45254, 'categories': [{'categoryCode': 'performance_storage_space', 'id': 382, 'name': 'Storage Space'}]}, {'itemId': 6028, 'setupFee': '0', 'recurringFee': '.36', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '5', 'description': '5 GB Storage Space', 'bundle': [], 'keyName': '5_GB_STORAGE_SPACE', 'units': 'GB', 'id': 6028}, 'id': 46136, 'categories': [{'categoryCode': 'storage_snapshot_space', 'id': 402, 'name': 'Storage Snapshot Space'}]}], 'sendQuoteEmailFlag': '', 'packageId': 240, 'useHourlyPricing': False, 'preTaxRecurringMonthly': '36.61', 'message': '', 'preTaxRecurring': '36.61', 'billingInformation': {'billingNameFirst': 'xxxxx', 'billingPostalCode': 'xxxxxx', 'billingPhoneVoice': 'xxxxx', 'billingNameCompany': 'xxxxxx', 'billingAddressLine1': 'xxxxx', 'cardExpirationMonth': '', 'billingNameLast': 'Urbisci', 'cardExpirationYear': '', 'billingState': 'xxxx', 'billingCountryCode': 'xxxx', 'billingEmail': 'xxxxxx', 'taxExempt': 0, 'billingCity': 'San Jose'}, 'primaryDiskPartitionId': '', 'locationObject': {'id': 1004995, 'name': 'sjc03', 'longName': 'San Jose 3'}, 'taxCompletedFlag': True, 'isManagedOrder': '', 'originVolumeScheduleId': '', 'imageTemplateId': '', 'postTaxRecurringMonthly': '36.61', 'resourceGroupTemplateId': '', 'postTaxSetup': '0', 'sshKeys': [], 'location': '1004995', 'stepId': '', 'proratedInitialCharge': '33.07', 'totalRecurringTax': '0', 'originVolumeId': '', 'osFormatType': {'createDate': '', 'keyName': 'LINUX', 'id': 12}, 'paymentType': 'ADD_TO_BALANCE', 'resourceGroupId': '', 'sourceVirtualGuestId': '', 'bigDataOrderFlag': False, 'properties': [], 'extendedHardwareTesting': '', 'preTaxRecurringHourly': '0', 'postTaxRecurringHourly': '0', 'taxCacheHash': '97601ef55aa1f213ad046476769b64f57ce4d1ab', 'currencyShortName': 'USD', 'itemCategoryQuestionAnswers': [], 'containerSplHash': '000000001fe660de00007f62a57eefc9', 'proratedOrderTotal': '33.07', 'serverCoreCount': '', 'privateCloudOrderFlag': False, 'totalSetupTax': '0', 'quantity': 1}}
The problem is the response does not provide any useful info to identify the storage, such as storage id or storage name.
I know the API to get all storage info:
client['SoftLayer_Account'].getIscsiNetworkStorage()
But I can't identify which one I just created(storage name is automated, created by API), check the created time is not a good idea.
The response from SoftLayer_Product_Order::placeOrder method contains an orderId property
'orderId': 11999815
We can find the storage through this, try this script for it:
"""
Get storage through orderId
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNetworkStorage
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Storage
http://sldn.softlayer.com/article/object-filters
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn#softlayer.com>
"""
import SoftLayer
"""
Your SoftLayer API username and key.
"""
USERNAME = 'set me'
API_KEY = 'set me'
# Define the orderId from block storage
orderId = 11999815
# Declare a new API service object
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
accountService = client['Account']
# Define an object filter to get storage through orderId
objectFilter = {"iscsiNetworkStorage": {"billingItem": {"orderItem": {"order": {"id": {"operation": orderId}}}}}}
try:
result = accountService.getIscsiNetworkStorage(filter=objectFilter)
print(result)
except SoftLayer.SoftLayerAPIError as e:
print("Error. "
% (e.faultCode, e.faultString))
I hope it helps

Convert list-of-dicts into tree

For two days I try to traverse a list of dicts into a tree.
`list_of_dicts = [
{'name':Category1, 'id': '7', 'parent_id': '7', 'level': '1'}
{'name':Category3, 'id': '33', 'parent_id': '7', 'level': '2'}
{'name':Category5, 'id': '334', 'parent_id': '33', 'level': '3'}
{'name':Category10, 'id': '23', 'parent_id': '7', 'level': '2'}
{'name':Category2, 'id': '8', 'parent_id': '8', 'level': '1'}
{'name':Category6, 'id': '24', 'parent_id': '8', 'level': '2'}
]`
As informations, we know a category on top level (1), has its own id as its parent_id, children have the id of its parent as parent_id and the level.
In a first step the list need to turn in something like a tree:
`traversed_list = [
{'name':Category1, 'id': '7', 'parent_id': '7', 'level': '1', 'children':
[
{'name':Category3, 'id': '33', 'parent_id': '7', 'level': '2', 'children': [
{'name':Category5, 'id': '334', 'parent_id': '33', 'level': '3', 'children':[]}]}
{'name':Category10, 'id': '23', 'parent_id': '7', 'level': '2', 'children':[]}
]}
{'name':Category2, 'id': '8', 'parent_id': '8', 'level': '1', 'children':
[{'name':Category6, 'id': '24', 'parent_id': '8', 'level': '2', 'children':[]}]
}]`
The following code:
import copy
def treeify(lst):
tree = [copy.deepcopy(cat) for cat in lst if cat['level'] == '1']
for el in tree:
el["children"] = []
for i in xrange(len(lst)):
for j in xrange(len(tree)):
if lst[i]["parent_id"] == tree[j]["id"]:
tree[j]["children"].append(copy.deepcopy(lst[i]))
return tree
list_of_dicts = [
{'name':"Category1", 'id': '7', 'parent_id': '7', 'level': '1'},
{'name':"Category3", 'id': '33', 'parent_id': '7', 'level': '2'},
{'name':"Category5", 'id': '334', 'parent_id': '33', 'level': '3'},
{'name':"Category10", 'id': '23', 'parent_id': '7', 'level': '2'},
{'name':"Category2", 'id': '8', 'parent_id': '8', 'level': '1'},
{'name':"Category6", 'id': '24', 'parent_id': '8', 'level': '2'}
]
tree = treeify(list_of_dicts)
for d in tree:
print d
prints
{'id': '7', 'parent_id': '7', 'children': [{'id': '7', 'parent_id': '7', 'name': 'Category1', 'level': '1'}, {'id': '33', 'parent_id': '7', 'name': 'Category3', 'level': '2'}, {'id': '23', 'parent_id': '7', 'name': 'Category10', 'level': '2'}], 'name': 'Category1', 'level': '1'}
{'id': '8', 'parent_id': '8', 'children': [{'id': '8', 'parent_id': '8', 'name': 'Category2', 'level': '1'}, {'id': '24', 'parent_id': '8', 'name': 'Category6', 'level': '2'}], 'name': 'Category2', 'level': '1'}

Categories