R bootstrap boot sampling leads to some predictors having only one value

Question: The context is not very important but still, here it is: i am using the boot package to calculate bootstrap confidence interval for different statistics (coefficients of doubly robust method estimates, C-index, etc…) and I happen to face the same problem each time.
If I understood it correctly, the issue is that, no matter which statistic, I need to fit some glm model on each sample and at some point it throws a contrast error because some of the factors predictors in my model have only one level on this sample.
I can think of 3 solutions but either I don't know how to code it easily or I don't like it :

I can include some test in my statistic function to exclude a predictor with less than one value but this means that i'm not fitting the same model on each sample, which I don't like…

I could just drop the samples where it happens (and I can increase the number of replicates - R parameter of boot function - to compensate the loss of replicates) but I can't do it easily with boot function since the error breaks the run. I could use some try or tryCatch code inside my statistic function but I feel like there must be an easier way

I could use the strata parameter of boot function: at the moment, I only stratified the sampling on my dependant variable y="event occured" but I could use a new strata variable that would consist of crossing all categorical predictors so as to have one strata for each combination of factors' levels. However, this may result in a very large number of stratas...

I hope someone might have some insights on this issue because I can't find anything online.
Any idea, advice or comment will be much appreciated.
It is not trivial but if needed I'll try to build some reproducible example.
Comment: To clarify, do you mean that one of your predictors is a categorical variable, and that variable has some level where only 1 individual has that level? Eg, for a City variable you have lots of City A and City B, but only 1 City C?
Comment: It is not that extreme but yes, proportions amongst levels can be quite unbalanced through the different categorical predictors. EDIT: eg, 14 'yes' vs 467 'no' for one predictor
Comment: "I need to fit some glm model on each sample" Maybe, bootstrap the residuals? (Also, using `tryCatch` is very easy but of course you can't really rely on the bootstrap result if too many resamples don't give a successful fit.)
Comment: I am probably not familiar enough with residual resampling but I still have to fit my model on each sample at some point, no ?
Comment: Yes, but it preserves the predictors. Only the values of the dependent variable are different between the resamples. Here is the algorithm: https://stats.stackexchange.com/a/73985/11849
Comment: ok I think I got it, you fit the model indeed on each sample but you have the same individuals so no problem of dropping factor levels, predictors remain the same only the response Y get changed by the residual resampling, am I correct ? EDIT: oops, crossed comments
Comment: After some reading, I have a new question : how can I generalize the residual resampling approach to GLM model ? Or at least to logistic regression since my outcome is binary ?
Comment: https://stats.stackexchange.com/a/448311/11849
Comment: yes thanks, i saw that one, and also the topic linked in one of the comment, but I don't know how am i supposed to generate my new $y_i$. Should I just do for each observation $i$ a Bernouilli `rbinom` with probability $p=\frac{1}{1+e^{\sum{\beta_ix_i}}}$ ? i opened a new topic to try to sort my problem out [here](https://stats.stackexchange.com/questions/584098/confidence-interval-for-c-index)
Comment: THANK YOU @Roland for your help. I edited the quesiton with the solution I eventually came up with
Comment: Please move that from the question to an answer so people can vote on it. Self-answering is encouraged here.
Comment: What are the frequencies of observed Y and what is the dimensionality of candidate predictors?  Need to make sure the data have sufficient information to do any of this.
Comment: I have around 7% of event occurrences in Y. There are 6 to 8 covariates in the different models (factor predictors have only 2 levels except one that has 3). Overall, I have around 1000 observations but there are between 250 and 450 observations according to the subsample of interest. That seemed ok to me but without any mathematical proof...
Answer: Following @Roland's advice and after some research about how to generalize residuals resampling to GLM models, here is what i did in the end.
# removing na
data_tmp <- df %>%
  filter(if_all(everything(), ~ !is.na(.)))

# fit the GLM model
mod1 <- glm(y ~ .,
            data = data_tmp %>% select(-ind1),
            family = "binomial")

mod2 <- glm(y ~ .,
            data = data_tmp,
            family = "binomial")

# Bootstrapping C-index
cind_boot_stat <- function(data, indices){
  # generate new y sample with Bernouilli each time
  d <- data %>% mutate(
    y1 = rbinom(nrow(data_tmp), 1, mod1$fitted.values),
y2 = rbinom(nrow(data_tmp), 1, mod2$fitted.values))

  # fitting the logistic and calculating C-index
  c1 <- DescTools::Cstat(glm(y1 ~ .,
                             data = d %>% select(-y, -ind1),
                             family = "binomial"))

  c2 <- DescTools::Cstat(glm(y2 ~ .,
                             data = d %>% select(-y),
                             family = "binomial"))

  return(c(c1, c2))
}

cind_boot <- boot::boot(data_tmp, cind_boot_stat, R = 1000)

Hopefully this is statistically right but at least it seems to work !
Answer: If I understand the question correctly, I've dealt with this issue before using boot.ci to create bootstrapped confidence intervals.
I've used code like the following.  As an example, here, the code returns the r-squared value for a linear model, labeled STAT.
library(boot)

Function = function(input, index){
                    Input = input[index,]

### CAN INSERT TEST OF APPROPRIATENESS HERE

                    Result = lm (Words.per.minute ~ Age + Age2,
                                          data = Input)
                    Stat = summary(Result)$r.squared
                    return(Stat)}

Boot = boot(Data,
             Function,
             R=5000)

mean(Boot$t[,1])

Reference: here, with the caveat that I wrote it: rcompanion.org/handbook/G_10.html
At the point where it says ### CAN INSERT TEST OF APPROPRIATENESS HERE you can check to be sure that your predictor has at least two levels.  This would be in the data frame called Input.
If it doesn't, there are a couple of options. First is to exclude that iteration, and go on to to calculate the confidence interval.  Second is to return an NA or something for the confidence interval as a whole.
In either case, if you find that your predictor has only one level (or fewer levels than the original data frame), you would set FLAG=1 and then make the next step if(FLAG==0){ so that it doesn't fit the model in the next step if FLAG equals 1.
Hopefully that makes sense, and hopefully I'm actually answering (that part of) the question.
Comment: Thanks for your answer ! It matches my solution 2. I coded something similar with a `try` inside my bootstrap stat function. However, the problem remained because at some point, most of the samples in the bootstrap iteration had a predictor with only one level (due to unbalanced levels in the original sample and to a too small population). The bootstrap results then were a bit unreliable to me. This is why I liked the residuals resampling solution since the population (and hence the levels of different predictors) stays the same, only the response gets changed


For Loop Enumerate vs dict on enumerate

Question: Let me preface this question by stating that I am a novice both in Python and programming in general. That said, I was wondering whether there is any difference between the following two ways of creating a dictionary based on an existing list:
index = {}
seq = ['a', 'b', 'c']
for a,b in enumerate(seq):
    index[a]= b
print(index)
print(dict(enumerate(seq)))

Output:
>>> print(index)
{0: 'a', 1: 'b', 2: 'c'}
>>> print(dict(enumerate(seq)))
{0: 'a', 1: 'b', 2: 'c'}

Both "print (index)" and "print (dict (enumerate (seq)" yield an equivalent result and I was wondering which one is better to perform in this context. I stumbled upon the latter by just practicing some coding and it made me question why one would need to perform a for loop if a one-line code such as the dict() gives the same result.
Comment: Please indent your code in the question the same as the code that you are running.
Comment: The `print` isn't really relevant. You could write `index = dict(enumerate(seq))` as a more concise alternative to a full `for` loop.
Comment: As you wrote yourself, there is no reason to write a loop.
Comment: This turned out to be an interesting question. Points from me :-)
Answer: I've seen a lot of one line loops. It's just used as maybe a more compiled way of defining something but a for loop is just as good in most situations.
Answer: There is no semantic difference. This
dict(enumerate(seq))

will execute faster.
Update: well, it seems that faster might not be as clear cut as I expected.. (or correct, except maybe for 3.9b1 - see @Bharel's answer).
Update2: I've added measurements for the dict comprehension solution from @ShadowRanger for completeness.
Py2.7:
(dev) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "dict(enumerate(seq))"
1000000 loops, best of 3: 1.06 usec per loop

(dev) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "index = {}" "for a,b in enumerate(seq):" "  index[a]= b"
1000000 loops, best of 3: 0.686 usec per loop

(dev) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "{a: b for a,b in enumerate(seq)}"
1000000 loops, best of 3: 0.807 usec per loop

Py3.5:
(dev35) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "dict(enumerate(seq))"
1000000 loops, best of 3: 1.19 usec per loop

(dev35) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "index = {}" "for a,b in enumerate(seq):" "  index[a]= b"
1000000 loops, best of 3: 0.813 usec per loop

(dev35) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "{a: b for a,b in enumerate(seq)}"
1000000 loops, best of 3: 0.917 usec per loop

Py3.8:
(dev38) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "dict(enumerate(seq))"
500000 loops, best of 5: 755 nsec per loop

(dev38) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "index = {}" "for a,b in enumerate(seq):" "  index[a]= b"
500000 loops, best of 5: 703 nsec per loop

(dev38) c:\srv> python -m timeit -s "seq = ['a', 'b', 'c']" "{a: b for a,b in enumerate(seq)}"
500000 loops, best of 5: 879 nsec per loop

Update 3: @Bharel and @ShadowRanger has pointed out, the length of the list is important. With lists of ~10 items, the dict(enumerate(..)) version becomes more performant.
code:
call workon dev
python -m timeit -s "seq = 'abcdefghij'" "dict(enumerate(seq))"
python -m timeit -s "seq = 'abcdefghij'" "index = {}" "for a,b in enumerate(seq):" "  index[a]= b"

call workon dev35
python -m timeit -s "seq = 'abcdefghij'" "dict(enumerate(seq))"
python -m timeit -s "seq = 'abcdefghij'" "index = {}" "for a,b in enumerate(seq):" "  index[a]= b"

call workon dev38
python -m timeit -s "seq = 'abcdefghij'" "dict(enumerate(seq))"
python -m timeit -s "seq = 'abcdefghij'" "index = {}" "for a,b in enumerate(seq):" "  index[a]= b"

output:
100000 loops, best of 3: 1.94 usec per loop
1000000 loops, best of 3: 1.9 usec per loop

1000000 loops, best of 3: 1.7 usec per loop
1000000 loops, best of 3: 1.6 usec per loop

200000 loops, best of 5: 1.29 usec per loop
200000 loops, best of 5: 1.66 usec per loop

Comment: mate, you're creating the dict at setup. You're using the same dict again and again on your first test, while creating a dict each time at the second. These tests aren't equal.
Comment: @Bharel ah, thanks. Running your code, I still get that the for-loop is faster for 2.7, 3.5, 3.8.
Comment: @thebjorn: Update the tests for a moderate number of items; for 50ish items, `dict(enumerate(seq))` should win (not by enough to matter, but still by a meaningful margin). With only three, the fixed overhead is swamping the actual work.
Comment: @ShadowRanger I'm sure it's dependent on the machine, but on mine it only takes about 10'ish elements...
Comment: @thebjorn: I didn't actually time it this time, I've just done this before and gave a number large enough to definitely show the effect. 10 does not surprise me. :-)
Answer: There's no difference at all in the output.
Regarding timing, you may use the timeit module to test for speed.
The second option, of one lining will necessarily be faster, and is also more readable in my opinion.
Timing results, Python 3.9:
python -m timeit -s "seq = ['a', 'b', 'c']" "index = {}" "for a,b in enumerate(seq):" "  index[a]= b"
500000 loops, best of 5: 803 nsec per loop

