Comment in twig expression - twig

I'm making a twig macro that has a multi-line object.
{% set someVar = {
'foo': 'bar',
'foo': 'bar',
'foo': 'bar',
'foo': 'bar',
'foo': 'bar',
'foo': 'bar',
'foo': 'bar',
'foo': 'bar',
'foo': 'bar',
'foo': 'bar',
} %}
Now I want to group the lines and add some comments to not confuse the reader. The below doesn't work. Is there another way?
{% set someVar = {
'foo': 'bar',
{# The below lines do bla-bla #}
'foo': 'bar',
'foo': 'bar',
} %}

Related

Convert a json file to a python dictionary of array

I'm a newbee in Python so excuse if my question looks, dummy.
I have a json file which look like this
[{'_id': '1', 'date': '2019-09-07', 'name': 'abi', 'value': 0, 'unit': '°C'},
{'_id': '2', 'date': '2019-09-08', 'name': 'allo', 'value': 3, 'unit': '°F'},
{'_id': '3', 'date': '2019-09-09', 'name': 'ali', 'value': 0, 'unit': '°C'}]
and I want to read this json file in order to convert it into a dictionary of array which looks like
[{'_id': [ '1', '2','3']},
{'date': [ '2019-09-07', '2019-09-08','2019-09-09']},
{'name': [ 'abi', 'allo','ali']},
{'value': [ '0', '3','0']},
{'unit': [ '°C', '°F','°C']},]
Thank you in advance
Use collections.defaultdict
Ex:
from collections import defaultdict
data = [{'_id': '1', 'date': '2019-09-07', 'name': 'abi', 'value': 0, 'unit': '°C'},
{'_id': '2', 'date': '2019-09-08', 'name': 'allo', 'value': 3, 'unit': '°F'},
{'_id': '3', 'date': '2019-09-09', 'name': 'ali', 'value': 0, 'unit': '°C'}]
result = defaultdict(list)
for i in data:
for k, v in i.items():
result[k].append(v)
print(result)
or .setdefault
Ex:
result = {}
for i in data:
for k, v in i.items():
result.setdefault(k, []).append(v)
print(result)
Output:
{'_id': ['1', '2', '3'],
'date': ['2019-09-07', '2019-09-08', '2019-09-09'],
'name': ['abi', 'allo', 'ali'],
'unit': ['°C', '°F', '°C'],
'value': [0, 3, 0]}

Certain Pages in my Express/EJS app get stuck on "Waiting for localhost..."

I'm building an Node/Express/EJS app, and most pages that display data being passed through my EJS templates get stuck in "Waiting for localhost..."
var express = require("express");
var app = express();
var data = require("./data/info.json");
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"))
//Index Route
app.get("/", function(req, res){
res.render("index");
});
//Profile Route
app.get("/profiles", function(req, res){
res.render("profiles");
});
//Indivual Routes
app.get("/profiles/test", function(req, res){
res.render("template", {data: data})
});
app.get("/idea", function(req, res){
res.render("idea", {data: data})
});
app.listen(3000, function(){
console.log("Server Has Started on Port 3000");
});
Currently the Index and Profile page load fine, but 9/10 times the 'idea' and 'test' page get stuck on the "Waiting for localhost..."
Any advice/suggestions would be very appreciated.
EDIT: Included Data JSON and Idea Template
Data JSON
{ Character:
{ LastChanged: '2018-04-06T10:28:14.857',
NotPersisted: 'false',
FileName: 'Char4.xml',
Name: '',
Key: 'c521cd46-ab33-431a-8b04-a110f59e4968',
Description:
{ CharName: 'Next PC',
PlayerName: 'Next PC',
GenderValue: 'cdgMale',
Age: '37',
Height: '6\'0"',
Build: 'Slim',
Hair: 'Long Black',
Eyes: 'Brown',
Gender: 'Male' },
Characteristics: { CharCharacteristic: [Array] },
Motivations: { CharMotivation: [Object] },
Specializations: { CharSpecialization: [Object] },
ForcePowers: '',
SigAbilities: '',
Skills: { CharSkill: [Array] },
ObOptions: { StartingSize: '10', Plus10XP: 'true' },
Obligations: { CharObligation: [Array] },
DutOptions: { StartingSize: '5' },
Duties: '',
Experience: { ExperienceRanks: [Object], UsedExperience: '100' },
Attributes:
{ SoakValue: [Object],
WoundThreshold: [Object],
StrainThreshold: [Object],
DefenseRanged: '',
DefenseMelee: '',
ForceRating: '' },
Species:
{ SpeciesKey: 'KYUZO',
SubSpeciesKey: '',
SelectedOptions: [Object] },
Career:
{ CareerKey: 'SOLDIER',
StartingSpecKey: 'SHARPSHOOTER',
CareerSkills: [Object],
CareerSpecSkills: [Object] },
Class: { ClassKey: 'PRIVFEW' },
Hook: { HookKey: 'OPP' },
Attitude: { AttitudeKey: 'ANCIENTRELIGION' },
Weapons: { CharWeapon: [Array] },
Armor: { CharArmor: [Object] },
Gear: '',
Vehicles: '',
NPCs: '',
SummaryPriorities: '',
Story: '',
Credits: '500',
Morality: { MoralityValue: '50' },
Grants: { UseGrants: 'false' },
Rigger:
{ VehicleKey: '',
WeaponIndex: '-1',
custRigger: '',
MaxSilhouette: '0' },
Schematics: '',
UseGrants: 'false',
AutoRecalc: 'true',
'_xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'_xmlns:xsd': 'http://www.w3.org/2001/XMLSchema' } }
Idea Template
<% include ./partials/header %>
<% var json = data['Character']['Skills']['CharSkill'] %>
<div class="containter">
<div class="row">
<div class="col-md-4">
<table>
<tbody>
<th>Skill</th>
<th>Rank</th>
<% for(var i = 0; i < json.length; i++ ){ %>
<tr>
<td><%= data['Character']['Skills']['CharSkill'][i]['Key'] %></td>
</tr>
<tr>
<td>
<% var skillstr = JSON.stringify(data['Character']['Skills']['CharSkill'][i]['Rank']); %>
<% var skill = skillstr.replace(/\D/g, ''); %>
<% var skillsum = 0;%>
<% while (skill) { skillsum += skill % 10; sw = Math.floor(skill / 10); } %>
<%= skillsum %>
</td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
</div>
<% include ./partials/footer %>
Edit2: I've been testing it and the problem only arises when I add
<% var skillsum = 0;%>
<% while (skill) { skillsum += skill % 10; sw = Math.floor(skill / 10); } %>
Before I add that everything works as intended.
Try rendering without passing data. If it fails, problem is with data.

how to find the difference between two complex json objects?

I am implementing long polling in nodejs and the google api sends complex json object after every request. How can i quickly find the difference between the resource queried now vs the previous one. This way I can get the latest resource and perform my operations.
[
{ kind: 'y',
etag: 'some etag',
id: '1',
snippet:
{ videoId: 'vid1',
top: [Object],
isPublic: true } },
{ kind: 'y',
etag: 'Some Etag',
id: '2',
snippet:
{ videoId: vid,
top: [Object],
isPublic: true }
}
]
This is a sample response for a data api . As you can see the array has two objects. Now, say i request the api again after 5min .The api return's an array with three objects.For this example, let's say that it contains a new one plus the two mentioned above.Now, How do i extract the new one.
The core of my question is that say the array represents an set having three elements and another that has two. A and B respectively. How do i find A - B.
Update according to the question update.
Assuming you have the following structure of the data:
const oldData = [
{
kind: 'y',
etag: 'some etag',
id: '1',
snippet: {
videoId: 'vid1',
top: [Object],
isPublic: true
}
},
{
kind: 'y',
etag: 'Some Etag',
id: '2',
snippet: {
videoId: vid,
top: [Object],
isPublic: true
}
}
];
And you received some newData, I would go simple filter::
const reallyNewData = newData.filter( item => {
return !oldData.some(el => el.id === item.id);
});
So basically we do it using ES5 array methods filter and some.

Watch node.js object variable in WebStorm debugger

I have a node.js object variable.
var json_sample =
{
'81': { length: '2', data: [ '11', '22' ] },
'82': { length: '1', data: [ 'ab' ] },
'83': { length: '2', data: [ '21', 'ac' ] },
'84': { length: '3', data: [ 'af', 'de', 'ad' ], }
};
When I do console.log(json_sample ), I can see the printed output.
However, when I want to use WebStorm debugger to view the object contents, I cannot see the content values. All I see is some properties of __proto__ which are not useful for my debugging.
Is this a limitation of WebStorm debugger in not being able to view object contents? Is one limited to using console.log() for debugging objects?
Look at the screenshot: it looks for me like this in the WebStorm 11.0.3 as well as WebStorm 2016.1
Could you provide a full code example?
For some reason, Webstorm debugger cannot show the object if the property name is a number.
This will not work on Webstorm.
var json_sample =
{
'81': { length: '2', data: [ '11', '22' ] },
'82': { length: '1', data: [ 'ab' ] },
'83': { length: '2', data: [ '21', 'ac' ] },
'84': { length: '3', data: [ 'af', 'de', 'ad' ], }
};
To show the object on Webstorm debugger, make this change.
var json_sample =
{
'aa': { length: '2', data: [ '11', '22' ] },
'bb': { length: '1', data: [ 'ab' ] },
'cc': { length: '2', data: [ '21', 'ac' ] },
'dd': { length: '3', data: [ 'af', 'de', 'ad' ], }
};

[fromly]: pass controller to template

I'm trying to make form with button inside js, because formly directive rendered form isn't correct. I decided push my button and render that button by template but button has lost access to the controller and validation form. Where i have missed here?
controller.js
.controller('X', ['$log', 'Api', function($log, Api) {
var _self = this;
_self.model = {};
_self.formParts = [{
className: 'row',
fieldGroup: [
{
className: 'col-xs-3',
key: 'q',
type: 'select',
templateOptions: {
required: true,
valueProp: "id",
labelProp: "name",
options: [
{name: '0', id: 0},
{name: '1', id: 1},
],
}
},
{
className: 'col-xs-7',
key: 'n',
type: 'input',
templateOptions: {
required: true,
type: 'text',
placeholder: 'name'
}
}
{
className: 'col-xs-2',
templateUrl: 'button.html'
}
]
}];
_self.save(model) {
$log.info(model);
};
}]);
main.html
<formly-form model="f.model"
fields="f.formFields"
form="formForm">
</formly-form>
button.html
<button type="submit" class="btn btn-success"
ng-disabled="formForm.$invalid"
ng-click="f.save(f.model)">Save
</button>

Resources