How to get a clean result out of DynamoDB? - python-3.x

I'm using DynamoDB and when I use the funcion boto3.get_item() it returns me a dict like that:
{'last_updated': {'S': '2020-02-11 21:48:05'},
'id_': {'S': 'ID_DA_SURVEY'},
'deleted': {'BOOL': False},
'created': {'S': '2020-02-11 21:48:05'},
'activated': {'BOOL': False},
'step': {'S': 'editor'},
'owner': {'N': '12'},
'built': {'BOOL': False},
'name': {'S': 'Survey UM'},
'up_lime': {'BOOL': False},
'lang': {'S': 'pt'},
'teste': {'M': {'test': {'M': {'test2': {'N': '54'}}}}}
}
There is a way so I can get my Dict like that:
{'last_updated': '2020-02-11 21:48:05',
'id_': 'ID_DA_SURVEY',
'deleted': False,
'created': '2020-02-11 21:48:05',
'activated': False,
'step': 'editor',
'owner': '12',
'built': False,
'name': 'Survey UM',
'up_lime': False,
'lang': 'pt',
'teste': {'test': {'test2': '54'}}
}
I Just need to get rid of the Attribut Type. Can anyone help me?

You can use boto3 class named TypeDeserializer from the dynamodb types package.
Here's an example:
import pprint
from boto3.dynamodb.types import TypeDeserializer
item = {
'last_updated': {
'S': '2020-02-11 21:48:05'
},
'id_': {
'S': 'ID_DA_SURVEY'
},
'deleted': {
'BOOL': False
},
'created': {
'S': '2020-02-11 21:48:05'
},
'activated': {
'BOOL': False
},
'step': {
'S': 'editor'
},
'owner': {
'N': '12'
},
'built': {
'BOOL': False
},
'name': {
'S': 'Survey UM'
},
'up_lime': {
'BOOL': False
},
'lang': {
'S': 'pt'
},
'teste': {
'M': {
'test': {
'M': {
'test2': {
'N': '54'
}
}
}
}
}
}
type_deserializer = TypeDeserializer()
py_data = {k: type_deserializer.deserialize(v) for k,v in item.items()}
pprint.pprint(py_data)
The output is:
{'activated': False,
'built': False,
'created': '2020-02-11 21:48:05',
'deleted': False,
'id_': 'ID_DA_SURVEY',
'lang': 'pt',
'last_updated': '2020-02-11 21:48:05',
'name': 'Survey UM',
'owner': Decimal('12'),
'step': 'editor',
'teste': {'test': {'test2': Decimal('54')}},
'up_lime': False}

Related

Filtering list of dicts based on a key value in python

