for loop produces unexpected result in Python - loops

I'm struggling with this 'simple' code. I have the following list of objects:
[
{
"element_id": "6185316",
"id": 123456,
"inv_part_id": 2345250,
"is_spare": true,
"num_sets": 191,
"part": {
"external_ids": {
"BrickOwl": [
"359273"
],
"NODEX": [
"17715",
"19385"
]
},
"name": "Bar 3L",
"part_cat_id": 32,
"part_num": "27808",
"print_of": null
},
"quantity": 1,
"set_num": "71043-1"
},
{
"element_id": "6185316",
"id": 654321,
"inv_part_id": 2345250,
"is_spare": true,
"num_sets": 191,
"part": {
"external_ids": {
"BrickOwl": [
"359273"
],
"NODEX": [
"17715",
"19385"
]
},
"name": "Bar 3L",
"part_cat_id": 32,
"part_num": "5861",
"print_of": null
},
"quantity": 1,
"set_num": "71043-1"
}]
My goal is to just add the value of part_num (inside PART object) to a list. I'm using the following for this:
final = []
count = 1
for x in spf:
for y in x["part"]["part_num"]:
final.append(y)
but it is appending each char instead of adding the complete value:
['2', '7', '8', '0', '8', '5', '8', '6', '1']
The expected output should be:
['27808', '5861']
What am I doing wrong?

Related

How do i access a embedded field from different collection after loopup operation?

I have 4 collections of players stats:
Laliga2017,Laliga2018,Laliga2019 and Laliga2020
{
'_id': ObjectId('61eda25219e7e44801799d71'),
'name': 'Sergio Ramos',
'age': 34,
'nationality': 'Spain',
'height': 184,
'weight': 82,
'team': 'Real Madrid',
'position': 'DF',
'general_stats': {
'games': 15,
'time': 1280,
'red_cards': 0,
'yellow_cards': 3,
},
'offensive_stats': {
'goals': 2,
'xG': 3.95,
'assists': 0,
'xA': 0.4,
'shots': 17,
'key_passes': 4,
'npg': 0,
'npxG': 2.46,
'xGChain': 9.06,
'xGBuildup': 7.22,
},
'defensive_stats': {
'Tkl': 20,
'TklW': 12,
'Past': 8,
'Press': 97,
'Succ': 39,
'Blocks': 18,
'Int': 16,
},
'passing_stats': {
'Cmp': 1023,
'Cmp%': 91.8,
'1/3': 106,
'PPA': 2,
'CrsPA': 1,
'Prog': 61,
},
}
Is there a way to get the sum of all the goal_contribution(goals+assists) for all 4 years (for each year and then sum of all the years stats)?
I reached here:
db.LaLiga_2020.aggregate(
[
{ $match: { name: 'Lionel Messi' } },
{
$lookup: {
from: 'LaLiga_2019',
localField: 'name',
foreignField: 'name',
as: 'stats2019',
},
},
{
$lookup: {
from: 'LaLiga_2018',
localField: 'name',
foreignField: 'name',
as: 'stats2018',
},
},
{
$lookup: {
from: 'LaLiga_2017',
localField: 'name',
foreignField: 'name',
as: 'stats2017',
},
},
{
$project: {
_id: 0,
name: 1,
team: 1,
position: 1,
goal_cotribution_2020: { $add: ['$offensive_stats.goals', '$offensive_stats.assists'] },
goal_cotribution_2019: { $sum: ['$stats2019.offensive_stats.goals', '$stats2019.offensive_stats.assists'] },
goal_cotribution_2018: { $sum: ['$stats2018.offensive_stats.goals', '$stats2018.offensive_stats.assists'] },
goal_cotribution_2017: { $sum: ['$stats2017.offensive_stats.goals', '$stats2017.offensive_stats.assists'] },
},
},
],
).pretty();
But it returns:
{
'name': 'Lionel Messi',
'team': 'Barcelona',
'position': 'FW',
'goal_cotribution_2020': 39,
'goal_cotribution_2019': 0,
'goal_cotribution_2018': 0,
'goal_cotribution_2017': 0,
};
The issue is from your $project stage, specifically how you $sum:
goal_cotribution_2018: {
$sum: [
"$stats2018.offensive_stats.goals",
"$stats2018.offensive_stats.assists"
]
}
The problem is that stats2018 is an array after the $lookup stage. so "$stats2018.offensive_stats.goals" resolves into an array of numbers and not a number, imagine what you're doing is this:
goal_cotribution_2018: {
$sum: [
[12],
[5]
]
}
So while there is no error thrown this is undefined behavior and $sum just returns 0.
You can solve this in several different ways. you could $unwind or use $arrayElemAt, But I think the "cleanest" way is just to add an additional nested $sum, like so:
goal_cotribution_2018: {
$sum: [
{
$sum: "$stats2019.offensive_stats.goals"
},
{
$sum: "$stats2019.offensive_stats.assists"
},
]
},
Simplest way is adding all the fields at the end should work
{
"$project":{
"_id":0,
"name":1,
"team":1,
"position":1,
"goal_cotribution_2020":{
"$add":[
"$offensive_stats.goals",
"$offensive_stats.assists"
]
},
"goal_cotribution_2019":{
"$add":[
"$stats2019.offensive_stats.goals",
"$stats2019.offensive_stats.assists"
]
},
"goal_cotribution_2018":{
"$add":[
"$stats2018.offensive_stats.goals",
"$stats2018.offensive_stats.assists"
]
},
"goal_cotribution_2017":{
"$add":[
"$stats2017.offensive_stats.goals",
"$stats2017.offensive_stats.assists"
]
},
"total_goal_cotribution":{
"$add":[
"$offensive_stats.goals",
"$offensive_stats.assists",
"$stats2019.offensive_stats.goals",
"$stats2019.offensive_stats.assists",
"$stats2018.offensive_stats.goals",
"$stats2018.offensive_stats.assists",
"$stats2017.offensive_stats.goals",
"$stats2017.offensive_stats.assists"
]
}
}
}

