nested

Re-opened nested module anomaly in Ruby

走远了吗. 提交于 2019-12-03 08:23:18
Why does re-opening a nested module give different results depending on the syntax used? For example, this works fine: module A module E end end module A module E def E.e end end end But this: module A module E end end module A::E def E.e end end gives the error: reopen.rb:6:in `<module:E>': uninitialized constant A::E::E (NameError) from reopen.rb:5:in `<main>' (Before someone points this out, a workaround is to use self instead of the module name when defining E.e, but that's not really the point of this post.) The module keyword sets a namespace context that is checked for references to

Turning nested JSON into an HTML nested list with Javascript

前提是你 提交于 2019-12-03 08:18:45
I'm fairly new to using JSON (as opposed to XML) and am currently working purely with Javascript to digest, parse and display my returned JSON data. I'm using the JSON2.js library and am getting back some valid JSON data representing a fairly simple nested list: { "node":{ "class":"folder", "title":"Test Framework", "node":{ "class":"folder", "title":"Item 1", "node":{ "class":"folder", "title":"Item 1.1", "node":{ "class":"file", "title":"Item 1.1.a" } }, "node":{ "class":"folder", "title":"Item 1.2", "node":{ "class":"file", "title":"Item 1.2.a" }, "node":{ "class":"file", "title":"Item 1.2

Creating a nested dictionary from a flattened dictionary

旧城冷巷雨未停 提交于 2019-12-03 08:06:11
问题 I have a flattened dictionary which I want to make into a nested one, of the form flat = {'X_a_one': 10, 'X_a_two': 20, 'X_b_one': 10, 'X_b_two': 20, 'Y_a_one': 10, 'Y_a_two': 20, 'Y_b_one': 10, 'Y_b_two': 20} I want to convert it to the form nested = {'X': {'a': {'one': 10, 'two': 20}, 'b': {'one': 10, 'two': 20}}, 'Y': {'a': {'one': 10, 'two': 20}, 'b': {'one': 10, 'two': 20}}} The structure of the flat dictionary is such that there should not be any problems with ambiguities. I want it to

How to reduce the number of if-else statements in PHP?

安稳与你 提交于 2019-12-03 07:24:34
I found that there are many if-else statements, especially nested if else statements, these statements make my code less readable. How to reduce the number of if else statements in PHP? My tips are as follows: 1.Use a switch statement when it is suitable; 2.use exit() statement when it is feasible; 3. Use ternary statement when it is feasible; Are there other tips that can reduce if else statements, especially nested if-else statements? Refactor your code into smaller work units. Too much conditional logic is a code-smell and usually indicates that your function needs to be refactored. Try to

How to “nest” the inclusion of modules when using the Ruby on Rails ActiveSupport::Concern feature?

怎甘沉沦 提交于 2019-12-03 07:18:43
I am using Ruby 1.9.2 and the Ruby on Rails v3.2.2 gem. I would like to "nest" the inclusion of modules given I am using the RoR ActiveSupport::Concern feature, but I have a doubt where I should state the include method. That is, I have the following: module MyModuleA extend ActiveSupport::Concern # include MyModuleB included do # include MyModuleB end end Should I state include MyModuleB in the "body" / "context" / "scope" of MyModuleA or I should state that in the included do ... end block? What is the difference and what I should expect from that? If you include MyModuleB in the "body" of

Python recursively replace character in keys of nested dictionary?

时光毁灭记忆、已成空白 提交于 2019-12-03 06:46:09
问题 I'm trying to create a generic function that replaces dots in keys of a nested dictionary. I have a non-generic function that goes 3 levels deep, but there must be a way to do this generic. Any help is appreciated! My code so far: output = {'key1': {'key2': 'value2', 'key3': {'key4 with a .': 'value4', 'key5 with a .': 'value5'}}} def print_dict(d): new = {} for key,value in d.items(): new[key.replace(".", "-")] = {} if isinstance(value, dict): for key2, value2 in value.items(): new[key][key2

ElasticSearch: access document nested value in groovy script

╄→尐↘猪︶ㄣ 提交于 2019-12-03 06:37:47
I have a document stored in ElasticSearch as below. _source: { "firstname": "John", "lastname": "Smith", "medals":[ { "bucket": 100, "count": 1 }, { "bucket": 150, "count": 2 } ] } I can access the string type value inside a document using doc.firstname for scripted metric aggregation http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html . But I am not able to get the field value using doc.medals[0].bucket. Can you please help me out and let me know how to access the values inside nested fields? Use _source for nested

CASE statement in SQLite query

陌路散爱 提交于 2019-12-03 06:28:39
问题 Why this query doesn't work? :( I tried to replace nested IF statement "...SET lkey = IF(lkey >= 11, lkey - 5, IF(lkey > 5, lkey + 2,lkey))" UPDATE pages SET lkey = CASE lkey WHEN lkey >= 11 THEN lkey - 5 ELSE CASE lkey WHEN lkey > 5 THEN lkey + 2 ELSE lkey END END, rkey = CASE lkey WHEN lkey >= 11 THEN rkey - 5 ELSE CASE rkey WHEN rkey < 11 THEN rkey + 2 ELSE rkey END END WHERE rkey > 5 AND lkey < 12; 回答1: The syntax is wrong in this clause (and similar ones) CASE lkey WHEN lkey > 5 THEN

Update nested map dynamodb

走远了吗. 提交于 2019-12-03 06:19:07
I have a dynamodb table with an attribute containing a nested map and I would like to update a specific inventory item that is filtered via a filter expression that results in a single item from this map. How to write an update expression to update the location to "in place three" of he item with name=opel,tags include "x1" (and possibly also f3)? This should just update the first list elements location attribute. ( "inventory": [ { "location": "in place one", # I want to update this "name": "opel", "tags": [ "x1", "f3" ] }, { "location": "in place two", "name": "abc", "tags": [ "a3", "f5" ] }

Handlebars nested 'each' syntax - not iterating over each element

為{幸葍}努か 提交于 2019-12-03 05:59:00
问题 I am brand new at this Javascript/JSON/Handlebars thing, and I am having trouble getting a JSON object, with two nested levels, to work in a Handlebars template. I have validated the JSON object with JSONLint, so it is valid JSON code, but I don't know if I have the correct JSON format to make the template work correctly. :) (I am building the JSON manually in another system.) Or perhaps it is the syntax of the template that I have incorrect. That's what I hope to find out... The short