python -m timeit -s "seq = ['a', 'b', 'c']" "dict(enumerate(seq))"
500000 loops, best of 5: 786 nsec per loop

You might not see the difference in here, because the dict() operation for dict creation is longer than {}, and it's a fixed "setup cost".
If you'll increase the numbers to offset this "setup cost", you'll see the huge difference:
python -m timeit -s "seq = ['a', 'b', 'c']*1000" "dict(enumerate(seq))"
500 loops, best of 5: 395 usec per loop

python -m timeit -s "seq = ['a', 'b', 'c']*1000" "index = {}" "for a,b in enumerate(seq):" "  index[a]= b"
500 loops, best of 5: 509 usec per loop

Comment: The gap is small because the data is tiny. It's not really a relevant profile, things like function call overhead begin to dominate.
Comment: which python version?
Comment: @thebjorn 3.9b1. For some reason it feels like the gap is smaller than I expected. Doesn't it? I haven't compiled this one. It's a pure build from pyorg
Comment: on 3.5 I'm getting that the for-loop is faster... I'm double checking my results now...
Comment: @thebjorn remember that the dict is not part of the setup. Oh and `dict()` does require a LOAD_GLOBAL & func call instead of pure opcode. Maybe that's the reason for the smaller diff? I bet it'll be much larger at larger numbers.
Comment: The setup isn't measured, so that should be inconsequential.
Comment: Fist option is faster for me on python 3.7.3. The first option, 1000000 loops, best of 5: 301 nsec per loop. The second option, 1000000 loops, best of 5: 321 nsec per loop
Comment: Again guys, dict creation is not part of the setup. Don't do `index = {}` at setup.
Besides, if you'll increase the timing, the difference will rise.
Comment: I'm not doing dict creation, still get that the for loop is faster on 2.7, 3.5, 3.8.
Comment: @juanpa.arrivillaga they're doing a dict creation at setup.
Answer: dict(enumerate(seq)) is strictly better on performance and simplicity, it's just less flexible. For more flexibility, you'd go with dict comprehensions typically, e.g.:
index = {i: x * 2 for i, x in enumerate(seq)}  # Repeats each value

only going to your original for loop when the conversion code is too complicated for a comprehension.
Comment: well.. surprisingly, it doesn't seem to be faster...
Comment: @thebjorn: It's slightly faster for larger inputs. For small inputs, the minor fixed overhead of going through generic function dispatch to invoke the `dict` constructor can make it ever-so-slighty slower.

For Loop Enumerate vs dict on enumerate


LimitToLast() freezes unity