Plot a Ganglia graph using AngularJS

I would like to plot a graph in AngularJS, using Highcharts, to be something like this:
This graph represents the load of a server in the last hour. So the datapoints given, contains a point and epoch time.
The data is received in a JSON format, [point,epochtime], as follows:
[
{
"ds_name": "a0",
"cluster_name": "",
"graph_type": "stack",
"host_name": "",
"metric_name": "1-min",
"color": "#BBBBBB",
"datapoints": [
[
0.58,
1604244900
],
[
0.59733333333,
1604244915
],
[
0.6,
1604244930
],
[
0.6,
1604244945
],
[
0.6,
1604244960
],
[
0.6,
1604244975
],
[
0.612,
1604244990
]
]
},
{
"ds_name": "a2",
"cluster_name": "",
"graph_type": "line",
"host_name": "",
"metric_name": "CPUs ",
"color": "#FF0000",
"datapoints": [
[
2,
1604244900
],
[
2,
1604244915
],
[
2,
1604244930
],
[
2,
1604244945
],
[
2,
1604244960
],
[
2,
1604244975
],
[
2,
1604244990
],
[
2,
1604245005
]
]
},
{
"ds_name": "a3",
"cluster_name": "",
"graph_type": "line",
"host_name": "",
"metric_name": "Procs",
"color": "#2030F4",
"datapoints": [
[
1,
1604244900
],
[
1,
1604244915
],
[
1,
1604244930
]
]
}
]
I posted here only part of the dataset, since it is too long, but I think you can understand the format.
How can I draw something similar using Highcharts?
The most important thing here is to properly parse the date. Since the names in the JSON are not the same as Highcharts requires you have to pass them manually to the right places as I did it in the demo below.
I also recommend using the keys property because the data should be declared as [x, y], in your case it is the other way around. You can change that by setting this property.
Notice that in the Highcharts there is no such series as stack, it is just column.
API: https://api.highcharts.com/highcharts/series.line.keys
Demo: https://jsfiddle.net/BlackLabel/246phxum/
So this is how I did it eventually, the most suitable types of charts are line, and area chart.
Highcharts.chart("container", {
chart: {
type: 'line'
},
xAxis: {
type: "datetime",
labels: {
formatter: function () {
return Highcharts.dateFormat('%H:%M:%S.%L', this.value);
}
}
},
series: [{
name: "A",
type: "area",
color: "#BB000B",
keys: ["y", "x"],
data: [
[0.58, 1604244900],
[0.59733333333, 1604244915],
[0.6, 1604244930],
[0.6, 1604244945],
[0.6, 1604244960],
[0.6, 1604244975],
[0.612, 1604244990]
]
},
{
name: "B",
type: "line",
color: "#FF00F0",
keys: ["y", "x"],
data: [
[2, 1604244900],
[2, 1604244915],
[2, 1604244930],
[2, 1604244945],
[2, 1604244960],
[2, 1604244975],
[2, 1604244990],
[2, 1604245005]
]
}, {
name: 'C',
keys: ['y', 'x'],
data: [
[1, 1604244900],
[1, 1604244915],
[1, 1604244930],
[1, 1604244945],
[1, 1604244960],
[1, 1604244975],
[1, 1604244990],
[1, 1604245005]
]
}
]
});

