Determine the number of items in a list using the yaml-cpp new api - yaml-cpp

Is there a way to determine the number of items in a YAML list, or check if an entry exists using the new yaml-cpp api? For instance, say I have the list
Food:
- pizza: 270
- ice_cream: 90
- fruit: 30
How can I determine the number of foods there are? Also, is there a way to check if a food is present or not in the YAML string? I know I could try to index to the food like root_node["Foods"]["fruit"].as<int>() and catch an exception if fruit doesn't exist, but is there a function similar to FindValue() in the old api (http://code.google.com/p/yaml-cpp/wiki/HowToParseADocument) to check if an entry exists?

To get the number of foods, use
root_node["Food"].size();
To check if a food is present or not is a little trickier in your example, because it's a sequence of maps, each with a single key/value pair. (This is often used to create an ordered map.) You'd just have to loop through each entry and check if it's what you want:
bool does_food_exist(const std::string& name) {
for(std::size_t i=0;i<root_node["Food"].size();i++) {
if(root_node["Food"][i][name])
return true;
}
return false;
}
If you meant, instead, to actually have an actual map, then your YAML file should look like:
Food:
pizza: 270
ice_cream: 90
fruit: 30
In this case, checking if a food exists is easy:
if(root_node["Food"]["fruit"]) {
// "fruit" exists
} else {
// it doesn't exist
}

Related

Merging data from different graphql resolvers in vue.js client side for simple outputting

I do query cars from an api with a single query but two resolvers (listing and listings)(hopefully resolver is the right name for it). One car I get by the id via listing and the other cars I get without filters by listings. The resolvers output the data i a little different structure on the server-side but I do get the same fields just at different „places“. I want to merge the structure in order to get a single array I can simply loop over in vue.js. For the apicalls I do use vue-apollo.
Couldn't find any information to merge data client-side inside graphqlqueries. All I found is about handling it serverside with resolvers but it's an api I do not own.
Is it possible with graphql or do I have to merge it inside my vuecomponent and if so what would be the best way to do so?
The output will be a grid of cars where I show the car of the week (requested by id) together with the newest cars of the regarding cardealer.
Full screenshot including response: https://i.imgur.com/gkCZczY.png
Stripped down example with just the id to show the problem:
query CarTeaser ($guid: String! $withVehicleDetails: Boolean!) {
search {
listing(guid: $guid){
details{
identifier{
id #for example: here I get the id under details->identifier
}
}
}
listings( metadata: { size: 2 sort:{ field: Age order: Asc}}) {
listings{
id #here it's right under listings
details{
…
}
}
}
}
}
}
Ideally you're right, it should be handled server-side, but if it's not your API the only solution is to manipulate the data on the client side, meaning in your component.
It's probably a lot simpler to leave the listings array untouched and to just merge the listing element with it, like this for instance:
// assuming 'search' holds the entire data queried from the api
const fullListing = [
// car of the week, data reformatted to have an identical structure as
// the 'other' cars
{
id: search.listing.details.identifier.id,
details: {
vehicle: search.listing.details.vehicle,
},
},
...search.listings.listings, // the 'other' cars
]

How to insert an item into a sequence using Sequelize, or How to manage an ordering attribute

I have an entity with a sequence attribute, which is an integer from 1-N for N members of the list. They are polyline points.
I want to be able to insert into the list at a given sequence point, and increment all the items at that point or beyond in the sequence to make room for the new item, and likewise if I delete then decrement everything above so we still have nice sequence ordering with no missing numbers.
There is a REST interface in this of course, but I dont want to hack about with that, I just want sequelize to magically manage this sequence number.
I am assuming I need to get hold of some "before insert" and "after delete" hooks in sequelize and issue some SQL to make this happen. Is that assumption correct or is there some cooler way of doing it.
I havent tested this, but this appears to be the solution, which is barely worth comment.
I know the modelName, and name==the attribute name,
options.hooks={
beforeInsert: function(record, options) {
return self.models[modelName].incrementAfter(name,record[name]);
},
afterDelete: function(record, options) {
return self.models[modelName].decrementAfter(name,record[name]);
}
}
and then added to my extended model prototype I have
incrementAfter:function(field,position){
return this.sequelize.query("UPDATE "+this.tableName+" SET "+field+" = "+field+"+1 WHERE "+field +" >= "+position);
},
decrementAfter:function(field,position){
return this.sequelize.query("UPDATE "+this.tableName+" SET "+field+" = "+field+"-1 WHERE "+field +" >= "+position);
},