Question: I'm usig Unity 2018.3.0f2
Firebase 6.3
The problem is without using limitToLast() the code below works fine. I can get all data which Is thousands of data.
But I need to limit it and when I put 1 or 10 or any number with limittoLast or LimitToFirst (10) unity give no response and after waiting or stop to run the Unit play cpu starts to run faster, unity Freezes and I have to stop unity from Task manager and restart. Is that a bug or am I doing something wrong?
 FirebaseDatabase.DefaultInstance
          .GetReference("users").OrderByChild("score").LimitToLast(100)
          .GetValueAsync().ContinueWith(task =>{
    if (task.IsFaulted)
              {
                 Debug.LogError("failed");
              }
    else if (task.IsCompleted)
              { Debug("success")} );

Answer: You are calling GetValueAsync() which will override your LimitToLast() clause and grab all the data in the "users" reference.
This code should get you closer to what you want:
DatabaseReference usersReference = FirebaseDatabase.DefaultInstance.GetReference("users");
Query query = usersReference.OrderByChild("score").LimitToLast(100);
query.ValueChanged += (object sender, ValueChangedEventArgs args) =>
{
    // this lambda will get called up to 100 times
    if (args.DatabaseError != null)
    {
        Debug.LogError(args.DatabaseError.Message);
        return;
    }
    Debug.Log(args.Snapshot.GetRawJsonValue());
};

LimitToLast() freezes unity


How Do I Write Multiple Shapefiles to ASCII in R

Question: I have the attached code which works for converting a singe polygon into the ASCII format, however I have 27000 polygons so I need to be able to loop through them all. Currently this loop takes the same polygon and writes it 27084 times, overwriting the output each time.
What changes do I need to make in order for it to take each polygon within the directory and write as a distinct *.ascii file for each?
P.S The ascii files produced include '0.0000000' as the first value, any ideas on how to avoid this?
code
library("rgdal")
library("sp")
library("raster")
library("stringr")

wd<- "path//to//directory"
setwd(wd)

PatchShapes <- list.files(wd, pattern = "\\.shp$")

asciiID<- paste0("", str_pad(1:27084, width = 2, side = "left", pad = 0),
".ascii") #List to save each ASCII as

## Raster to be used as extent templates
template<-raster("path//to//raster//file.txt")

for (i in 1:27084){

p <- readOGR(dsn= wd , layer = "1") #Need to turn this "1" into i

pr<-raster()

extent(pr)<-extent(template)
template raster

res(pr)<-1000

patch<-rasterize(x=p, y=pr ,field= p$Patch)

writeRaster(patch, filename = "place//to//save//ASCII//file",
format="ascii",overwrite=T, NAflag=0) }

Answer: It seems you would like something like this:
library("raster")
library("rgdal")

PatchShapes <- list.files(pattern = "\\.shp$")

template <- raster("path//to//raster//file.asc")
res(template)<-1000

for (i in 1:length(PatchShapes)) {
    s <- shapefile(PatchShapes[i])
    f <- paste0(i, ".asc")
    patch <- rasterize(s, template ,field= s$Patch, filename=f, overwrite=TRUE, NAflag=0)
}


access profile2 fields values

Question: How can I access fields values inside profile2 main profile? Only on edit main profile page? I try to hide a field on profile2 edit page based on another value. Code is from my template.php
function my_theme_form_profile2_edit_main_form_alter

and then calling
global $user;
$profile = profile2_load_by_user($user->uid, 'main');
$checkbox1 = $profile->field_checkbox_admin1['und'][0]['value'];

The problem is that profile variable is not accessible if all of the fields in profile are empty. dpm($profile) shows nothing and I get undefined variable error.
Answer: Just define the  variable already before using it
$profile ="";
global $user;
$profile = profile2_load_by_user($user->uid, 'main');
$checkbox1 = isseet($profile->field_checkbox_admin1['und'][0]['value']) ?      $profile->field_checkbox_admin1['und'][0]['value'] : "";

That is nature the profile 2 module, it doesn't create any entry for any field in the database untill it is not assigned any value actually. So it returns nothing if it is empty.
Hope it will help
Comment: There is no point in declaring the $profile variable before the call the profile2_load_by_user. It will be overwritten no matter what. What is missing is a test to see what was returned after the call and before making assumptions on how to use it.
Comment: @J.Reynolds How do you otherwise propose to check a value of a field inside an empty profile2 profile? I am hiding a field based on a field in same profile on same profile page. I need to get to the value first to be able to compare. If empty then hide field.
Comment: @loparr If you are checking values on the profile page, then you are better off just inspecting $form and $form_state. You say you want to hide a field on profile2 edit page based on another value. Is this 'another value' present as a field value on the same profile page? If so $form and $form_state is what you are looking at.
Comment: @J.Reynolds Defining $profile was just to avoid undefined variable error if you are using it further. Otherwise we can have many options to get the data. Also the options proposed by you is also good.
Comment: @J.Reynolds Yes, I want to control visibility of fields based on values of checkbox in the same form - so values are 0 or 1. So you say there is no need to load profile? Looking at dpm($form) I could see there are my fields but how to access boolean data? devel shows ['profile_main']['field_checkbox_admin1']['und']['#value_key']. Should I do something like $check = ['profile_main']['field_checkbox_admin1']['und']['#value_key']? Thank you
Comment: @J.Reynolds I did dsm the $form-state and found that getting to my field value should be $xxx = $form_state['profiles']['main']->field_checkbox_admin1['und'][0]['value']; Now it is getting values without need to load user profile, which is good. Thank you for guidance. If you want please write an answer so I can give you credit.
Comment: @J.Reynolds after some testing I found out that I have to add a check if profile exists anyway, because the field value is not accessible. The  I am confused. $form_state['profiles']['main'] object does not contain any values if this is a new user.
Comment: @loparr Just work with the values that are in the $form variable, ie. $form['profile_main']['field_YOURFIELD']. For a new user, $form_state will not yet contain a profile object, and $form_state is used for validation, submission and multi-step forms. Back to your original question, you want to hide a field dependent on another. Look at this [Drupal 7 FAPI's #states: A Great New UI Improvement For Forms](http://randyfay.com/states). There are lots of articles and videos tutorials. If you want to for example hide or show a field when a user selects a specific radio button, then this is for you.

access profile2 fields values


Has anyone used beta profiling module

Question: For most of my projects i am using Devel module. Recently i looked at other module Profiling - it also provide integration with views and Panels. I was thinking to use this, as mentioned here it is stable but still lacks the Views and Panels support. So not sure i should use it or not (i am using views and panels).
Is there anybody who used this module and found it working (without bugs) ?
OR
You would recommend other module?
Thanks in advance.
Answer: I have used profiling 7.x-1.0-alpha1, as it was alpha release there were some issues. I hope those have been fixed in the beta release, so maybe you can use it. I would recommend not to use the beta version either; it can break anything.
However, if you are willing to learn how to use this module, just use it; it is not too risky.
Comment: Thanks bee for your quick response. Actually i was not sure how much it could break other functionality. Defiantly I am not going to use on production environment until i am not happy with it on my dev server. I will test it on my local machine.
Comment: i was curious is there any other better module for profiling with panels
Comment: @innocent there are other modules but sorry, i have not used on commercial level. you need to do some research -:)

Has anyone used beta profiling module


Struggling with jQuery Toggle function

Question: I am working on a band blurb page, example here.
The idea is to click on a photo which will display the band member blurb on the left, overlaying the overall band description.
My issue with the current code is that using toggle to display the alternative individual blurbs means that when you click between the different members, sometimes the toggle state resets and it seems like nothing is happening. Each time you click a photo, that persons bio should display. Only when you click the same one twice, or a 'return to band bio' button (not yet on the page) should the main bio display.
My current code is below:
jQuery(document).ready(function($) {

$('#gotoben').toggle(function() {
    $('li.gotoben').fadeTo("slow", 1);
    $("#dan").hide();
    $("#ben").show();
    $('li.gotoben').siblings().fadeTo("slow", 0.3);
},
  function () {
    $('li.gotoben').fadeTo("slow", 1);
    $('li.gotoben').siblings().fadeTo("slow", 1);
    $("#ben, #dan").hide();
});

$('#gotodan').toggle(function() {
    $('li.gotodan').fadeTo("slow", 1);
    $("#ben").hide();
    $("#dan").show();
    $('li.gotodan').siblings().fadeTo("slow", 0.3);
},
  function () {
    $('li.gotodan').fadeTo("slow", 1);
    $('li.gotodan').siblings().fadeTo("slow", 1);
    $("#dan, #ben").hide();
});

});

I have tried using the .click function but also cannot make everything work 100% smoothly.
Can anyone help me make this work?
EDIT - WORKING CODE
The only thing added here to patricks code is the inclusion of the jQuery stop function.
    jQuery(document).ready(function($) {

    $('.bigLeft div:gt(0)').hide();

    $('ol.band li').click(function() {
        var $th = $(this);

            // Hide the current profiles
        $(".bigLeft").children().hide();

        if( $th.hasClass('selected') ) {
            $th.stop(true, false).siblings().fadeTo("slow", 1);
            $th.removeClass('selected');
            $('#theBand').show(); // <-- change the ID to the default
        } else {
            $th.addClass('selected');

              // Grab the ID of the one that was clicked
            var id = $th.attr('id');

              // Fade in the current, and fade the siblings
            $th.stop(true, false).fadeTo("slow", 1)
                   .siblings().removeClass('selected').stop(true, false).fadeTo("slow", 0.3);

              // Show the clicked profile by concatenating the ID clicked with '_profile'
            $("#" + id + "_profile").show();
        }
    });

});

Answer: I would tend to take a little different approach.
Give each li under ol.bind the ID #ben, #dan, etc.
Then give each profile a similar ID #ben_profile, #dan_profile, etc.
Then use that consistency to your advantage:
$('ol.band li').click(function() {
    var $th = $(this);

        // Hide the current profiles
    $(".bigLeft").children().hide();

    if( $th.hasClass('selected') ) {
        $th.siblings().fadeTo("slow", 1);
        $th.removeClass('selected');
        $('#defaultProfile').show(); // <-- change the ID to the default
    } else {
        $th.addClass('selected');

          // Grab the ID of the one that was clicked
        var id = $th.attr('id');

          // Fade in the current, and fade the siblings
        $th.fadeTo("slow", 1)
               .siblings().removeClass('selected').fadeTo("slow", 0.3);

          // Show the clicked profile by concatenating the ID clicked with '_profile'
        $("#" + id + "_profile").show();
    }
});

You can try it out here: http://jsfiddle.net/kDqsT/
Comment: That works great except for if you click the selected profile again it does not return to the 'Band' bio. That was the main reason for using 'toggle' so you could return to the start so to speak.

Any ideas on that?
Comment: @DanC - That's alright. I added those later. I just updated. Untested, but I think it will work. Let me know. :o)
Comment: @patrick scrap last comment - that works just fine! Many thanks for your help! Now I can get back to watching the World Cup and not fretting over this minor design detail. Thanks again!
Answer: Here's a way to do it with the click function.
var current_loc = '';
jQuery(document).ready(function($) {

    $('#gotoben').click(function() {
        if(current_loc != 'ben'){
            current_loc = 'ben';
            $('li.gotoben').fadeTo("slow", 1);
            $("#dan").hide();
            $("#ben").show();
            $('li.gotoben').siblings().fadeTo("slow", 0.3);
        }else{
            current_loc = '';
            $('li.gotoben').fadeTo("slow", 1);
            $('li.gotoben').siblings().fadeTo("slow", 1);
            $("#ben, #dan").hide();
        }
    });

    $('#gotodan').click(function() {
        if(current_loc != 'dan'){
            current_loc = 'dan';
            $('li.gotodan').fadeTo("slow", 1);
            $("#ben").hide();
            $("#dan").show();
            $('li.gotodan').siblings().fadeTo("slow", 0.3);
        }else{
            current_loc = '';
            $('li.gotodan').fadeTo("slow", 1);
            $('li.gotodan').siblings().fadeTo("slow", 1);
            $("#dan, #ben").hide();
        }
    });
});

I'm sure there are any number of better ways than this.
Comment: Can't seem to get that to work but many thanks for the suggestion! I'll have a play and see what happens!

Struggling with jQuery Toggle function


Is it possible to run the following web queries in parallell?

Question: I am using Python with the modbus_tk package to poll n PLCs. Each poll takes ~5 seconds. Is it possible to run these in parallel so that it doesn't take n*5 seconds to get all the data back?
My current code:
for ip in ip_addresses:
    master = modbus_tcp.TcpMaster(host=ip_address)
    my_vals = (master.execute(1, cst.READ_HOLDING_REGISTERS, starting_address=15))
    return my_vals

Comment: If either of the answers helped, you should consider accepting one...
Comment: @wallacer I will, I have tried the code you provided, although with only 4 active ip's that I have, I am getting the same speed on my stopwatch (about 13 seconds). I would like to come back and answer with what solved my issue.
Comment: That's surprising... Yes, don't accept if it didn't solve your issue ;)
Have you tried putting some simple timing code and prints in there?  Even just a print at the end of the run method for each thread would let you see if they appear to be hitting a bottleneck somewhere and waiting on eachother - which would explain the time being roughly the same.
Answer: I don't have knowledge of modbus_tk, but can you just use the threading library?  Create 1 thread for each ip address to poll.
Here's some sample code that should get you rolling:
import threading

class Poller( threading.Thread ):
    def __init__( self, ipaddress ):
        self.ipaddress = ipaddress
        self.my_vals = None
        threading.Thread.__init__(self)

    def run( self ):
        master = modbus_tcp.TcpMaster(host=self.ipaddress)
        self.my_vals = (master.execute(1, cst.READ_HOLDING_REGISTERS, starting_address=15))

pollers = []
for ip in ip_addresses:
    thread = Poller(ip)
    pollers.append(thread)
    thread.start()

# wait for all threads to finish, and collect your values
retrieved_vals = []
for thread in pollers:
    thread.join()
    retrieved_vals.append(thread.my_vals)

# retrieved_vals now contains all of your poll results
for val in retrieved_vals:
    print val

Multiprocessing will work here as well, though it's overkill for the problem.  Since this is an I/O operation, it's an ideal candidate for threading.  The GIL (global interpreter lock) won't slow you down or anything, and threads are lighter weight than processes.
Comment: In general a bad idea to generate one thread per ip address. For one - particularly with a 32-bit process - you can easily run out of resources for all those threads and second having thousands of threads will lead to bad performance. But since I doubt any casual user will have thousands of PLCs it's fine in this case.
Comment: @Voo Yes, if your use case involves having thousands of PLCs, you'd probably want to batch multiple ips per Poller thread.  However I expect, as you say, that this is not the OP's use case, and so I presented a simple solution that should give him the performance boost he's looking for
Answer: Use multiprocessing.imap_unordered. It lets you start up a pool of processes, send jobs to the pool, and receive results when they come in.
Here's sample code that downloads a bunch of URLs:
import multiprocessing, re, subprocess, sys

CMD_LIST = [
    ["wget", "-qO-", "http://ipecho.net/plain"],
    ["curl", '-s', "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi"],
    ["curl", '-s', "v4.ident.me"],
    ["curl", '-s', "ipv4.icanhazip.com"],
    ["curl", '-s', "ipv4.ipogre.com"],
]

ip_pat = re.compile('[0-9.]{7,}')
pool = multiprocessing.Pool(5)
for output in pool.imap_unordered(subprocess.check_output, CMD_LIST):
    print 'output:',output
    m = ip_pat.search(output)
    if m:
        print 'GOT IP:', m.group(0)
        pool.terminate()
        sys.exit(0)

print 'no IP found'

Comment: why not use threads?  On nix systems, processes are cheap, so it's not too much overhead.  If the OP happens to be using windows, the overhead will be much higher than threading...
Comment: @wallacer: good point.  For OP's question on multiple I/O bound jobs, threads would indeed be lower overhead, especially on Windows.  I recommend `multiprocessing` because it's easier to program.  The `Pool` alone makes many things much easier; `threading` is too low-level.


Why does stochastic gradient descent lead us to a minimum at all?

Question: Why do we think that stochastic gradient descent is going to find a minimum at all? I mean on each iteration SGD moves in the direction that reduces only current batch's error (SGD doesn't care about the rest of the samples). But why should this lead us to a local minimum of our cost function?
I mean we expect normal gradient descent to get stuck on the nearest local minimum, right? But what kind of minima do we expect SGD to get stuck on and why?
And why do we hope that this brand new minimum is going to be deeper than the initial one? Is it more likely and why? What is the reasoning?
Answer: I am assuming you are asking for some intuition behind stochastic gradient decent (SGD) and not for a rigorous proof.
First, recall that you are actually only moving into the direction of the batch gradient scaled by the rate parameter α. Thus, for every batch you only change the parameters slightly.
Let us consider a simple example. Suppose you have to find the coordinates (x, y) of a point r in the 2D plane that is close to two given points p and q. More precisely, you have to find (x, y) that minimizes the function
f(x, y) = (x - pₓ)² + (y - pᵧ)² + (x - qₓ)² + (y - qᵧ)² .

The solution of this problem, is just the midpoint between p and q. (which is 1/2 (p+q)). Let us assume, however, we would be solving this problem using SGD.
Let the first batch be just the point p and the second batch just the point q. Hence, the first batch reduces the term
(x - pₓ)² + (y - pᵧ)²

while the second one reduces
(x - qₓ)² + (y - qᵧ)² .

The minimum value for the first term is the point p while the minimum value for the second term is q. (The gradients the two problems are 2 (r-p) and 2 (r-q), respectively.) Hence, if we apply SDG, we start with a random value for r. Then we move r slightly into the direction of p by applying the first batch (assuming that α is small enough). Then we move r slightly into the direction of q. These two steps are repeated.
The point r will move along a zig-zag path. First, getting a bit closer to p then a bit closer to q then a bit closer to p and so on. If you choose the rate α appropriately then r will actually move towards the midpoint between p and q, for the following reason. If you draw a line from the starting point to the midpoint of p and q. On average you will be moving into the direction of this line, since whatever one step moves into the direction perpendicular to this line will be canceled by the next step. An example is shown below.

From this example, you can see, which assumptions are made when using SGD. The parameter α has to be chosen small enough such that you are not jumping around wildly, but just move slightly towards the minimum of each batch. If you were to compute the corrections of all batches at once with respect to the same value of r, instead of updating r between each batch, and then applying all corrections at once, you would actually be performing a step of (batch) gradient decent. If α is small, then r does not change significantly when the correction of a single batch is applied. Hence, the effect of computing the corrections of all batches at once is very similar to computing the corrections after each update, and thus (batch) gradient decent and SDG converge to the same local minimum.
Comment: Thank you so much for such a detailed answer! Would it be correct to say, that if these assumptions (in your last paragraph) don't hold, then there is less chance of moving towards a local minimum? In this case we can't predict where this point is going to head, right? So SGD implies that we have some assumptions about dataset.
Comment: @mathgeek I have to admit that the last part was a bit misleading. I was actually referring to the generalization property that you want in a machine learning context. This, however, has nothing to do with the convergence of SGD, and there is no dependence on the data. I have updated the answer.
Comment: @H.Rittich Very elucidating answer! Is the following correct? Intuitively based on your example, the optima of all samples $x_i$ call it $w_i^*$ are concentrated around a (probably) hypersphere. In the early phases of the training each individual update takes us closer to this region and that's why we see the fast decrease in the training loss. However, when we are close enough to this region, the algorithm can't settle down, because each update pulls toward $w_i^*$ rather than $w^*$.
Comment: @ado sar Assuming that the optima of the samples are concentrated around a point is sufficient to explain the behavior that you describe. However, you do not need to be that strict with your assumptions. As you can see in the picture, progress is being made towards the center of $p$ and $q$, eventhough the iterate is neither close to $p$ or $q$. In machine learning we usually minimize an error function that is the sum of the individual error terms, and in this case, the errors that we make by each individual minimization average out.
Comment: Yeah, what I wanted to say is that the minimizer of the total training loss will be somewhere betweeen the individual minimizers (the same as with the two point examples).
Comment: @H.Rittich Is there any intuition why SGD works also for non-convex functions (e.g. loss functions of neural nets)?


