Can't iterate over children's children because of the subsequent .clear()?
By : karan
Date : March 29 2020, 07:55 AM
should help you out The problem is that iterparse by default yields end events. For subelements end events are generated earlier than for their ancestors: code :
>>> for ev, elem in etree.iterparse(xml):
print elem
<Element b at 0x38fe320>
<Element d at 0x38fe0f0>
<Element c at 0x38fe2d0>
<Element a at 0x38fe190>
<Element z at 0x38fe230>
<Element a at 0x38fe3c0>
<Element root at 0x2df48c0>
for ev, elem in etree.iterparse(xml, events=('start',)):
...
found = False
for event, elem in etree.iterparse(source, events=('start', 'end')):
if event == 'start':
if elem.attrib.get('id') == elem_id:
found = True
else:
if elem.attrib.get('id') == elem_id:
return _get_info(elem)
if not found:
elem.clear()
|
JSON.parse returns children objects with null value, children values not being parsed
By : hathania
Date : March 29 2020, 07:55 AM
hop of those help? I have a JavaScript object that I am stringifying with JSON.stringify that returns a JSON string with parent and children data. , So I eventually solved my problem and this is how I did it. code :
function cacheForm(agency) {
// GET my object from agency vm
var agency = ko.toJS(agency);
var s = YUI().use("json-stringify", function (Y) {
var jsonStrAgency = Y.JSON.stringify(agency, ["activities", "agencyName", "agencyID", "campaignBillings", "category", "declaredBillings", "immediateParent", "numberOfEmployees", "ultimateParent", "uRL"]); // Use an array of acceptable object key names as a whitelist.
var jsonStrOffices, jsonStrContacts;
for (i in agency.offices) {
jsonStrOffices = Y.JSON.stringify(agency.offices, ["address1", "address2", "address3", "address4", "address5", "agencyID", "faxNumber", "officeID", "postCode", "telephoneNumber"]);
for (ii in agency.offices[i].contacts) {
jsonStrContacts = Y.JSON.stringify(agency.offices[i].contacts, ["agencyID", "emailAddress", "firstName", "jobName", "officeID", "personID", "surName", "title"]);
}
}
localStorage.setItem('Agency', jsonStrAgency);
localStorage.setItem('Offices', jsonStrOffices);
localStorage.setItem('Contacts', jsonStrContacts);
});
}
var s = YUI().use("json-stringify", function (Y) {
<script src="http://yui.yahooapis.com/3.11.0/build/yui/yui-min.js"></script>
define(['services/datacontext'], function (dataContext) {
var initialized = false;
var agency;
if (localStorage.Agency && localStorage.Offices && localStorage.Contacts) {
var objAgency = new Object(ko.mapping.fromJSON(localStorage.getItem('Agency')));
var objOffices = new Object(ko.mapping.fromJSON(localStorage.getItem('Offices')));
var objContacts = new Object(ko.mapping.fromJSON(localStorage.getItem('Contacts')));
objAgency.offices = objOffices;
objAgency.offices._latestValue[0].contacts = objContacts;
agency = ko.observableArray([ko.mapping.fromJS(objAgency)]);
ko.applyBindings(agency);
initialized = true;
}
else {
agency = ko.observableArray([]);
}
define(['services/datacontext'], function (dataContext) {
var initialized = false;
var agency;
if (localStorage.Agency && localStorage.Offices && localStorage.Contacts) {
var objAgency = new Object(ko.mapping.fromJSON(localStorage.getItem('Agency')));
var objOffices = new Object(ko.mapping.fromJSON(localStorage.getItem('Offices')));
var objContacts = new Object(ko.mapping.fromJSON(localStorage.getItem('Contacts')));
objAgency.offices = objOffices;
objAgency.offices._latestValue[0].contacts = objContacts;
agency = ko.observableArray([ko.mapping.fromJS(objAgency)]);
ko.applyBindings(agency);
initialized = true;
}
else {
agency = ko.observableArray([]);
}
var save = function (agency, myStoredValue) {
// Clear Cache because user submitted the form. We don't have to hold onto data anymore.
//amplify.store("Agency", null);
return dataContext.saveChanges(agency);
};
var vm = { // This is my view model, my functions are bound to it.
//These are wired up to my agency view
activate: activate,
agency: agency,
title: 'agency',
refresh: refresh, // call refresh function which calls get Agencies
save: save,
cacheForm: cacheForm
};
return vm;
function activate() {
vm.agency;
if (initialized) {
return;
}
initialized = false;
return refresh();
}
function refresh() {
return dataContext.getAgency(agency);
}
function cacheForm(agency) {
// GET my object from agency vm
var agency = ko.toJS(agency);
var s = YUI().use("json-stringify", function (Y) {
var jsonStrAgency = Y.JSON.stringify(agency, ["activities", "agencyName", "agencyID", "campaignBillings", "category", "declaredBillings", "immediateParent", "numberOfEmployees", "ultimateParent", "uRL"]); // Use an array of acceptable object key names as a whitelist.
var jsonStrOffices, jsonStrContacts;
for (i in agency.offices) {
jsonStrOffices = Y.JSON.stringify(agency.offices, ["address1", "address2", "address3", "address4", "address5", "agencyID", "faxNumber", "officeID", "postCode", "telephoneNumber"]);
for (ii in agency.offices[i].contacts) {
jsonStrContacts = Y.JSON.stringify(agency.offices[i].contacts, ["agencyID", "emailAddress", "firstName", "jobName", "officeID", "personID", "surName", "title"]);
}
}
localStorage.setItem('Agency', jsonStrAgency);
localStorage.setItem('Offices', jsonStrOffices);
localStorage.setItem('Contacts', jsonStrContacts);
});
}
});
|
Safely clear (or remove children) of a DivElement
By : Sanj
Date : March 29 2020, 07:55 AM
Any of those help Think about what happens in this loop. Lets take a div with three children: int count = rows.getChildCount(); code :
Node child = rows.getChild(0);
rows.removeChild(child);
Node child = rows.getChild(1);
rows.removeChild(child);
Node child = rows.getChild(2); // Exception!
while (rows.hasChildNode()) {
rows.removeChild(rows.getFirstChild());
}
for (int i = count; i >= 0; i--) {
rows.removeChild(rows.getChild(i));
}
rows.setInnerHTML("");
rows.removeAllChildren();
|
Rails & Active Record: Find objects whose children don't belong to current_user OR who don't have any children
By : Sagar Vipul
Date : March 29 2020, 07:55 AM
Does that help User.rb , Hacky but fun answer code :
class Event
has_many :votes
has_many :user_votes,
->() { where({user_id: Thread.current[:user]&.id}.compact) },
class_name: 'Vote'
def self.using_user(user)
old_user, Thread.current[:user] = Thread.current[:user], user
yeild
ensure
Thread.current[:user] = old_user
end
end
Event.using_user(current_user) do
Event.includes(:user_votes).where("votes.id IS NULL")
end
Event.
joins(
"LEFT OUTER JOIN votes ON votes.event_id = events.id AND " +
"votes.user_id = #{user.id}"
).
where("votes.id IS NULL")
|
How to filter through children of array of objects and then return parent with updated children?
By : user3316519
Date : March 29 2020, 07:55 AM
wish helps you You can use map() and filter() to do that. code :
let arr = [ { "id": 1, "name": "group1", "users": [ { "id": 1, "name": "Mike" }, { "id": 2, "name": "Steve" }, { "id": 3, "name": "John" } ] }, { "id": 2, "name": "group2", "users": [ { "id": 4, "name": "Phill" }, { "id": 5, "name": "Joe" }, { "id": 6, "name": "Dominik" } ] } ];
function filterUsers(arr, name) {
return arr.map(obj => {
return {
...obj,
"users": obj.users.filter(user => user.name === name)
};
});
}
console.log(filterUsers(arr, "Mike"));
|