I have a list of dictionaries in python which looks like below
list = [{'entityType': 'source', 'databaseName': 'activities', 'type': 'POSTGRES', 'children': [{'id': '3c144414-0c73-41df-9f0e-4dd7cb5af46e',
'path': ['Activities (DEV)', 'public'],
'type': 'CONTAINER',
'containerType': 'FOLDER'}]'checkTableAuthorizer': False},
{'entityType': 'source', 'databaseName': 'pd-prod-dev', 'type': 'POSTGRES', 'children':
[{'id': '75d84ead-a9fe-4949-bc21-d4deb34e1ae1',
'path': ['pg-prd (DEV-RR)', 'pghero'],
'tag': 'PWGqdrkcD08=',
'type': 'CONTAINER',
'containerType': 'FOLDER'},
{'id': 'facc2c20-7561-430f-ac35-547b5bc7a92f',
'path': ['pg-prd (DEV-RR)', 'public'],
'tag': 'gcUL0NTOc+4=',
'type': 'CONTAINER',
'containerType': 'FOLDER'}]'checkTableAuthorizer': False},
{'entityType': 'source', 'databaseName': 'pd-prod-prd', 'type': 'POSTGRES', 'children':
[{'id': '75d84ead-a9fe-4949-bc21-d4deb34e1ae1',
'path': ['pg-prd (PRD-RR)', 'pghero'],
'tag': 'PWGqdrkcD08=',
'type': 'CONTAINER',
'containerType': 'FOLDER'},
{'id': 'facc2c20-7561-430f-ac35-547b5bc7a92f',
'path': ['pg-prd (PRD-RR)', 'public'],
'tag': 'gcUL0NTOc+4=',
'type': 'CONTAINER',
'containerType': 'FOLDER'}]'checkTableAuthorizer': False}]
This is just a sample. The actual list has a list of 30 dictionaries. What I am trying to do is filter out the dictionaries where the nested children dictionary has only ' public' schema in it. So my expected output would be
public_list = [{'entityType': 'source', 'databaseName': 'activities', 'type': 'POSTGRES', 'children': [{'id': '3c144414-0c73-41df-9f0e-4dd7cb5af46e',
'path': ['Activities (DEV)', 'public'],
'type': 'CONTAINER',
'containerType': 'FOLDER'}]'checkTableAuthorizer': False},
{'entityType': 'source', 'databaseName': 'pd-prod-dev', 'type': 'POSTGRES', 'children':
[{'id': 'facc2c20-7561-430f-ac35-547b5bc7a92f',
'path': ['pg-prd (DEV-RR)', 'public'],
'tag': 'gcUL0NTOc+4=',
'type': 'CONTAINER',
'containerType': 'FOLDER'}]'checkTableAuthorizer': False},
{'entityType': 'source', 'databaseName': 'pd-prod-prd', 'type': 'POSTGRES', 'children':
[{'id': 'facc2c20-7561-430f-ac35-547b5bc7a92f',
'path': ['pg-prd (PRD-RR)', 'public'],
'tag': 'gcUL0NTOc+4=',
'type': 'CONTAINER',
'containerType': 'FOLDER'}]'checkTableAuthorizer': False}]
I tried accessing the nested dict children by iterating but unable to filter out what condition to use
for d in list:
for k, v in d.items():
if k == 'children':
print(v)
I would love to apply this as a function since I'll be reusing it on a pandas column of list of dicts
You could create a function that gets the public data for children of each entry:
def get_public_data(data):
result = []
children = data.get("children")
if children:
for row in children:
path = row.get("path")
if path and "public" in path:
result.append(row)
return result
And then create a new list of entries where you just replace the children key:
public_list = []
for x in entities:
public_data = get_public_data(x)
if public_data:
public_list.append({**x, "children": public_data})
Combine these two and you'll get the function you need.
IIUC you want to collect the entries were all items have a public schema?
Assuming your 'children' keys are always valid and a tuple of 2 elements, you can use a simple comprehension:
[d for d in lst
if all(e['path'][1] == 'public' for e in d['children'])
]
NB. I called your input lst as list is a python builtin

Having issue while merging graph in xlsxwriter

I am trying to merge two graphs in xlsxwriter.
1 : a line graph
2 : a stacked graph having y2 axis.
While merging for the second graph the staring value is coming from the negative value of the line graph. Like below image. I want it start from x axis only i.e from 0. Please find the code i hv used for both graphs.
line_chart.set_x_axis({
'name_font': {'size': 14, 'bold': True},
'major_gridlines': {'visible': False},
'major_tick_mark': 'none',
'minor_tick_mark': 'none'
})
line_chart.set_y_axis({
'name': 'Sales',
'name_font': {'size': 14, 'bold': True},
'major_gridlines': {'visible': False},
'major_tick_mark': 'none',
'minor_tick_mark': 'none',
})
line_chart.add_series({
'name': ' ',
'categories': '=data_sheet!$F$2:$F$25',
'values': '=data_sheet!$D$2:$D$25',
'line': {'color': '#FFCC00'},
'marker': {'type': 'square',
'border': {'color': 'red'},
'fill': {'color': 'red'}
},
})
bar_chart = workbook.add_chart({'type': 'column', 'subtype': 'stacked'})
bar_chart.set_x_axis({
'name_font': {'size': 14, 'bold': True},
'major_gridlines': {'visible': False},
'major_tick_mark': 'none',
'minor_tick_mark': 'none'
})
bar_chart.set_y2_axis({
'name': 'Traffic',
'name_font': {'size': 14, 'bold': True},
'major_gridlines': {'visible': False},
'major_tick_mark': 'none',
'minor_tick_mark': 'none'
})
bar_chart.add_series({
'name': '',
'fill': {'color': 'white'},
'categories': '=data_sheet!$F$2:$F$25',
'values': '=data_sheet!$B$2:$B$25',
'y2_axis': True,
})
bar_chart.add_series({
'name': '',
'fill': {'color': '#117A9A'},
'categories': '=data_sheet!$F$2:$F$25',
'values': '=data_sheet!$A$2:$A$25',
'y2_axis': True,
})
line_chart.combine(bar_chart)
line_chart.set_legend({'position': 'low'})
line_chart.set_size({'width': 1025, 'height': 360})
line_chart.set_chartarea({'border': {'none': True}})