workbox not picking up in-memory files with webpack-dev-server

Question: In our project we are using webpack-dev-server to run our development environment.
We are currently transforming the project into a progressive-web-app using workbox.
When we run webpack with NODE_ENV=production everything is working fine, as it first compiles the files, and then the workbox plugin picks them up and adds them to the service-worker.
When we run webpack-dev-server hot, the build fails when running the workbox plugin as it cannot find any files in the dist folder.
The problem seems to be that workbox is not picking up the js and css files that are generated in-memory and seems to need the files on the file system.
module.exports = {
  context: path.resolve(__dirname, 'front'),
  entry: [
    ...preEntries,
    './react/app.js'
  ],
  output: {
    path: path.resolve(__dirname, 'front-dist'),
    filename: `react/app.${git.gitCommitAbbrev}.js`,
    chunkFilename: `react/[id].app.${git.gitCommitAbbrev}.js`,
    publicPath: '/glass/'
  },
  devtool: isProdEnv ? false : 'eval-source-map',
  stats: {
    chunkModules: false
  },
  module: {
    ...
  },
  plugins: [
    new WorkboxPlugin({
      globDirectory: path.resolve(__dirname, 'front-dist'),
      globPatterns: ['**/*.{html,js,css,woff2}'],
      swDest: path.join(path.resolve(__dirname, 'front-dist/sw/'), 'service-worker.js'),
      handleFetch: true,
      clientsClaim: true,
      skipWaiting: true,
    })
  ]
}

Any idea's as to how this should be working
Comment: have u read https://developers.google.com/web/tools/workbox/get-started/webpack?
Comment: They claimed to make it work with webpack-dev-server in this merge:
https://github.com/GoogleChrome/workbox/pull/808
But I'm still having the same problem. If there's no build directory in the file system, it just doesn't work :(
Comment: As you pointed out it's because files are in memory not on disk, I believe this answer can help you here - https://stackoverflow.com/questions/41928358/webpack-how-to-compile-write-on-disk-and-serve-static-content-js-css-using-w
Comment: Have you found a solution since then?
Answer: Recent releases of workbox-webpack-plugin have support for in-memory filesystems, with coverage in its test suite.
If you're still having troubles with the current release of Workbox, please open an issue on the issue tracker and we can investigate further.
That being said, using a cache-first service worker inside of a hot-reload, local development environment is a puzzling choice, as caching defeats the purpose of seeing updates right away.


Double click on row in data grid in WPF

Question: I want to have function in WPF:Double click on row in Data Grid will show
cells in window. How can I do it that?I programm so but I never have double click event. Can somebody help me?
Thanks.
     <DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left"Margin="14,55,0,46"
                Name="dataGridCustomers" Width="575" ItemsSource="  {Binding Path=LoadDataBinding}"
                CanUserResizeRows="False">

            <DataGrid.RowStyle>

            </DataGrid.RowStyle>

            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=DEPARTMENT_ID}" Header="DepartmentID" Width="100"
                    IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding Path=DEPARTMENT_NAME}" Header="Department name" Width="100"
                    IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding Path=LOCATION}" Header="Location" Width="150"
                    IsReadOnly="True" />

            </DataGrid.Columns>
        </DataGrid>

Comment: Can you add your C# code where are your handler is?
Answer: Try this
So, for instance, your XAML might look something like this:
<SomeControl  MouseDown="MyMouseDownHandler">
...
 </SomeControl>

code behind the click event..
private void MyMouseDownHandler(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 2)
    {
        //Handle double-click
    }
}

Answer: Just create your own dependency property DataGridDoubleClickProperty where you attach handler for DataGrid.MouseDoubleClick event
public static class Commands
{
  public static readonly DependencyProperty DataGridDoubleClickProperty =
    DependencyProperty.RegisterAttached("DataGridDoubleClickCommand", typeof ( ICommand ), typeof ( Commands ),
                      new PropertyMetadata(new PropertyChangedCallback(AttachOrRemoveDataGridDoubleClickEvent)));

  public static ICommand GetDataGridDoubleClickCommand(DependencyObject obj)
  {
    return (ICommand) obj.GetValue(DataGridDoubleClickProperty);
  }

  public static void SetDataGridDoubleClickCommand(DependencyObject obj, ICommand value)
  {
    obj.SetValue(DataGridDoubleClickProperty, value);
  }

  public static void AttachOrRemoveDataGridDoubleClickEvent(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  {
    DataGrid dataGrid = obj as DataGrid;
    if ( dataGrid != null )
    {
      ICommand cmd = (ICommand) args.NewValue;

      if ( args.OldValue == null && args.NewValue != null )
      {
        dataGrid.MouseDoubleClick += ExecuteDataGridDoubleClick;
      }
      else if ( args.OldValue != null && args.NewValue == null )
      {
        dataGrid.MouseDoubleClick -= ExecuteDataGridDoubleClick;
      }
    }
  }

  private static void ExecuteDataGridDoubleClick(object sender, MouseButtonEventArgs args)
  {
    DependencyObject obj = sender as DependencyObject;
    ICommand cmd = (ICommand) obj.GetValue(DataGridDoubleClickProperty);
    if ( cmd != null )
    {
      if ( cmd.CanExecute(obj) )
      {
        cmd.Execute(obj);
      }
    }
  }

}

In your View you use Binding to map this DependencyProperty to Command
<Grid DataContext="{StaticResource viewModel}">
  <DataGrid AutoGenerateColumns="True"
        ItemsSource="{Binding Data}"
        SelectedItem="{Binding SelectedItem}"
        clr:Commands.DataGridDoubleClickCommand="{Binding DataGridDoubleClick}"
    />
</Grid>

DataGridDoubleClick is ICommand property in your ViewModel class
Comment: @leoleo Can you add your C# code where are your handler is?
Answer: You can subscribe on PreviewMouseDoubleClick event.

Handler:
private void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
    System.Diagnostics.Debug.WriteLine("DoubleClick!");
}

Comment: I guess your handler doesn't work because MouseDoubleClick event is handled by DataGrid.
Routed event may be either bubble (from child to parent) or tunnel (from parent to child).
Any Preview event is tunnel event that allows you to handle it before DataGrid does.

Double click on row in data grid in WPF


Detectar tamanho da tela e aplicar scroll no elemento responsivo

Question:
Quero fazer uma aplicação com seguintes definições, uma tela(responsiva),com uma lista de títulos para vídeo aula á direita, e é preciso ter scroll,sempre que coloco o scroll nesse elemento, não fica como gostaria, pois o Height 100% não pega a resolução.
    <!DOCTYPE html>

            <title>play de video</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <link href="assets/css/reset.css" rel="stylesheet" type="text/css"/>
            <link href="assets/css/style.css" rel="stylesheet" type="text/css"/>
            < src="assets/js/jquery.js"></>
            <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet" type="text/css"/>

Curso: Administração.

01 - Iniciando curso de Administração.

                        <progress class="progress_bar">

                        </progress>

01 - Iniciando curso de Administração

Conceitos de Administração

02 - Iniciando curso de Administração

02 - Conceitos de Administração