How to find Trello ListId's or CardId's with Trello.NET

I'm trying to find all cards in a specific list using Trello.NET
In the examples given it's clear to to find all list names in a Board, and all Cards in a Board. However if I want to find all Cards in a specific List, as far as I can see I need ListID (can't filter by List Name) by using trello.Lists.WithId()
Knowing the List name, how can I determine what it's ListId is so I can subsequently filter for all Cards in it?
And if I would subsequently like to edit an existing Card, how can I determine the CardId.
Use LINQ and filter in-memory:
// More than one list can have the exact same name, this finds the first:
var list = trello.Lists
.ForBoard(board)
.FirstOrDefault(list => list.Name == "name of the list");
if (list != null)
{
var cards = trello.Cards.ForList(list);
// ...
}
You could use a similar technique to find a card if you know it's name: use trello.Cards.ForBoard or trello.Cards.ForList first, and then filter by name afterwards.
For cards there is another option though: trello.Cards.Search. For example:
var cards = trello.Cards.Search("name of the card");
The search will be performed on the server in this case.

Getting windows-8 semantic zoom out to include empty items

I understand providing the data template to the ItemTemplate selector, but the items that I want to be displayed are not part of the data. For instance let's say you have the following list of children in the classroom:
Mark Anderson
Sara Buckingham
Dave Christy
Jeni Thompson.
and are using the first letter in the last name to organize the data. If I pass dataItems that represent the students (FirstName, LastName) to the groupedItemList
'groupedItemList = itemList.createGrouped(
function getGroupKey(dataItem) {
return dataItem.LastName.toUpperCase().charAt(0);
},
function getGroupData(dataItem) {
return {
Name: dataItem.LastName.toUpperCase().charAt(0)
};
},
function compareGroups(left, right) {
return left.toUpperCase().charCodeAt(0) - right.toUpperCase().charCodeAt(0);
}
),`
The semantic zoom out contains only the following letters
A, B, C, T.
I would like a list of all the letters in the alphabet and then style them so that those letters that have no items get another visual treatment.
After spent some time trying to find the solution to the same problem, I have have found a solution and posted it on gist.
https://gist.github.com/pimpreneil/4714483
Hope it is clear enough.

User-friendly URLs

I currently have the following setup for my paging model: /pages/6-about
class Page < ActiveRecord::Base
def to_param
"#{id}-#{permalink}"
end
end
I would like the URL to be /pages/about. What is the best solution? Thanks in advance.
So you change your
def to_param
permalink
end
And then implement a
def self.from_param(value)
find_by_permalink(value)
end
in your page model.
In your controllers you would then need to
def show
#page = Page.from_param(params[:id])
...
end
There are not much informations on your question.
There are several routes to doing that but it all depends on what you are doing.
If the number of pages is fixed you can just keep a static hash table that keeps track of name <-> id.
private static readonly Dictionary<string, int> PagesDict = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) { { "about", 6 }, { "home", 1 }, { "xxx", 2 } };
///<summary>Gets a page id by a string name. Returns -1 if page doesn't exists.</summary>
public static int GetPageId(string name)
{
int result;
if (!PagesDict.TeyGetValue(name, out result))
return -1;
return result;
}
If you need a read-write dictionary instead, you can use a ConcurrentDictionary, but of course, this dictionary is not stored anywhere and will be lost if the dictionary is not loaded from something like a DB table or an XML.
If your pages are dynamic and loaded from a DB you need a table on the DB that gives you an id by a name. You can index the table by name then, and the query should be quite fast.
If you want to reduce the number of access on DB you can implement a (quite complicated) cache system using concurrent dictionary.
Edit-
The amount of information required to restore the ID must be greater or equal the amount of information the ID field can store :)
I mean...
If permalink is not unique, there is no way you can obtain an id from it.
If permalink field is unique it is the same thing as an ID, it is a string id.
Then you can write a query that given a permalink will return you an id.
If you can restore an ID from a unique couple of fields, for example, permalink and fieldX, you can write a query where permalink='about' AND fieldX='XXX'