How to set the field values in namedTuple on Python? - python-3.7

In my pytest (python 3.7.4), I have test method which define and set the values for the fields of namedtuple. But I do not actually see any values set in to the field though I set the values in my code.
def test_TapVisaCardOnTheReader(Process):
ResponseResults = namedtuple('ResponseResults',['STATUS', 'ISO8583', 'TOKEN', 'ICC_PUBLIC_KEY'])
ResponseResults('01', True, True, True)
TapResponseResults=Process.TappingPaymentCardOntheReader(' Visa-Card ')
assert ((ResponseResults.STATUS == TapResponseResults.STATUS) and (
ResponseResults.ISO8583 == TapResponseResults.ISO8583) and (
ResponseResults.TOKEN == TapResponseResults.TOKEN) and (
ResponseResults.ICC_PUBLIC_KEY == TapResponseResults.ICC_PUBLIC_KEY))
Please check the following debug output window where I do not see any values have been set.
Also I have one more question about comparison of namedtuple fields, In my code I had to compare every single filed of namedtuple ,instead is there any way to compare all the fields of namedtuple at once.

In this part of the code, you create the ResponseResults object without saving it:
ResponseResults = namedtuple('ResponseResults',['STATUS', 'ISO8583', 'TOKEN', 'ICC_PUBLIC_KEY'])
ResponseResults('01', True, True, True)
What you actually want is:
ResponseResults = namedtuple('ResponseResults',['STATUS', 'ISO8583', 'TOKEN', 'ICC_PUBLIC_KEY'])
response_results = ResponseResults('01', True, True, True)
# continue with response_results...
Edit: Regarding your second question: if you want to compare all fields of two namedtuples directly, you can use the == operator:
from collections import namedtuple
ResponseResults = namedtuple('ResponseResults',['STATUS', 'ISO8583', 'TOKEN', 'ICC_PUBLIC_KEY'])
response_results = ResponseResults('01', True, True, True)
response_results_2 = ResponseResults('01', True, True, True)
response_results_3 = ResponseResults('01', True, True, False)
response_results == response_results_2 # this is True
response_results == response_results_3 # this is False

Related

How can I CORRECTLY append map array to firestore?

I want to add Map<> data to my firestore database with code :
Map<String, Object> prop = {
'read' : true,
'vote_left' : false,
'vote_right' : false,
};
Map<String, Object> attended_topic =
{
received_id:prop
};
FirebaseFirestore.instance.collection('userinfo').doc(user_now!.uid)
.update({"attended_topic": FieldValue.arrayUnion([attended_topic])});
What I expected is this.
attended_topic
topicId_0
read : true
vote_left : false
vote_right : false
But I got something unexpected.
attended_topic
0
topicId_0
read : true
vote_left : false
vote_right : false
I never expected that new category '0' appearing and donot know why. Since the atabase has other properties so I thought using update() rather than set() is adequate. Please somebody tell my why this happens.
From the docs
FieldValue.arrayUnion adds elements to an array but only elements not already present.
So {"a": FieldValue.arrayUnion([b])} adds b variable to the Array a.
To solve your problem, just remove FieldValue as shown below.
FirebaseFirestore.instance
.collection('userinfo')
.doc(user_now!.uid)
.set({"attended_topic": attended_topic}, SetOptions(merge: true));
// use set method to add new data (not update)
// or
FirebaseFirestore.instance.collection('userinfo').doc(user_now!.uid).set(
{
'attended_topic': {
received_id: {
'read': true,
'vote_left': false,
'vote_right': false,
}
}
},
SetOptions(merge: true),
);
I solved this problem referring Peter's solution and changing it slightly.
FirebaseFirestore.instance.collection('userinfo').doc(user_now!.uid)
.set({"attended_topic": attended_topic}, SetOptions(merge: true));

Ag-grid valueFormatter and Column Filter