Flutter: Parse json arrays without name

I am getting json response from server as below.
[
[{
"ID": 1,
"Date": "11-09-2015",
"Balance": 1496693.00
}, {
"ID": 2,
"Date": "01-10-2015",
"Balance": 1496693.00
}],
[{
"ID": 1,
"Date": "03-09-2000",
"IntAmount": "003.00"
}],
[{
"EmployeeId": "000",
"DesignationName": "deg"
}],
[{
"LoanAmount": "00000.00",
"IntRate": "3.00",
"LoanNo": "56656"
}]
]
I can parse json array with name but in above json there are three arrays without name.
How to parse above json in three different arrays?
If you are positive that the data will always come in the stated format, then you can iterate through the result. See below for example:
main(List<String> args) {
// Define the array of data "object" like this
List<List<Map<String, dynamic>>> arrayOfData = [
[
{"ID": 1, "Date": "11-09-2015", "Balance": 1496693.00},
{"ID": 2, "Date": "01-10-2015", "Balance": 1496693.00}
],
[
{"ID": 1, "Date": "03-09-2000", "IntAmount": "003.00"}
],
[
{"EmployeeId": "000", "DesignationName": "deg"}
],
[
{"LoanAmount": "00000.00", "IntRate": "3.00", "LoanNo": "56656"}
]
];
/*
Iterate through the array of "objects" using forEach,
then, iterate through each resulting array using forEach
*/
arrayOfData.forEach((datasetArray) => datasetArray.forEach((dataset) => print(dataset)));
/*
============== RESULT ========
{ID: 1, Date: 11-09-2015, Balance: 1496693.0}
{ID: 2, Date: 01-10-2015, Balance: 1496693.0}
{ID: 1, Date: 03-09-2000, IntAmount: 003.00}
{EmployeeId: 000, DesignationName: deg}
{LoanAmount: 00000.00, IntRate: 3.00, LoanNo: 56656}
*/
}

jsonb query in postgres