©2015 - TODOS OS DIREITOS RESERVADOS

            < src="assets/js/s.js"></>

    body{ overflow: hidden;}
    .container{position: relative; top: 0px; min-height: 100%;}
    .main_header{
        width: 100%;
        height: 90px;
        background: #555454;
    }
    .logo{width: 100%; max-width: 120px; margin: 15px 35px;}
    .content h1{margin: 5px 35px;}
    .content p{margin: 0 60px;}
    .content p img{ margin-bottom: 5px;}
    .content_width{width: 70%;}
    .fontlitgh{ font-size: 14px; font: 'Open Sans', sans-serif; font-weight: 300;}
    .title_curso{ font-size:15px; font:'Open Sans', sans-serif; color: #a4a4a4;}
    .title_aula{font-size:18px; font: 'Open Sans', sans-serif;}
    .content_ligth{width: 70%; height:60px; background: #e4e4e4;}
    .content_video{ float: left; height: 100%; }
    .progress_bar{clear: both; width: 100%; height: 5px; background: #c83d00;}

    .asside_block{width: 30%; height:100%; float:right; color: #FFF; background: #343434; margin-top: -83px;}
    .asside_block ul{ background: #343434; float: left; display: block; margin: 2px;}
    .asside_block li{width: 70%; display:block; background: #e4e4e4; float: right; margin: 5px 10px;}
    .asside_block h1{font-size: 14px !important; margin: 3px 10px 5px 0; float: right;}
    //.asside_block li:hover{ cursor: pointer; background: red;}
    .clear{clear: both;}

    /*ICONS*/
    .img_icons{width: 100%; max-width: 25px;}

    /*-- FOOTER -- */
    .final_footer{ width: 100%; height: 60px; background: red;}
    .final_footer p{text-align: center;}

    /*-- ARTICLE -- */
    .article_left{width: 70%; margin: 0 auto; padding: 0;}
    .content_section{width: 100%;}
    .header_article{height:75px; background:#e4e4e4 url(../images/icons-seta.png) no-repeat -5px 43px; border-left:5px solid #c83d00; }
    .header_article h1{margin: 8px 35px 3px; float: left;}
    .header_article p{margin: 0 60px; float: left; clear: both;}
    .header_article img{ margin: 0px 5px 5px 0px; float: left;}

    .content_section_video{ float: left; width:100%; height: 100%; clear: both;}

    /*PROGRESS BAR*/
    .progress_bar{clear: both; width: 100%; height: 10px; float: left; position:relative; bottom: 0px;}
    progress{display:block; -webkit-appearance: none;}
    progress::-webkit-progress-bar { background: #343434; padding: 2px;}
    progress::-moz-progress-bar { background: #343434; padding: 2px;}
    progress::-ms-progress-bar { background: #343434; padding: 2px;}
    progress::-o-progress-bar { background: #343434; padding: 2px;}

    progress::-webkit-progress-value { -webkit-border-radius: 50px; background:#c83d00;}
    progress::-moz-progress-value { -moz-border-radius: 50px; background:#c83d00;}
    progress::-ms-progress-value { -ms-border-radius: 50px; background:#c83d00;}
    progress::-o-progress-value { -o-border-radius: 50px; background:#c83d00;}

    /*-- ASIDE SIDEBAR -- */
    .thumbnail{float: left; width: 90px; height: 60px; margin: 5px;}
    .aside_block{width: 30%; height:100%; float:right; color: #FFF; background: #343434; overflow-y: scroll;}
    .aside_block ul{ background: #343434; float: left; display: block; margin: 2px;}
    .aside_block li{width: 80%; display:block; background: #e4e4e4; float: right; margin: 5px 10px;}
    .aside_block h1{font-size: 14px !important; margin: 3px 10px 5px 0; float: right;}
    .left_block{width: 100%; float: left;}
    .left_block:hover{width: 100%; float: left; border-left:4px solid #c83d00; cursor: pointer;}

    /*-- FOOTER -- */
    .content_footer{width: 100%; height: 60px; background: #dbdbdb; position: relative; bottom: 0px;}
    .content_footer p{text-align: center; font-size: 0.6em;}

    /* -- STYLE VIDEO YOUTUBE*/

/*outro css - reset*/
*{
    margin: 0;
    padding: 0;
    font-family:'Open Sans', sans-serif;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
}
html,body{height: 100%;}
/* --FONT - TEXT--*/
.fontzero{font-size: 0px !important; margin: 0 !important;}
.fontwriter{color: #D78484 !important;}
.fontblack{color:#000 !important;}
.ligth{font-weight: 300 !important;}
.bolder{font-weight: 400 !important;}
.small{font-weight: 600 !important;}
.bold{font-weight: 700 !important;}
.small-caps{font-variant: small-caps !important;}

/* -- ICONES -- */
.icons_play{background: url(../..//assets/images/play_title.png) no-repeat center left;}

/* -- FLOATS --*/
.f_left{float: left;}
.f_right{float: right;}
.f_clear{clear: both;}

article, section{
    text-align: center;
}
h1{font-size: 1em; font-weight: 300;}
p{font-size: 0.8em; font-weight: 400; margin-bottom: 10px;}
img{max-width: 100%; vertical-align: middle;}
ul{ list-style: none;}

form{float: left; width: 100%; text-align: left;}
label{float: left; width: 100%; margin-bottom: 20px;}
label .text{font-size: 0.8em; text-transform: uppercase; font-weight: 600; margin-bottom: 6px; display: block; color: #fff;}

Comment: Vai facilitar a ajuda se você colocar o código html da sua página na sua pergunta.
Comment: Coloca um bocado do código, e como gostarias que ficasse?
Comment: Amigos, estou resumindo o código, assim que deixar mais limpo, coloco aqui.
Answer: O height do seu "aside" não pega a resolução toda, pois você não definiu um height para a div com a classe "content" que é a pai dele. Você definindo o tamanho para ela, o seu height 100% do aside começa a funcionar.
Faça um teste acrescentando o exemplo abaixo no seu css.
.content {
   height: 85%;
}

aside {
 overflow:auto;
}

Comment: Após ter feito essa mudança deu certo o scroll, porém o rodapé não acompanha a resolução. @Rafael Lacerda
Comment: Não sei se seria possível fazer de acordo com a proposta, o vídeo já estar adaptativo, agora os elementos não estão.
Comment: @Arnaldo isso eu teria que analisar melhor seu código, rodar ele pra ver, porque só olhando a estrutura não sei te dizer. Estou meio sem tempo agora, assim que eu voltar eu vejo.


Angularjsjs form required field don't validate on load

Question: I have a form text field it is required but its initial value is an empty string, so when I load the form the controller validates the form field and marks it as error, I want on the initial load not to validate the form field only after an onblur event.
<code><input data-ng-model="MyCtrl.opportunity.company" type="text" class="form-control" name="company" ng-model-options="{ updateOn: 'blur' }" required />
</code>
And here is the fiddle http://jsfiddle.net/petran/Lvc0u55v/725/
I know I can call $setValidity on controller I am looking for a more elegant solution if there is exist.
Answer: Add this statement to ng-class statement:
<code> ...&& addOpportunityForm.company.$dirty...
</code>
Comment: Please add some explanation. Without, your answer might get flagged "low quality" and eventually removed.
Comment: This statement is for checking that input was changed once. After being changed once, $dirty flag is true.
Answer: Use the $pristine for validation tool in AngularJS. This will allow the funtion to wait until the input field has been changed before testing the condition. You can learn more about form states here: Angular Form States


Calling a function with multiple parenthesis

Question: While doing some JavaScript challenges, I faced with the code add(1)(2), a function being called with several parenthesis. The challenge is to make a chain of add function.
add(1)(2)(3);       // -> 6
add(1)(2)(3)(4);    // -> 10
add(1)(2)(3)(4)(5); // -> 15

Also, we should be able to store the returned values and reuse them.
var addTwo = add(2);

addTwo;       // -> 2
addTwo + 5;   // -> 7
addTwo(3);    // -> 5
addTwo(3)(5); // -> 10

Thanks in advance.
Comment: can you share the code in your post please?
Comment: What programming language ?
Comment: I edited the post . It is JavaScript , sorry if  you found my post  little messy it is my first question and i haven't get used to it yet .Thanks
Answer:  function sum(firstArg, opt_argsStack) {
    const argsStack = (opt_argsStack || []).concat([firstArg]);

    const nextSum = nextArg => sum(nextArg, argsStack);
    nextSum.valueOf = () => argsStack.reduce((a, b) => a + b);
    return nextSum;
    }

    console.log(+sum(1));
    console.log(+sum(1)(2));
    console.log(+sum(1)(2)(3));
    console.log(+sum(1)(2)(3)(4));

Here, we pass the stack of all accumulated arguments down to recursive call. And in valueOf call, stack is transformed to a number via reduce just in time. This gives us more flexibility, imo, if we wanted to change chained sum to chained multiplication or whatever else — we just plug in different reduce function.
refer:https://medium.com/reflecting-on-bits/fun-fact-sum-of-numbers-through-infinite-currying-in-js-a5c229765a18
Comment: Got it. Thanks a lot I really appreciate your help.
Comment: @Sashi yadav Do not forget to quote the original resource from where you copied the solution. https://medium.com/reflecting-on-bits/fun-fact-sum-of-numbers-through-infinite-currying-in-js-a5c229765a18


Criar array de strings de textarea usando \n como separador

Question: Como criar um array de strings usando quebra de linha como separador value.split("\n")?
Ex:
<textarea placeholder="LISTA" type="text"> </textarea>

E como posso enviar um request utilizando axios para cada quebra de linha utilizando um foreach?
Tentativa falhada:

Comment: faz o split e um for/foreach no resulta, então faz a chamada ajax dentro do loop
Comment: Wagner, acredito que sua dúvida não seja sobre vue, tente criar exemplos enxutos sem aquilo que não faz parte do problema, isto ajuda quem quer te ajudar, ok?
Answer: Já respondeu sua própria pergunta: use o split para separar as linhas. O que faltou no seu código foi iterar sobre o array, com for ou foreach para, como perguntou, fazer uma chamada ajax para cada linha, ou cada item do array:

document.getElementById("testar").addEventListener("click", teste);

function teste() {
  // usarei isso para que "e" seja o textarea
  var e = document.getElementById("lista");
  var lista = e.value.split("\n");

  lista.forEach(function(linha){
      console.log(linha);
      // então é só fazer a chamada ajax:
      // axios.get('http://localhost/fg/nova.php?lista=' + linha) ....
  });

}
<textarea id="lista" placeholder="LISTA" type="text"> </textarea>
<button id="testar">Testar</button>


Return items around all instances of an item in a list

Question: Say I have a list...
['a','brown','cat','runs','another','cat','jumps','up','the','hill']

...and I want to go through that list and return all instances of a specific item as well as the 2 items leading up to and proceeding that item.  Exactly like this if I am searching for 'cat'
[('a','brown','cat','runs','another'),('runs','another','cat','jumps','up')]

the order of the returned list of tuples is irrelevant, ideally the code handle instances where the word was the first or last in a list, and an efficient and compact piece of code would be better of course.
Thanks again everybody, I am just getting my feet wet in Python and everybody here has been a huge help!
Comment: http://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python
Answer: Without error checking:
words = ['a','brown','cat','runs','another','cat','jumps','up','the','hill']
the_word = 'cat'

seqs = []

for i, word in enumerate(words):
    if word == the_word:
        seqs.append(tuple(words[i-2:i+3]))

print seqs #Prints: [('a', 'brown', 'cat', 'runs', 'another'), ('runs', 'another', 'cat', 'jumps', 'up')]

Answer: A recursive solution:
def context(ls, s):
    if not s in ls: return []
    i = ls.index('cat')
    return [ tuple(ls[i-2:i+3]) ] + context(ls[i + 1:], s)

ls = ['a','brown','cat','runs','another','cat','jumps','up','the','hill']

print context(ls, 'cat')

Gives:
[('a','brown','cat','runs','another'),('runs','another','cat','jumps','up')]

Answer: With error checking:
def grep(in_list, word):
    out_list = []
    for i, val in enumerate(in_list):
        if val == word:
            lower = i-2 if i-2 > 0 else 0
            upper = i+3 if i+3 < len(in_list) else len(in_list)
            out_list.append(tuple(in_list[lower:upper]))
    return out_list

in_list = ['a', 'brown', 'cat', 'runs', 'another', 'cat', 'jumps', 'up', 'the', 'hill']

grep(in_list, "cat")
# output: [('a', 'brown', 'cat', 'runs', 'another'), ('runs', 'another', 'cat', 'jumps', 'up')]

grep(in_list, "the")
# output: [('jumps', 'up', 'the', 'hill')]

Comment: did you assign the list to a variable?
Comment: @AvinashRaj I'm exactly sure what you mean, but I edited my answer to hopefully clarify...


Alguém sabe porque está a dar erro neste programa?

Question: void ordenar(struct piloto *vetor,int contador_pilotos)
{
    int z,i,x,j;
    for(x=contador_pilotos-1;x<=1;x--)
    {
        for(i=0;i>x;i++)
        {
            if(strcmp(vetor[i].nome,vetor[i+1].nome)>0)
            {
                strcpy(z,vetor[i].nome);
                strcpy(vetor[i].nome,vetor[i+1].nome);
                strcpy(vetor[i+1].nome,z);
            }
        }
    }
    for(j=0;j<=contador_pilotos;j++)
    {
        printf("%s",vetor[j].nome);
    }
}

Comment: @VictorStafusa suicídio prematuro :)
Comment: Qual é o erro, meu filho?
Comment: até onde eu sei, strcpy utiliza 2 vetores de char como parâmetro, logo essa variável z do tipo int no strcpy(z,vetor[i].nome); deve dar problema
Comment: Perguntado há 13 minutos por user64362. Como é que é isso? Quem é o autor da pergunta?
Comment: simplesmente o programa crasha :/
Answer: A sua primeira estrutura for, não inicia ou fica em loop infinito.
for(x=contador_pilotos-1;x<=1;x--)

Pode se perceber que contador_piloto poderá ser qualquer valor, > 1 ou < 0. Então:

Quando contador_piloto < 0 x irá decrescer para sempre pois será sempre menor ou igual a 1;
Quando contador_piloto > 2 x será 2 e quando x <= 1, decrementara de x, mas isso não acontecerá pois x não esta dentro da instrução.

Para corrigir esse erro, deve-se mexer na condição:
for(x=contador_pilotos-1;x>0;x--)
// ou
for(x=0;x<contador_pilotos;x++)

Outro erro, é a parte de cópia, z deve ser um vetor de Char. char z[512], então se faz a troca.
strcpy(z, vetor[i].nome);
strcpy(vetor[i].nome,vetor[i+1].nome);
strcpy(vetor[i+1].nome,z);

De uma olhada em algorítimos de ordenação:
Ordenação Comp Sort


State in bolt/function in storm or trident

Question: To apply some function to stream in trident storm we pass newly created instance to each method which is called on stream like this:
stream.each(inputFields, new SomeFunc(), outputFields)

where SomeFunc is a descendant of BaseFunc.
Suppose I want to have some state variable in SomeFunc:
class SomeFunc extends BaseFunction {

  var someState: String = _

  override def execute(tuple: TridentTuple, collector: TridentCollector) = ???
}

If I set parallelism hint to some value greater than 1 for SomeFunc component will storm create multiple instances of SomeFunc? Is accessing/updating someState in SomeFunc a thread safe operation? If instead of defining SomeClass as class I define it as an object, will smth be changed?
EDIT
Ok, with help of user @Shaw in comments to his answer I learned that storm creates one instance of storm component(storm/bolt/function/aggregator etc.) per executor. The question is how does it do this? I want to know the mechanism of this behaviour
Answer: I don't kwow how exactly Trident works but in Storm if you define parallelism hint > 1 you create multiple executors for that component which are threads spawned by the worker process.
That executor will create a X (number of tasks, 1 by default) "instances" of SomeFunc and don't share the variable someState between them. As storm works someState is threadSafe because they execute tuples "sequentially in its own thread" as they are arriving to the component.
I almost sure in Trident is the same because simply is a micro-batching abstraction over Storm.
Sure you have read it but if not, I strongly recommend you read this great article about parallelism in Storm.
Comment: How does it create X number of instances if you don't pass factory method but a concrete instance to `each` method?
Comment: You create a new instance but storm framework creates multiple "threads" underlying by calling prepare, execute, cleanup... methods. That's one of the greatest things of Storm.
Comment: `stream.each(inputFields, new SomeFunc(), outputFields)` is the same if you create instance of someFunc outside and pass the reference to `each`: `stream.each(inputFields, someFunc, outputFields)`. Multiple instances may only exist if topology is submitted to cluster which has many workers. In that case that instances would be created in multiple different jvm instances. But how a few instances will be created in single JVM?
Comment: But in one worker you can have multiple executor threads so you will have one instance per executor.

Each worker runs in its own JVM. Each executor (parallellism hint) is a thread spawned by a worker and then you have tasks (numTask option) that run in the same executor thread serially so numTasks do not increase the parallelism.

Read this answer, http://stackoverflow.com/questions/17257448/what-is-the-task-in-twitter-storm-parallelism
Comment: Imagine the situation: you've created an instance of SomeFunc in main method of the application; you've also created a storm topology in that main method and submitted it. Then later in your main method(suppose after 10 minutes) you changed `someState` variable. Storm topology will see this change as it has the same reference to someFunc instance as your main method
Comment: In my opinion scenario proposed by you will only hold if storm serializes each component(spout, bolt ,function etc) and for each executor restores(deserialize) the state observed in component before serialization happened. Do things happen in this way?
Comment: I don't know how it would work but I think you wouldn't see that change in the variable. Storm serialize once per executor and then work in local, for this reason you can use non-serialize objects in prepare and execute methods.


How to compile related scss file using Live Sass Compiler?

Question: I'm modifying a bunch of related scss files of mustard-ui css framework on vscode. The scss files are imported to mustard-ui.scss which is like this:
// Variables
@import 'vars/breakpoints';
@import 'vars/colors';

// Base
@import 'base/base';

I have also added this snipped to  Live Sass Compiler settings to define the path to put the output compiled .css files:
"liveSassCompile.settings.formats": [{
    "format": "expanded",
    "extensionName": ".css",
    "savePath": "/static/css/mystyle"
}]

The resulting compiled css then is obtained from static/css/mystyle/mustard-ui.css
The problem however is that when I edit one of the scss components, like base.scss, the mutard-ud.scss does not compile automatically to reflect changes. So I need to save it again with some dummy updates to get the updated mustard-ui.css. This is obviously tedious and absurd.
I'm wondering if there is a way to circumvent this? How to order the compiler to watch for related files?
Comment: There is an [open issue](https://github.com/ritwickdey/vscode-live-sass-compiler/issues/83) about this.
Answer: Do two thinks:

Every included file like base.scss must start with a underscore => _base.scss
edit your settings.json and add:

"liveSassCompile.settings.includeItems": ["./path/to/your/main.scss"]

If you now change _base.scss live sass compiler will compile main.scss! :)
Hope it help!
Comment: Tipp: To get the correct path, right click on the file in vsCode and copy relative path.


How to check node authorship info in views

Question: I want to show users their published articles in a block. Those articles have their author name set in by Admin manually while the articles are created by Admin on behalf of the users.
In views I don't find any field in Node to compare authorship of the node with logged in user.
I've found a related article: http://drupal.org/node/228959
but no idea about how to look for following point in the article:
Go to 'Arguments' and select "User: UID is Author" and set the default
value to 'Return Page not found'.

I don't see "User: UID is Author". I only see "User:UID".
Answer: Add a filter to your view of User: Current.  This will filter the nodes to ones created by the current user.
Comment: Laxman13 you are genius.
Comment: Your method works. I've done another way, not sure if it is correct: In Arguments I added User:Uid then in "Action to take if argument is not present" selected "Provide default argument". Then "Default argument type": User ID from logged in user. And this seems to be working. Is this too right?
Comment: Yes that should work as well


Getting the old value of a asp.net control on postback

Question: I want to be able to find the old value of a asp.net control (textbox, checkbox etc) without relying on events (e.g. OnTextChange event). Something like "someTextboxControl.OldText"  would be perfect! I imagine that it is stored in the viewstate and need to somehow get at it via a custom control. Any ideas?
Answer: The answer provided by Shrage was helpful, but it took some additional searching to make this solution work. I placed his code into the code behind within the namespace of the page I was working on. Additionally I had to register the control, and change update the control in question.
Register extended textboxex control; this goes right below the page directive
<code>    <%@ Register TagPrefix="group" Namespace="ESM_Web_Portal" Assembly="ESM Web Portal" %>
</code>
Update the texbox you want to extend in the aspx page
<code>    <group:TextBoxEx ID="txbEditPartNumber" runat="server" Text='<%# Bind("part_number") %>'></group:TextBoxEx>
</code>
Then in code behind I was able to use the extended TextBox control
<code>    protected void CheckForExistingPartNumber(object sender, ServerValidateEventArgs e)
    {
        CustomValidator cv = ((CustomValidator)sender);
        TextBoxEx tb = (TextBoxEx)cv.Parent.FindControl(cv.ControlToValidate);

        if (SupplyBLL.GetSupplyByPartNumber(e.Value) != null && tb.Text != tb.OldText)
        {
            e.IsValid = false;
        }
    }
</code>
Answer: <code>public class TextBoxEx:System.Web.UI.WebControls.TextBox
    {
        public string OldText { get; set; }

        protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            OldText = Text;
            return base.LoadPostData(postDataKey, postCollection);
        }

    }
</code>


C code does not work when coded like C++

Question: Hello Developers! I am learning algorithms from Algorithms Design Manual Book by Skiena. There I have the following code:
    #include <stdio.h>
#include <stdlib.h>

typedef int item_type;

typedef struct{
    item_type item;
    struct list* next;
    }list;

void insert_list(list **l, item_type x){
    list *p;
    p = malloc(sizeof(list));
    p->item = x;
    p->next = *l;
    *l = p;
    }

int main(){
    return 0;
    }

It gives me Warning when compiled:

gcc -Wall -o "test" "test.c" (in directory:
  /home/akacoder/Desktop/Algorithm_Design_Manual/chapter2) test.c: In
  function 'insert_list': test.c:15: warning: assignment from
  incompatible pointer type Compilation finished successfully.

But when I rewrite this code as C++:
 #include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef int item_type;

typedef struct{
    item_type item;
    struct list* next;
    }list;

void insert_list(list **l, item_type x){
    list *p;
    p = malloc(sizeof(list));
    p->item = x;
    p->next = *l;
    *l = p;
    }

int main(){
    return 0;
    }

It gives the following:

g++ -Wall -o "chapter2" "chapter2.cpp" (in directory:
  /home/akacoder/Desktop/Algorithm_Design_Manual/chapter2)
  chapter2.cpp:15: error: conflicting declaration 'typedef struct list
  list' chapter2.cpp:14: error: 'struct list' has a previous declaration
  as 'struct list' chapter2.cpp: In function 'void insert_list(list**,
  item_type)': chapter2.cpp: In function 'void insert_list(list**,
  item_type)': chapter2.cpp:19: error: invalid conversion from 'void*'
  to 'list*'

Can anyone explain why it is so? And How can I rewrite it in C++?
Answer: This is because c++ is stricter than c with respect to type conversions.
There are host of other errors in your code. Please note that just putting a c source code, renaming the file as .cpp & compiling using g++ does not make a c source code as c++.
If you are writing a program in c++ please use new & not malloc, doing so you do not need to explicitly type cast as in case of malloc.
Comment: @Chris Johnson: By the time i finished writing the main erroneous part, other answers already came up with the `elaborated list` of errors, I don't think its worthwhile to repeat the same here, the answer, answered the basic flaw & the point I wanted to make.
Comment: This answer needs to be more specific: "There are host of other errors in your code" doesn't help anyone.
Answer: Your problem in both cases is in the struct definition: struct list *next doesn't refer to the struct you are in the process of declaring. Try this instead:
typedef struct list {
    item_type item;
    struct list* next;
} list;

In addition, in C++ you must cast the void * returned by malloc to the appropriate pointer type (list *), C++ is stricter about these things. Also, BTW, in C++ you can leave off the typedef completely if you want.
The reason for the differing error messages is a difference in the languages.
In C, the compiler knows that struct list * is a pointer to a struct, so it doesn't need to complain that it doesn't actually know what a "struct list" is yet. Later, though, when you try to assign this "struct list *" from a pointer of type "list *" (the type of which is "pointer to an anonymous struct"), it complains about the mismatch.
In C++, a "struct" declaration is more or less equivalent to a "class" declaration (the major difference is in the default visibility of members). Among other things, this means that structs in C++ are more or less automatically typedefed. So when the compiler sees "struct list *next", it takes it as a forward declaration of a class named "list"; then when it finishes the statement and processes the typedef, throws an error because you're trying to typedef something to an identifier that is already (forward-)declared as something else. Then it issues further errors because it doesn't actually know what "list" might be, due to the earlier error.
Comment: it doesn't have to have previous declaration, it interprets `struct list *next` as forward declaration.
Answer: C++ does not allow arbitrary pointer conversions, while C does. But since this is not considered good style, the compiler emits a warning.
Just add a cast and it will solve both messages:
p = (list*)malloc(sizeof(list));

Or if you want to be C++ only:
p = new list;

But then, you should declare constructors and such, also.
Comment: Casting the result of `malloc()` is considered *poor* style in C.  It can mask errors; for example, if you forget the `#include `, some compilers will assume that `malloc()` returns an int.  An idiom that avoids type mismatches is: `p = malloc(sizeof *p);`.
Comment: And if you're trying to write code that will compile either as C or as C++, you're probably doing it wrong.  C/C++ interoperability is good enough that there's rarely a good reason to do this.
Answer: This is explained in this link.
Quote:

Gotcha for a C++ programmer using C
Structs and Enums
You have to
  include the struct keyword before the name of the struct type to
  declare a struct: In C++, you could do this
struct a_struct {
int x; };
a_struct struct_instance;
and have a new instance of a_struct called struct_instance. In C,
  however, we have to include the struct keyword when declaring
  struct_instance:
struct a_struct struct_instance;
In fact, a similar situation also holds for declaring enums: in C, you
  must include the keyword enum; in C++, you don't have to. As a side
  note, most C programmers get around this issue by using typedefs:
typedef struct struct_name {
         /* variables */ } struct_name_t;
Now you can declare a struct with
struct_name_t struct_name_t_instance;
But there is another gotcha for C++ programmers: you must still use
  the "struct struct_name" syntax to declare a struct member that is
  a pointer to the struct.
typedef struct struct_name {
    struct struct_name instance;
    struct_name_t instance2; /* invalid!  The typedef isn't defined
yet */ } struct_name_t;

Answer: You need to change this class:
typedef struct{
    item_type item;
    struct list* next;
    }list;

to this:
struct list {
    item_type item;
    list* next;
    };

Explanation: in the first example, you have anonymous structure, inside which struct list is forward declared. So when compiler sees typedef on the next line it finds a name collision, because typedef is not the same as struct declaration in C++.
Comment: Not quite; in `list* next;` "list" is undeclared.  (C++ lets you refer to "struct list" as "list"; C doesn't.)  You want either `struct list { item_type item; struct list *next; };` (and then refer to the type as "struct list") or `typedef struct list { item_type item; struct list *next; } list;`.
Comment: @Keith -- `list* next;` is declared (compiler would have failed otherwise); inside class body all names (not only class name) are visible. What you probably wanted to say is that `list` is incomplete at that point, which is fine, it doesn't prevent compiler to take a pointer on incomplete type.
Comment: In C++, the declaration of `struct list`, even as an incomplete type, does let you refer to the type as `list`.  In C, it doesn't -- which is why C code commonly uses typedefs for struct types.  (My own preference is to omit the typedef and just refer to the type as `struct list`.)
Answer: Since what you're doing is really defining a struct and then creating an alias with the typedef I think it's more readable to do this in the C case:
typedef struct list_ {
    item_type item;
    struct list_* next;
} list;

Comment: Why do you use different identifiers for the tag (list_) and the typedef name (list)?  Since struct tags are in a different namespace (in the C sense of the term), it's perfectly legal to write `typedef struct list { ... } list;`; then "struct list" and "list" are two different names for the same type.
Comment: @Keith you're right, I guess I just prefer this style because it gives the impression that the user shouldn't make use of `struct list_`, but should go through the `typedef`, mainly for consistency.
Answer: Use the following code
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef int item_type;

struct list{
    item_type item;
    list* next;
};

void insert_list(list **l, item_type x){
    list *p;
    p = (list*)malloc(sizeof(list));
    p->item = x;
    p->next = *l;
    *l = p;
}

int main(){
    return 0;
}

Comment: Why `malloc`? This is supposed to be C++ NOT C!
Answer: What C only warns against, C++ is likely to consider an error.
It's a programming cultural thing.  C was very forgiving in not enforcing it's typing system.  C++ is still quite forgiving, but you're doing something in C that even C++ won't forgive.
When you malloc that block of memory, cast it to a pointer to a list.  That will covert the address (pointer) to a pointer of the right type.
Without that cast, you could have malloc'd the size of anything, and there's no telling if it was meant to be referenced by a list pointer or some other pointer.
Comment: In C, casting the result of `malloc()` is more likely to create or conceal errors than to solve them.  Just assign the result to your pointer object; the implicit conversion from void* to any pointer-to-object type will take care of the rest.  Use `p = malloc(sizeof *p);` to ensure that all the sizes and types are consistent and correct.  (This is specific to C; for C++, you probably shouldn't be using `malloc()` anyway.)


Yii2 is it possible to toggle onmotion/yii2-widget-apexcharts series by default?

Question: I am using onmotion/yii2-widget-apexcharts. In my chart I have series with ~ 20 data sets but I don't want show them all after page loads (like it's toggled off), instead it would be nice if chart could show only selected series names with option toggle those who are hidden. Primary need is to toggle all series off when page loads, perfect need is toggle off selected series. Can anyone help me with this?
Answer: You can disable specific series_name by adding JS code to the widget initialization. id is generated, so it can help to specify it explicitly.
<?= ApexchartsWidget::widget([
    ...merge your existing code...,
    'chartOptions' => [
        'chart' => [
            'id' => 'your_chart_id',
        ],
    ],
    'js' => new JsExpression("ApexCharts.exec('your_chart_id', 'hideSeries', 'series_name')"),
]); ?>

Comment: first of all thank you for your response. I'm not sure where can I add this code. Mby if you have a little time for me to tell more how to add this piece of code?
Comment: I've updated the sample code. Open your view, look for `ApexchartsWidget` and merge my code there.
Comment: no, i have this error "Setting unknown property: onmotion\apexcharts\ApexchartsWidget::js"
Comment: I just now found your pull request on that widget and now it works with single series. Is it possible to hide multiple series? If yes, could you please tell me how?


Using S3 instead of MinIO for video management

Question: I am relatively new to AWS so I wanted to reach out to the gurus in the community to get some advice on architecture, and I thought I would share my thoughts.
I am building out cloud infrastructure for a Video Management Software for a Body Worn Camera system. We are talking lots of video footage being uploaded to the cloud.
So my initial thoughts are:

EC2 8 vCPUs, 32 GB RAM, 512 GB storage, Debian (allocated Elastic IP)
RDS for PostgreSQL
S3 for storage

Now connecting the EC2 to the S3 seems to be done via IAM role, so I have done that and created an IAM role called "full_s3_access_from_ec2", now the S3 is available to the EC2 through AWS CLI e.g.
<code>aws s3 ls s3://tactical-zepcam-s3-storage
</code>
The issue I have is that the application storage system was designed and provisioned for MinIO and expects an https end point and username and password, so I am not sure that using AWS CLI is an option.
I thought to overcome my issues with application access and authentication from EC2 to S3 I could use the AWS API Gateway, I just wanted to check with the community if I am overthinking this and if there is a better way?
If I did go down the route of configuring an API, which one should I use "Web Socket API" or "HTTP API"?
Answer: We have successfully setup an EC2 connect to an s3 bucket in AWS. You need to create an access key to interact with the s3 API:
The url has the form https://s3.eu-west-3.amazonaws.com
Where eu-west-3 is your region.
And you need an user with the AmazonS3FullAccess permissions
You do not need the AWS API Gateway Console
Comment: Generally EC2 instances use an instance role to provide permissions such as S3 permissions, rather than setting up an access key. It might be that this specialized use case requires use of an access key. Perhaps you could add a bit more detail to your answer to help others who try this in future, it's fairly high level right now.
Comment: Thanks Tim, the main issue I had was I needed to access S3 from the EC2 over HTTPS and I figured out how to do that with the access key, so basically solved my own issue. My confusion originally was that I only seemed to be able to access the S3 from the EC2 via the AWS CLI tooling. Thanks for you comment though it is much appreciated.


Alternative Analysis for Non-Functioning Secondary Dominants

Question: I've been reading though my Pocket Music Theory book and in the chapter on secondary dominants there is a concept that confuses me. The book says there can be non-functioning secondary dominants that are secondary dominants that don't resolve. While I understand the concept, it seems like it is a stretch to call it a secondary dominant since it doesn't quite function as one. The examples of non-functioning secondary dominants and the analysis of them from the book are as follows:

C     E7     F    C
I     V7/vi  IV   I

C     D7     F    C
I     V7/V   IV   I

C     C7     Eb      F     G     C
I     V7/IV  bIII    IV    V     I

Is there a different way to analyse these progressions that does not involve using non-function secondary dominants?
Comment: Your first example looks pretty darn functional to me: E7-F can be analyzed as V7/vi to VI/vi, the chords of a deceptive cadence.
Answer: I agree, these are somewhat dubious designations, but there's a possible justification for looking at them more or less as analyzed in your example, in increasing order of dubiousness.
The first example is actually just a deceptive resolution of the secondary dominant, akin to a primary V7 going to vi. V7/vi in C major is an E7 chord that wants to go to A minor—that would be the i if we were transposing to the key of the vi chord. Instead the chord resolves to F major, which would be the deceptive resolution to VI in the same key, or VI/vi in the actual C Major key. F major and A minor triads share two notes, A and C, and this is part of why any deceptive progression works, two of the three notes in the target chord are the same.
The third example is a bit funkier, but it's worth noting that the target chord of the V7/IV does come, it's just delayed by the bIII on the way. Delaying the resolution of a secondary dominant with a brief interjection is actually pretty common, although bIII is a fairly uncommon chord to throw into the mix. It essentially functions like a bVII/IV, which is a fairly common resolution in rock/pop/jazz harmony. So I think I might call the progression I — V7/IV — bVII/IV — IV, etc.
I'm still thinking about the second example. I'll edit my answer after I've thought about it a bit more.
EDIT:
OK, having meditated on it a bit, I find the second example to be the least effectively understood as having anything to do with secondary dominants. I hear this more as a really nice example of linear harmony—parsimonious connections between various triadic structures largely by means of stepwise connections combined with common tones. The move from C to D7 is glued together by the common tone C. The move from D7 to F is particularly gorgeous, and—if we temporarily ignore the seventh of the D chord—is common enough to 19th-century music that it has a name, the so-called "chromatic mediant." Any time you have two major chords (or two minor chords) that have roots a third apart, you have a chromatic mediant relationship. D major to F major is definitely an example. There will always be: 1) a common tone, in our example A; 2) a chromatic modification, in our example F# moving to F natural; and 3) a stepwise movement, in our example the D moving to C. Moving between chords that are chromatic mediants thus has a relatively close connection in that all the notes are either the same or a step away, but they also have a freshness to them because the two chords never could belong to the same key. It's one of my favorite tonal connections. The seventh in the D7 chord in this particular example just provides further glue in that the C is a common tone with the preceding chord AND with the following chord. In terms of a Roman numeral analysis, I would not try to make up numerals for the D7, I'd just call it linear harmony that gets to IV via the chromatic mediant relationship, and then moves plagally to I. Does any of that make sense? I can edit for clarity if not.
Comment: The second progression is the dominant (sorry for the pun!) chord sequence in the Beatles' "Eight days a week". The "chromatic mediant" was a favourite device of theirs.
Comment: Chords whose roots are a third apart are related (because they share chord tones, and if you include tensions then the one chord swallows another) and often used as substitutes for eachother: Am as a substitute for Cmaj. Em as a substitute for Cmaj. (But not Am as a substitute for Em). so, often the secondary dominant of one is used to resolve to another.
Answer: You might say these chords do resolve to their respective tonic, but not directly or in a hidden form.

    C     E7     F ≈ Am6/F      C
    I     V7/VI      vi         I

    C     D7     F ≈ G7sus46/F  C
    I     V7/V       V7         I

    C     C7     (E♭)      F    G    C
    I     V7/IV  (passing) IV   V    I

But alternatively, you might just say it's a dominant that's deliberately not resolved "correctly", to disrupt the listener's expectation.
Answer: Following Pat Muchmore's answer, that bIII could be construed as coming from the parallel key (of C minor)rather than being found in C major itself.This idea features in some pop songs, and can technically be explained away as such.
The first example resolves from a listener's point of view in that the E contains notes that are as close as possible to the target (F) as you can go. Normal resolution relies on this concept with V7 > I.But with all the notes - rather like a tritone substitution, which drops a semitone to the target, whereas E rises a semitone.
Answer: Chords don't resolve, notes do.
Don't worry about trying to make each chord a full dominant 7th of the next one.  Look for melodic tensions and resolutions.  Look in particular for tritones - the main tension element of a dominant-tonic type progression.
There's a tritone between the G# and D in the E7 chord.  Standard resolution would be to the A and C in A minor. But those notes are also in F major - so that's a good place to go too!  (Don't call it Am6 - that would include an E, and it doesn't!  It would kill the strong C, E, F bass melody, another element that drives the progression along.)


How random is a random walker on a graph

Question: First of all, I should mention that I'm not a mathematician but I'm coming from a biological background.
Last months I'm trying to understand the basic ideas under the random walker on a graph and some basics of Markov chains.
For simplicity's sake, let's say that we release a random walker on the following transition matrix that describes a weighted graph. Our final target could be the calculation of the edge relevance based on the times that a random walker visited each specific edge of the graph. Let's, for now, don't stay on that calculation of the edge relevance (could be calculated using some Markov chains properties).
So the first thing that we should do is to run the random walker on that transition matrix. The thing that concerns me know, is how much random this walk is gonna be? Will the decision of the random walker - on what his next step is going to be - be, affected by the weights of the graph (values in the transition matrix)?
If yes, why we call it random? I mean, I would expect a random walker to have equal probabilities to move from one node to the other and not be based on pre-specified probabilities like the initial probability distribution described by the transmission matrix.
Doesn't this initial distribution destroy the randomness?
<code>    A   C   E   F   D   B
A 0.2 0.1 0.1 0.1 0.2 0.3
C 0.0 0.4 0.2 0.1 0.1 0.2
E 0.2 0.1 0.1 0.2 0.2 0.2
F 0.3 0.4 0.0 0.0 0.1 0.2
D 0.0 0.0 0.0 0.0 1.0 0.0
B 0.0 0.0 0.0 0.0 0.0 1.0
</code>
If no, then the randomness is saved, but then why do we need those weights to describe the relationships between the nodes of the graph? Just for the Markov chain calculations?
My question might look like very noobish and that's why I'm asking to excuse me. Of course, any intuitive explanation or external resources that you think will help me understand it more clearly that subject, are welcomed.
Comment: Randomness does not imply uniformity, just some level of uncontrolled statistical variation. The weights just imply some bias, very similar to flipping an unfair coin or anything.
Comment: The walker does take into account the bias in how they select sites to visit. As for bias in general, this is everywhere in probability, so I don't really know what to tell you.
Comment: So this so-called bias you said, should be taken into account by the random walker. Shouldn't it? (Mr. Ripstein just answered). Also, I liked your first sentence about uniformity. Can you provide me with some more info on that subject? Because I cannot understand how something biased, could be called random too.
Comment: Ok. I'll take some time to think about it. Thank you.
Answer: I think you are confusing the tool and the target.
A random walk is an algorithm used to detect most probable flows or behaviours in a situation in one or more dimensions. But the final output may not be random at all.
Think about it as a disoriented person lost in the forest that starts to walk. He is less likely to go up the hills (it's tiring and not probable to find towns there) and more likely to go down, but since he is disoriented he sometimes may do things which are counterintuitive. His walk is random in the sense that, if he starts again, he would do something completely different. However, over one million tries, surely he finishes most of the times in one of the towns, and only a few times in top of a hill
And, coming back again to your example, B and D are sinks (you cannot leave those states), so the walker will end there eventually. The algorithm may help you to see with which probability it end there

<code>Example of pseudo-algorithm, being M the matrix of probabilities

for i in 1 to n_runs:
   S=initial_state(i) # returns a random initial state A/B/C/D/E/F
   for j=1...n_changes_state
        S=change_state(S,M) # returns the new state using M
   end for
   final_state(i)=S # vector of final outputs for each run
end for
</code>
And with this you will have after n_runs, a vector of final states, where most likely all are Bs and Ds if n_changes_state is big enough
Comment: It is taking by rolling a 10 faces dice where 3 faces say B, 2 are As and 2 are Ds and finally 1C, 1E and 1F
Comment: So, can we say that the decision of the next step is affected by the probabilities stored in the transmission matrix or it is random at all? Could you describe the process with a pseudo-algorithm?
Comment: In a Markov chain or a random walk, the next step is determined only by your current state and not by the way you reached it.

That means that, for your example, the probability to go from A to B is 0.3 no matter where you were before
Comment: Yeah, I know that. You probably misunderstood the question or I didn't ask it properly. Ok. We agree that 0.3 is the probability to go from A to B is 0.3 but how the decision to go from A to B is taken? Is it taken randomly or probabilistically based? In other words and according to your pseudo-algorithm, how the change_state() function decides the next step? Just picks one randomly with non zero probability or not?
Comment: Wow! Thank you very much. That was the answer, that cleared everything. So. It is a biased dice. Now how can we call something bias and random at the same time I cannot fully understand, as I wrote to the @lan's comment.


jQuery - blur, but not if click on button

Question: I have a search box that is small in normal state.
on focus it expands, and a submit button shows up.  This was done to save space.
now, on blur, the search box shrinks again, and the submit button goes away.
Problem is, the click to the submit button IS the blur (which happens before it gets submit) there by making the submit button a 'race' to click it in the right spot.
I don't want to use an interval/timer... over kill.
Ideally, i just want the blur to not happen if focus is on the submit button.
$('#searchtext').focus(function() {
    $('#searchtext').stop().animate({'width':'175px'} , 800);
    $('#ctrl_search').fadeIn('slow');
});
$('#searchtext').blur(function() {
    $('#searchtext').stop().animate({'width':'85px'} , 800);
    $('#ctrl_search').fadeOut('slow');
});

searchtext is the input
ctrl_search is the submit button
I want to change
$('#searchtext').blur(function() {

to something like:
$('#searchtext').blur.not$('#ctrl_search').focus())(function() {

but that obviously doesn't work. lol
Can something like this be fanageled?
help!
Comment: Just because i blur, i don't necessarily want to submit a search, so i don't think that's a valid use case unless i'm understandng you wrong??? Thanks though.
Comment: See there could help: http://stackoverflow.com/a/121708/1414562
Comment: Interval/timer isn't overkill, it's a good solution in this case.
Answer: I don't how know complex or simple of a solution you are looking for but you could simply wrap your blur animation inside an if statement to check and see if text has been entered (text would mean someone wants to actually search).
$('#searchtext').focus(function() {
    $('#searchtext').stop().animate({'width':'175px'} , 800);
    $('#ctrl_search').fadeIn('slow');
});
$('#searchtext').blur(function() {
    //Check to see if text is blank (after removing whitespace), if so they must not want to search
    if($('#searchtext').val().trim() == ''){
       $('#searchtext').stop().animate({'width':'85px'} , 800);
       $('#ctrl_search').fadeOut('slow');
    }
});

Fiddle here: http://jsfiddle.net/sChkC/1/

Of course this is the simplest form, you could trim or use some
  other fancy function to remove whitespace in the case the user enters
  a space and clicks out accidentally.

Added this in there for good measure.
Comment: I think this could be a good starting point. The problem is that if the user just types something and changes their mind, I would like for the box to shrink back to 85px. Perhaps this is where the timer factor comes in.

ie8 doesn't seem to like this trim command either.

Webpage error details

Message: Object doesn't support this property or method

pointing to the new if line num
Comment: this solved it:
http://stackoverflow.com/questions/4232190/are-text-trim-really-cant-work-in-ie8


Como desenhar 3 círculos em CSS com 1 div

Question: Gostaria de fazer 3 icones em CSS, porém não quero fazer com vários div, com div filhos, etc.
Gostaria algo clean, como

Consegui fazendo com vários div (um para borda) e um div para cada círculo, no caso ficou até com 4 divs, para fazer o Status=3.
No JsFiddler como eu fiz
https://jsfiddle.net/dorathoto/rchdwy1f/11/
Meu código:

Resultado final:
Status=1

Status=2

Status=3
Answer: Pode ser que existam outras formas de fazer, mas a mais simples que eu achei foi usando uma sequência de 3 radial-gradient.
Repare que eu tenho apenas uma div, porém a div que tem 1 bola tem 1 radial-gradiente no background-image, a que tem 2 bolas tem 2 radial, e a que tem 3 bolas vai ter 3 radial-gradiente dentro do background-image.
É simples de configura dentro do próprio gradiente vc contra o X/Y para posicionar as bolinhas e as cores... Link com a documentação da Mozilla sobre radial-gradiente: https://developer.mozilla.org/pt-BR/docs/Web/CSS/radial-gradient

body {
    background-color: black;
}

[class^="bola"] {
    width: 44px;
    height: 20px;
    border-radius: 6px;
    background-color: #000;
    border: 1px solid #999;
    background-repeat: no-repeat;
    margin-bottom: 1rem;
}
.bolaCinza {
    background-image: radial-gradient(circle at 12px 10px, #fff 0px, #fff 5px, #000 6px);
}
.bolaAzul {
    background-image:   radial-gradient(circle at 12px 10px, #fff 0px, #fff 5px, transparent 6px),
                        radial-gradient(circle at 22px 10px, #00f 0px, #00f 5px, #000 6px);
}
.bolaRed {
    background-image:   radial-gradient(circle at 12px 10px, #fff 0px, #fff 5px, transparent 6px),
                        radial-gradient(circle at 22px 10px, #00f 0px, #00f 5px, transparent 6px),
                        radial-gradient(circle at 32px 10px, #f00 0px, #f00 5px, #000 6px);
}

Comment: Basicamente essa técnica: https://pt.stackoverflow.com/questions/101323/
Comment: No IE aqui ficou da forma que coloquei na resposta. :(
Comment: @Bacco essa pergunta é um clássico ;)
Comment: @Sam ai renderizou na tela bem feio mesmo... não sei dizer pq... mas no geral gradiente não denderiza bem no browser mesmo, principalmente na diagonal e em círculos, por isso eu deixei 1px de "transição" na cor do gradiente entre uma cor e outra