Match key and value from two different dictionaries and merge them

I have this two dictionaries:
{'data': {'id': '001_101_001', 'name': 'chview', 'type': 'multiple', 'mapping': {}},
{'id': '001_102_001', 'name': 'view', 'type': 'binary', 'mapping': {'abc':'exp'}}
And:
{'queries':{'view': 'text', 'chview': 'text1'}}
The desired output should be:
{'new_data' : {'001_101_001': { 'query': 'text1', 'type': 'multiple', 'mapping': {}},
'001_102_001': { 'query1': 'text', 'type': 'binary', 'mapping': {'abc':'exp'}}
Because there are a lot of this dictionaries I need to match them by 'name', to have the coresponding id matched. Any ideas?
Your first dictionary has a problem, it is not hashable. It should be a list of dictionaries.
{"data" :[
{'id': '001_101_001', 'name': 'chview', 'type': 'multiple', 'mapping': {}},
{'id': '001_102_001', 'name': 'view', 'type': 'binary', 'mapping': {'abc':'exp'}}
]}
Complete code:
data = {"data" :[
{'id': '001_101_001', 'name': 'chview', 'type': 'multiple', 'mapping': {}},
{'id': '001_102_001', 'name': 'view', 'type': 'binary', 'mapping': {'abc':'exp'}}
]}
queries = {"queries" : {'view': 'text', 'chview': 'text1'}}
new_data = {}
for d in data["data"]:
item = {d["id"] : {
"query": queries["queries"][d["name"]],
"type": d["type"],
"mapping": d["mapping"]
}}
new_data.update(item)
print({"new_data": new_data})
OUTPUT:
{'new_data': {'001_101_001': {'query': 'text1', 'type': 'multiple', 'mapping': {}}, '001_102_001': {'query': 'text', 'type': 'binary', 'mapping': {'abc': 'exp'}}}}

Trying to sort a list of dictionaries in side another dictionary

I need to sort a list of dictionaries to able to compare the data.
I tried the sort() function of list
I tried sorted(My_dict["IndividualDUC"][List hardware], key=lambda x: x[1])
Dictionary before sort
My_dict = {'IndividualDUC':
{'List hardware':
[
{'AMDEVBOS': {'Group': 'Default','Version': 'Not installed'}},
{'ATM0-IT-QAV1': {'Group': 'IT', 'Version': 'Not installed'}},
{'ATM0-PEO-QAV2': {'Group': 'PECO', 'Version': '10.0.40219'}},
{'ATM0-PEO-QAV1': {'Group': 'PECO', 'Version': '10.0.40219'}},
{'MSTQA-ITQA-VM2': {'Group': 'msGrp1', 'Version': '10.0.40219'}},
{'ATM0-IT-QAV2': {'Group': 'IT', 'Version': '10.0.40219'}},
]
},
'DUCPercentages':
{'List hardware':
{'Not installed': {'Percentage': '18.2', 'Count': 2},
'10.0.40219': {'Percentage': '81.8', 'Count': 9}
}
}
}
Expect the output:
reuslt = {'IndividualDUC':
{'List hardware':
[
{'AMDEVBOS': {'Group': 'Default','Version': 'Not installed'}},
{'ATM0-IT-QAV1': {'Group': 'IT', 'Version': 'Not installed'}},
{'ATM0-IT-QAV2': {'Group': 'IT', 'Version': '10.0.40219'}},
{'ATM0-PEO-QAV2': {'Group': 'PECO', 'Version': '10.0.40219'}},
{'ATM0-PEO-QAV1': {'Group': 'PECO', 'Version': '10.0.40219'}},
{'MSTQA-ITQA-VM2': {'Group': 'msGrp1', 'Version': '10.0.40219'}},
]
},
'DUCPercentages':
{'List hardware':
{'Not installed': {'Percentage': '18.2', 'Count': 2},
'10.0.40219': {'Percentage': '81.8', 'Count': 9}
}
}
}

Button alignment in Adaptive cards

I was wondering if there is a way to control alignment of buttons in Adaptive card in Bot Emulator.
I tried the same code in the emulator, and the Microsoft Visualizer, but they render differently. Here are the images: Emulator Visualizer
Here's the code I've used:
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
'$schema': 'http://adaptivecards.io/schemas/adaptive-card.json',
'version': '1.0',
'type': 'AdaptiveCard',
'body': [
{
'type': 'TextBlock',
'text': 'Meeting Title',
'weight': 'bolder'
},
{
'type': 'TextBlock',
'text': 'Location',
'separator': true,
'isSubtle': true,
'size': 'small'
},
{
'type': 'TextBlock',
'text': 'Location',
'spacing': 'none'
},
{
'type': 'TextBlock',
'text': 'Organizer',
'separator': true,
'isSubtle': true,
'size': 'small'
},
{
'type': 'TextBlock',
'text': 'Organizer Name',
'spacing': 'none'
},
{
'type': 'TextBlock',
'text': 'Start Time',
'separator': true,
'isSubtle': true,
'size': 'small'
},
{
'type': 'ColumnSet',
'spacing': 'none',
'columns': [
{
'type': 'Column',
'width': 'auto',
'items': [
{
'type': 'TextBlock',
'text': '05:00 PM',
'isSubtle': false,
'weight': 'bolder'
}
]
},
{
'type': 'Column',
'width': 'auto',
'items': [
{
'type': 'TextBlock',
'text': 'May 21'
}
]
},
{
'type': 'Column',
'width': 'auto',
'items': [
{
'type': 'TextBlock',
'text': '2017',
'isSubtle': true,
'weight': 'bolder'
}
]
}
]
},
{
'type': 'TextBlock',
'text': 'End Time',
'separator': true,
'isSubtle': true,
'size': 'small'
},
{
'type': 'ColumnSet',
'spacing': 'none',
'columns': [
{
'type': 'Column',
'width': 'auto',
'items': [
{
'type': 'TextBlock',
'text': '05:30 PM',
'isSubtle': false,
'weight': 'bolder'
}
]
},
{
'type': 'Column',
'width': 'auto',
'items': [
{
'type': 'TextBlock',
'text': 'May 21'
}
]
},
{
'type': 'Column',
'width': 'auto',
'items': [
{
'type': 'TextBlock',
'text': '2017',
'isSubtle': true,
'weight': 'bolder'
}
]
}
]
}
],
'actions': [
{
'type': 'Action.Submit',
'title': 'Accept',
'data':{
'accept': true
}
},
{
'type': 'Action.Submit',
'title': 'Decline',
'data':{
'accept': false
}
}
]
}
}
As seen, the buttons are aligned horizontally in the emulator, and next to each other in the visualizer. Is there a way to modify the height and width of the buttons, as well as the way they are aligned?
I was wondering if there is a way to control alignment of buttons in
Adaptive card in Bot Emulator.
Short answer is "No". You cannot modify the render of components in the Emulator.
Long answer is: Bot Framework Emulator is open-source, so you can try to modify and run locally your custom emulator. But I'm not sure that making a custom render on the emulator is very useful for real projects, as they will not run on the emulator.
Emulator sources are located here: https://github.com/Microsoft/BotFramework-Emulator
There is something called Hostconfig with Adaptive cards, try using that. Have shared the editor tool link.

Resources