I've a table in postgres named: op_user_event_data, which has a column named data, where I store a jsonb, and what I have at the moment is a json like this:
{
"aisles": [],
"taskGroups": [
{
"index": 0,
"tasks": [
{
"index": 1,
"mandatory": false,
"name": "Dados de Linear",
"structuresType": null,
"lines": [
{
"sku": {
"skuId": 1,
"skuName": "Limiano Bola",
"marketId": [
1,
3,
10,
17
],
"productId": 15,
"brandId": [
38,
44
]
},
"taskLineFields": [
{
"tcv": {
"value": "2126474"
},
"columnType": "skuLocalCode",
"columnId": 99
},
{
"tcv": {
"value": null
},
"columnType": "face",
"columnId": 29
},
]
},
{
"sku": {
"skuId": 3,
"skuName": "Limiano Bolinha",
"marketId": [
1,
3,
10,
17
],
"productId": 15,
"brandId": [
38,
44
]
},
"taskLineFields": [
{
"tcv": {
"value": "2545842"
},
"columnType": "skuLocalCode",
"columnId": 99
},
{
"tcv": {
"value": null
},
"columnType": "face",
"columnId": 29
},
]
},
{
"sku": {
"skuId": 5,
"skuName": "Limiano Bola 1/2",
"marketId": [
1,
3,
10,
17
],
"productId": 15,
"brandId": [
38,
44
]
},
"taskLineFields": [
{
"tcv": {
"value": "5127450"
},
"columnType": "skuLocalCode",
"columnId": 99
},
{
"tcv": {
"value": "5.89"
},
"columnType": "rsp",
"columnId": 33
}
]
}
Basically I've an object which has
Aisles [],
taskGroups,
id and name.
Inside the taskGroups as shown in the json, one of the atributes is tasks which is an array, that also have an array called lines which have an array of sku and tasklines.
Basically:
taskGroups -> tasks -> lines -> sku or taskLineFields.
I've tried different queries to get the sku but when I try to get anything further than 'lines' it just came as blank or in some other tries 'cannot call elements from scalar'
Can anyone help me with this issue? Note this is just a sample json.
Anyone knows how make this to work:
I Want all lines where lines->taskLineFields->columnType = 'offer'
All I can do is this, but throwing error on scalar:
SELECT lines->'sku' Produto, lines->'taskLineFields'->'tcv'->>'value' ValorOferta
FROM sd_bel.op_user_event_data,
jsonb_array_elements(data->'taskGroups') taskgroups,
jsonb_array_elements(taskgroups->'tasks') tasks,
jsonb_array_elements(tasks->'columns') columns,
jsonb_array_elements(tasks->'lines') lines
WHERE created_by = 'belteste'
AND lines->'taskLineFields'->>'columnType' = 'offer'
say your data is in some json_column in your table
with t as (
select json_column as xyz from table
),
tg as ( select json_array_elements(xyz->'taskGroups') taskgroups from t),
tsk as (select json_array_elements(taskgroups->'tasks') tasks from tg)
select json_array_elements(tasks->'lines') -> 'sku' as sku from tsk;

Yii2 return JSON format with array inside an array

I am trying to get these data below,
Table relations:
people(one) <---> (many) people_info (one) <---> (many) people_contact
in the following format,
people: {
p_id: 10,
p_price: 3.99,
people_info : [
{
pl_id: 3,
pl_state: 2,
pl_district: 6,
pl_latitude: 6.323434,
pl_longitude: 108.23499,
people_contact: [
{
plc_id: 2
},
{
plc_id: 1
}
]
},
{
pl_id: 2,
pl_state: 7,
pl_district: 12,
pl_latitude: 6.000434,
pl_longitude: 108.9910003,
people_contact: [
{
plc_id: 5
},
{
plc_id: 9
}
]
}
]
}
Currently with these controller codes,
class PeopleController extends Controller
{
public function actionPeople($params){
Yii::$app->response->format = Response::FORMAT_JSON;
....//some other codes//.....
$people= People::find()->select(['p_id', 'p_price'] )->where(['p_id' => $itemId])->one();
$info= PeopleContact::find()->with(['plPeople'])->asArray([])->all();
return array(
'people' => $people,
'info' => $info,
);
}
}
I got these,
"people": {
"p_id": "3",
"p_price": "32.42"
}, "locations": [{
"pl_id": "1",
"pl_people": "3",
"pl_title": "",
"pl_latitude": "6.16438700000000000000",
"pl_longitude": "102.28314649999993000000",
"pl_place": null,
"pl_premise": null,
"pl_street": "1",
"pl_area": "1",
"pl_postcode": "1",
"pl_district": "1",
"pl_state": "3",
"pl_country": 1,
"place": null,
"premise": null,
"street": null,
"area": null,
"postcode": null,
"district": null,
"state": null,
"country": "United Kingdom",
"contacts": [{
"plc_name": "joe",
"plc_phone": "123456",
"plc_email": null
}]
}]
}
How do I achieve it in the format mentioned at the top?
$output;
$people=People::find()->select(['p_id', 'p_price'] )->asArray()->all();
foreach($people as $person) {
$infos = PersonInfo::find()->where(['person_id' => $person->id])->asArray()->all();
foreach($infos as $info) {
$contacts = PersonContact::find()->where(['person_info_id' => $info->id])->asArray()->all();
foreach($contacts as $contact) {
$info['contacts'][] = $contact;
}
$person['info'][] = $info
}
$output['people'][] = $person
}
return $output;
You should loop through and fetch data like this: people > info > contact each next level relying on info fetched from the previous one. Then store it in the format you want such as demonstrated above.
This will output something like:
"people": [{
...
"info": [{
...
"contacts": [{
...
},{
...
}]
}]
},{
...
}]

Resources