I am having problems using ag-grid valueFormatter and column filters (https://www.ag-grid.com/javascript-data-grid/filtering/).
I have a simple colDef:
{
headerName: 'My column',
field: 'myData',
hide: true,
valueFormatter: this.formatterBooleanToHuman,
},
the formatterBooleanToHuman is a simple code to change true to Yes and false to No.
It works as expected, the issue is that we are using column filters, and when I click on the filter I have true and false to select, if I select any of them, nothing returns from the filters because the value now is actually Yes and No.
I couldn't manage to have both of them working together. To have the column filter working properly I need to remove the valueFormatter, but I would like to have both working.
I tried to apply the valueFormatter function to filterParams.valueFormatter, it did change the values on the filter but something is failing, I am getting 2 No and 1 Yes, and none of them filter.
Any suggestions?
UPDATE:
So, I found a solution, but I am not convinced it is the right way to do it.
get getcolumnDef(): Array<ColDef> {
return [
{
headerName: 'Boolean Column',
field: 'booleanValue',
hide: true,
valueFormatter: this.formatterBooleanToHuman,
filterParams: {
valueGetter: (params) => this.filterBooleanValueGetter(params, 'booleanValue')
}
}
];
}
private filterBooleanValueGetter(params: ValueGetterParams, propertyName: string) {
let isDeleted = false;
const hasValue = !!params && !!params.data && params.data[propertyName];
if (hasValue) {
isDeleted = String(params.data[propertyName]) === 'true';
}
return isDeleted ? 'Yes' : 'No';
}
So, the valueGetter works as expected and makes my filter work, I just think it is a bit "dirty" to have it to work like that, I haven't found anything on the docs saying this is the way it needs to be done. So suggestions are more than welcome.
valueFormatter applies only to data in grid. However even if the filter shows true and false instead of formatted values, it should work correctly. If filtering does not work, it may indicate some other error in your code. Maybe you depend on this in formatterBooleanToHuman method?
Anyway to format values in filter, you should define filterParams.valueFormatter like this:
{
// ...
valueFormatter: this.formatterBooleanToHuman,
filterParams: {
valueFormatter: this.formatterBooleanToHuman
}
}
For some reason, the value given to filter formatter is string instead of boolean (bug in ag-grid?), you need to adjust that.
See complete example here: https://plnkr.co/edit/o3sN3GodqQumVe09

How to get the length of map values flutter

Map<String, bool> _toDos = {
'case1': true,
'case2': false,
'case3': false,
'case4': false,
'case5' : false
};
Say I have a map object like this and I want to get the length of strings that contains "true" values. How do I do so with flutter?
_toDos.length gives me the length of the map but I want to get the length of items that contain only "true" values.
_todos.values.where((element) => element == true).length

Dart how do I find the index of true value in the list

I have list List<bool> _selections = [false, true, false]; and this list may change only one can be true
How do I know which index is true ?
you can use indexWhere
_selections.indexWhere((value) => value)
You can try indexWhere() method.
_selections.indexWhere((ele) => ele);
See more
while the above answer are correct and simpler you can also use a good old foreach loop like this:
for (var elements in _selections) {
if (elements == true) {
print(elements);
}
}

Append in jsonb array and update existing record based on a key

I have a table media with a jsonb array field pictures that contains an empty array.
The idea is that every time I append a new json object, I want to switch the default attribute from any previous one from true to false.
Sample object:
{"file": "file.jpg", "default": true}
I accomplished that with 2 different queries.
One for inserting a new record:
update media
set pictures = jsonb_set(
pictures,
concat('{' , jsonb_array_length(pictures) , '}')::text[],
jsonb_build_object('file', 'somepicture.jpg', 'default', true)
)
where user_id = 8
And one for switching from default: true to default:false
update media
set pictures =
(
select
jsonb_agg(
case when value->>'default' = 'true' and value->>'file' != 'somepicture.jpg'
then value || jsonb_build_object('default', false)
else value
end
)
from jsonb_array_elements(media.pictures)
)
where user_id = 8
My final pictures array:
[
{
"file": "previouspicture.jpg",
"default": false
},
{
"file": "somepicture.jpg",
"default": true
}
]
How can I achieve the same thing, with only one query?
Use an extra jsonb_set() to update the previous object. You have to use coalesce() to be able to start with an empty array:
update media
set pictures = jsonb_set(
coalesce(
jsonb_set(
pictures,
array[(jsonb_array_length(pictures)-1)::text],
pictures->jsonb_array_length(pictures)-1 || '{"default": false}'
),
pictures
),
array[(jsonb_array_length(pictures))::text],
jsonb_build_object('file', 'filename.jpg', 'default', true)
)
where user_id = 8
returning *;
DbFiddle.