How to design the label for tensorflow's ctc loss layer

Question: I just started using ctc loss layer in tensorflow(r1.0) and got a little bit confused with the "labels" input
In tensorflow's API document, it says

labels: An int32 SparseTensor. labels.indices[i, :] == [b, t] means labels.values[i] stores the id for (batch b, time t). labels.values[i] must take on values in [0, num_labels)

Is [b,t] and values[i] mean there is a label "values[i]" at "t" of sequence "b" in the batch?
It says value must be in [0,num_labels), but for a sparse tensor, almost everywhere is 0 excepted for some specified places, so I don't really know how should the sparse tensor for ctc be like
And for example, if I have a short video of hand gesture, and it has a label "1",should I label the output of all timesteps as "1", or only label the last timestep as "1" and take other as "blank"?

thanks!
Answer: To address your questions:
1. The notation in the documentation here seems a bit misleading, as the output label index t need not be the same as the input time slice, it's simply the index to the output sequence. A different letter could be used because the input and output sequences are not explicitly aligned. Otherwise, your assertion seems correct. I give an example below.

Zero is a valid class in your sequence output label. The so-called blank label in TensorFlow's CTC implementation is the last (largest) class, which should probably not be in your ground truth labels anyhow. So if you were writing a binary sequence classifier, you'd have three classes, 0 (say "off"), 1 ("on") and 2 ("blank" output of CTC).
CTC Loss is for labeling sequence input with sequence output. If you only have
a single class label output for the sequence input, you're probably better off using a softmax cross entropy loss on the output of the last time step of the RNN cell.

If you do end up using CTC loss, you can see how I've constructed the training sequence through a reader here: How to generate/read sparse sequence labels for CTC loss within Tensorflow?.
As an example, after I batch two examples that have label sequences [44, 45, 26, 45, 46, 44, 30, 44] and [5,  8, 17,  4, 18, 19, 14, 17, 12], respectively, I get the following result from evaluating the (batched) SparseTensor:
SparseTensorValue(indices=array([[0, 0],
       [0, 1],
       [0, 2],
       [0, 3],
       [0, 4],
       [0, 5],
       [0, 6],
       [0, 7],
       [1, 0],
       [1, 1],
       [1, 2],
       [1, 3],
       [1, 4],
       [1, 5],
       [1, 6],
       [1, 7],
       [1, 8]]), values=array([44, 45, 26, 45, 46, 44, 30, 44,  5,  8, 17,  4, 18, 19, 14, 17, 12], dtype=int32), dense_shape=array([2, 9]))

Notice how the rows of the indices in the sparse tensor value correspond to the batch number and the columns correspond to the sequence index for that particular label. The values themselves are the sequence label classes. The rank is 2 and the size of the last dimension (nine in this case) is the length of the longest sequence.
Comment: sorry for the late replying, so "t" is just the order of the specific label, no matter how long the respective input sequence will be last ?
Comment: I believe that's right. However, whereas the model can only produce at most one output per input slice, your label sequence shouldn't be longer than the input length. (With label merging, it could also produce _fewer_ outputs than inputs.)


#states attribute of FORM API?

Question: Currently I am using states attribute in my form to show/hide a drodown having days as its value on selecting a parent dropdown value. My working scenerio is that I want to show a dropdown having 31 days if january,march,may.... is selected and to show another dropdown having 30 days as its value when febraury,april,june .... is selected , can some one suggest me how its could be achieved because in usage of #states attribute only one value could be selected.
function sandbox_ajax_dropdown($form, &$form_state) {
<code>$options = array(1 => t('1 月'), 2 => t('2 月'), 3 => t('3 月'), 4 => t('4 月'), 5 => t('5 月'), 6 => t('6 月'), 7 => t('7 月'), 8 => t('8 月'), 9 => t('9 月'), 10 => t('10 月'), 11 => t('11 月'), 12 => t('12 月'),);

$form['state_list'] = array(
    '#type' => 'select',
    '#title' => 'Choose A Day First',
    '#options' => $options
);
$form['illinois_cities'] = array(
    '#type' => 'select',
    '#description' => t("Thirty-One Days."),
    '#states' => array(
        'visible' => array(
            ':input[name="state_list"]' => array('value' => '5'),
        ),
    ),
    '#options' => array(1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
        13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
        25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30'), 31 => t('31')),
);

$form['ohio_cities'] = array(
    '#type' => 'select',
    '#description' => t("Thirty Days."),
    '#states' => array(
        'visible' => array(
            ':input[name="state_list"]' => array('value' => '1', '2'),
        ),
    ),
    '#options' => array(1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
        13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
        25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30'))
);

return $form;
</code>
}
Answer: Found it !!!!!!!!
The solution is below highlighted.
function modulename_form_alter(&$form, &$form_state, $form_id) {
<code>if ($form_id == 'webform_client_form_14209') {

    $form['submitted']['dob'] = array(
        '#type' => 'fieldset',
        '#title' => t('出生日期'),
        '#weight' => 4,
    );

    $form['submitted']['dob']['dob_month'] = array(
        '#type' => 'select',
        '#title' => t('月'),
        '#options' => array(
            1 => t('1 月'), 2 => t('2 月'), 3 => t('3 月'), 4 => t('4 月'), 5 => t('5 月'), 6 => t('6 月'), 7 => t('7 月'), 8 => t('8 月'), 9 => t('9 月'), 10 => t('10 月'), 11 => t('11 月'), 12 => t('12 月'),
        ),
        '#description' => t('Select month.'),
        '#required' => true,
    );

    /* for 31 days */
    $form['submitted']['dob']['dob_day_31'] = array(
        '#type' => 'select',
        '#title' => t('日'),
        '#options' => array(1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
            13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
            25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30'), 31 => t('31')),
        '#description' => t("Thirty-One Days."),
        '#required' => true,
        ***'#states' => array(
            'visible' => array(
                ':input[name="submitted[dob][dob_month]"]' => array(
                    array('value' => '1'),
                    array('value' => '3'),
                    array('value' => '5'),
                    array('value' => '7'),
                    array('value' => '8'),
                    array('value' => '10'),
                    array('value' => '12'),
                ),
            ),***
        ),
    );

    /* for 30 days */
    $form['submitted']['dob']['dob_day_30'] = array(
        '#type' => 'select',
        '#title' => t('日'),
        '#options' => array(1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
            13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
            25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30')),
        '#description' => t("Thirty Days."),
        '#required' => true,
        '#states' => array(
            'visible' => array(
                ':input[name="submitted[dob][dob_month]"]' => array(
                    array('value' => '2'),
                    array('value' => '4'),
                    array('value' => '6'),
                    array('value' => '9'),
                    array('value' => '11'),
                ),
            ),
        ),
    );

    $form['submitted']['dob']['dob_year'] = array(
        '#type' => 'select',
        '#title' => t('年'),
        '#options' => array(1 => t('2013'), 2 => t('2012'), 3 => t('2011'), 4 => t('2010'), 5 => t('2009'), 6 => t('2008'), 7 => t('2007'), 8 => t('2006'),
            9 => t('2005'), 10 => t('2004'), 11 => t('2003'), 12 => t('2002'), 13 => t('2001'), 14 => t('2000'), 15 => t('1999'), 16 => t('1998'), 17 => t('1997'),
            18 => t('1996'), 19 => t('1995'), 20 => t('1994'), 21 => t('1993'), 22 => t('1992'), 23 => t('1991'), 24 => t('1990'), 25 => t('1989'), 26 => t('1988'),
            27 => t('1987'), 28 => t('1986'), 29 => t('1985'), 30 => t('1984'), 31 => t('1983'),
        ),
        '#description' => t('Select year.'),
        '#required' => true,
    );
}
return $form;
</code>
}

#states attribute of FORM API?


Proving that $X$ is bijective to $\{ a, b \} \times X$ if $X$ is an infinite set

Question: I'm trying to prove that for any infinite set $X$, not assumed to be in bijection with $\mathbb N$ or with $\mathbb R$, $X$ is bijective with $\{ a, b \} \times X$.
I'm trying to use the Cantor Bernstein theorem, but I'm unable to prove that there exists an injection $\{ a, b \} \times X \rightarrow X$. I've seen for example here an example of an injection for sets of cardinality $| \mathbb N |$, and here for sets of cardinalty $| \mathbb R |$, but these don't seem to generalize to arbitrarily infinite sets.
I've also seen that the situation is more complicated when trying to prove that $X \times X$ injects into $X$, as seen from the second answer here, and that this result relies on the axiom of choice as the first answer points out.
To be honest, I have not gone through the proof given in this answer as it seems tedious. I would expect however that there is a shorter proof with a different idea for this case, as it is only a cartesian product with a finite set, after all.
So, does $|\{ a,b \} \times X| = |X|$ rely on the axiom of choice? And, more importantly, is there a different proof than that given in the answer to the question about $|X \times X| = |X|$?
Comment: From the linked post "*If choice fails, however, this may no longer be the case: e.g. it is consistent with ZF that there is a set $X$ which is infinite but cannot be partitioned into two infinite sets.*"  If we were to have a bijection $f$ then $X$ could have been partitioned into two sets... the image of $f(\{(a,x)~:~x\in X\})$ and the image of $f(\{(b,x)~:~x\in X\})$, so the same set that worked as a counterexample if choice fails in the other problem also works as a counterexample here if choice fails.
Comment: Since the claim is false when working in the context where we are without the axiom of choice, it follows that any correct proof for the usual context will require invoking the axiom of choice or an equivalent assumption.
Comment: The (ZFC) proof I was given for "$|X \times X| = |X|$" was by choosing an initial ordinal $\alpha$ in bijection with $X$, constructing a slightly complicated well-ordering on the set $\alpha \times \alpha$ and showing it's order-isomorphic to $\alpha$. This proof can be simplified a bit in this special case - it's not so hard to show that if $\alpha$ is any limit ordinal, then $2\alpha  = \alpha$ in the usual ordinal arithmetic, from which the result follows. This does use full AC, which is not technically necessary - but not using it won't necessarily make the proof simpler :)
Comment: @IzaakvanDongen I'd be very happy with an answer using the theorem that any set can be well ordered or using Zorn's lemma (whichever is more convenient with you), I'm familiar with both of them, I've used Zorn's lemma in particular in a lot of proofs. I just haven't used ordinals before.
Comment: @IzaakvanDongen I haven't worked with ordinals and I'm unable to follow your proof as it is now. Could you maybe give an answer with the ideas, maybe a bit more elaborated and without the concept of ordinals (if possible)?
Comment: Does this post help: https://math.stackexchange.com/questions/1896440/cardinality-of-cartesian-product-of-an-infinite-set-with-n
Comment: Yeah, I wasn't sure you'd have met ordinals, which is why I left a comment, not an answer! Do you know what "Zorn's lemma" and "well-orders" are? It's kind of unavoidable to use some machinery like this, because general sets that you have no information about don't give you a foothold to work with in ZFC without doing something like well-ordering them. I'd be happy to write an answer based only on the fact that $X$ can be well-ordered, or using Zorn's lemma, but the proofs and definitions underlying these facts are frankly much more work to understand than the eventual argument!
Comment: I'm sorry not to be more helpful. As far as I know, cardinal arithmetic is fairly non-trivial set theory, that you need a number of theorems and lemmas to start proving things about. If you really desperately want to understand why $|X| + |X| = |X|$, I think the best way forward to to find an introductory set theory textbook or set of lecture notes and work through it. The good news is that set theory doesn't have many prerequisites! But it is a very abstract subject. The other option is to wait until you have the chance to take a set theory course (but maybe this isn't an option for you.)
Comment: Actually it's not too difficult to show this statement is equivalent to the statement that if $A,B$ are infinite and $B$ is equinumerous to $A$ then $A$ union $B$ is equinumerous to $A$. My proof of the latter uses $A$ x $A$ is equinumerous to $A$ for infinite $A$. I suspect there are no shortcuts.
Comment: I'm sure you know how to prove it if $X=\mathbb N$ or if $X$ is countably infinite. Using the axiom of choice (e.g. in the form of Zorn's lemma) you can prove that any infinite set $X$ can be partitioned into countably infinite sets, and that $X$ is bijective to $Y\times\mathbb N$ for some set $Y$.
Answer: Here is an argument using the well-ordering theorem, and no ordinals, which I find fairly intuitive (although some of this intuition probably does come from intuition about ordinal arithmetic). I spell a lot of facts about well-orders out, so if you know those facts already then there will be sentences and paragraphs that you can skip.

Let $\le$ be a well-ordering on $X$.
Firstly, we can assume that $X$ has no largest element. This is because any well-ordering can be (uniquely) decomposed as $X' + F$, where $F$ is finite and $X'$ has no largest element. This notation means that $X'$ and $F$ are well-ordered sets, and $X$ looks like "a copy of $X'$ followed by a copy of $F$". (Proof - if there is a largest element, let $x$ be the smallest element such that $\{y \in X : y \ge x\}$ is finite, and take $X' = \{y \in x : y < x\}$, $F = \{y \in X : y \ge x\}$).
So replace $X$ by the well-ordered set $F + X'$ if needs be (which is obviously the same size as $X' + F$). $X'$ must be non-empty, because $X$ was assumed to be infinite, so $F + X'$ has no largest element, because $X'$ has no largest element.
Now we need to show that $\{0, 1\} \times X$ bijects with $X$.
Note that $\{0, 1\} \times X$ is well-ordered by the colexicographic order induced by the usual order on $\{0, 1\}$ and the order $\le$ on $X$. If you think of $X$ as a discrete line of points, this order looks like what you get if you replace every point of $X$ by a copy of $\{0, 1\}$, or in other words, two points. Now I claim that this ordering is actually isomorphic to $(X, \le)$. Hopefully if you think about $X = \Bbb N$, for example, this is believable - in that case, having countably many copies of $\{0, 1\}$ next to each other is obviously the same order type as $\Bbb N$.
In fact, I think the nicest way to prove this conceptually is to prove that whenever $X$ is a well-ordering with no greatest element, then we can decompose $X$ as $\Bbb N \times X''$ for some well-ordered set $X''$ (think of this as saying that $X$ is a number of copies of $\Bbb N$).

Proof: take $X''$ to be the subset of elements of $X$ that have no direct predecessor in the ordering on $X$. Then the map $\Bbb N \times X'' \to X$ which sends $(n, x)$ to the $n$th successor of $x$ is an order isomorphism. (Take $0$ to be in $\Bbb N$ here).
It's well-defined, since $X$ has no largest elements.
It's strictly order-preserving, because if $x_1 < x_2$ have no direct predecessors, then the $n$th successor of $x_1$ is always less than $x_2$, by induction on $n$, and clearly if $n < m$ then the $n$th successor of $x_1$ is less than the $m$th successor of $x_1$. Hence it is also injective.
It's surjective, because any element of $X$ has only finitely many direct predecessors (otherwise the infinite sequence of direct predecessors would be a set with no smallest element).

You can think of this as some sort of transfinite exhaustion process - any well-order with no largest element starts with a copy of $\Bbb N$. Then after that, there is another copy, and another copy, ...
Given this, it's fairly clear that we can just use the bijection between $\{0, 1\} \times \Bbb N$ and $\Bbb N$ in each copy of $\Bbb N$. More formally, we have a chain of isomorphisms of ordered sets:
\begin{equation*}
\{0, 1\} \times X \cong \{0, 1\} \times (\Bbb N \times X'') \cong (\{0, 1\} \times \Bbb N) \times X'' \cong \Bbb N \times X'' \cong X.
\end{equation*}
So certainly $\{0, 1\} \times X$ and $X$ biject. (Here I used the fact that products of isomorphic total orders are isomorphic, and taking products of total orders is associative up to isomorphism, which are both straightforward.)

You could also have argued directly about cardinalities at the end without even worrying about the ordering anymore:
\begin{equation*}
|\{0, 1\} \times X| = |\{0, 1\} \times (\Bbb N \times X'')| = |(\{0, 1\} \times \Bbb N) \times X''| = |\Bbb N \times X''| = |X|.
\end{equation*}
This argument uses similar facts, but you only care about bijections, so they're very slightly more straightforward to prove.
As I mentioned in the comments, this argument is basically a translation of the following:

Choose some limit ordinal $\alpha$ in bijection with $X$. By Euclidean division, we can write $\alpha = \omega \beta$. Hence $\{0, 1\} \times X$ bijects with $2 \cdot \alpha = 2 \cdot \omega \beta = \omega \beta = \alpha$.
Alternatively, one can show by induction that whenever $\alpha$ is a limit ordinal and $\gamma < \alpha$, we have $2 \gamma < \alpha$, from which it follows that $2\alpha \le \alpha$.

Proof: If we know $2\gamma < \alpha$, then $2(\gamma + 1) = 2\gamma + 2 < \alpha$ because $\alpha$ is a limit. Also, if $\gamma < \alpha$ is a limit, then by induction on $\alpha$, we know that $2\delta < \gamma$ for all $\delta < \gamma$. Hence $2\gamma \le \gamma < \alpha$.

To prove the full result that $|X \times X| = |X|$ the argument is quite similar in spirit - you just define a well-ordering on the set $X \times X$ which makes it order-isomorphic to $X$. In that case, the order theory is more important, and so is the specific well-ordering that you choose on $X$ - you have to pick a well-ordering such that for every $x \in X$, the set $\{y \in X: y < x\}$ does not biject with $X$ ("an initial ordinal"). We may assume this because if there is an $x$ violating this, then we can consider the least such $x$ and just replace $X$ by the set $\{y \in X : y < x\}$, which bijects with $X$, but no proper initial segment of which bijects with $X$. This is a much, much stronger property than having no largest element.
Working with this well-ordering can also give an argument that shows $\{0, 1\} \times X$ bijects with $X$, by showing that each of its initial segments embeds into $X$. This does requires a fair bit more order theory, which I think is best understood by learning about ordinals. At that point you also might as well do the full proof for $X \times X$, after which you can easily apply Schröder-Bernstein by injecting $\{0, 1\} \times X$ into $X \times X$.
You can also use Zorn's lemma in various clever, but I think less enlightening ways. For example, the result follows from this already.


Implementing 2way Fixed Effects

Question: My objective is to assess the impact on BMI following the establishment of a new donut shop in the neighborhood, particularly considering that the donut shop commenced operations in 2017.
Researchers have proposed two-way fixed effects (TWFE) models of this type to analyze such events:
$y_{i,t}$ = $\beta_0$ + $\beta_1$$Z_i$ + $\beta_2$$t$ + $\beta_3$$Z_{it}$
Here
$y$ is outcome.
$Z_i$ is the post/pre binary indicator variable . $Z=1$ indicates post period , $Z=0$ indicates pre period.
$t$ is the time variable in years , values : 2001, 2002, 2003...so on...
Here $\beta_3$ is the causal parameter of interest.
When I implement this model
 ID         BMI    Year     Z
  1        10.12  2003      0
  2        19.78  2003      0
  3        7.12   2004      0
  3        31.56  2005      0
  4        24.12  2006      1
  5        21.67  2007      0
  .        .      .         .
  .        .      .         .
  .        .      .         .
  .        .      .         .
  2000     11.12  2002      0
  2001     29.78  2006      1
  2002     17.12  2008      0
  2122     13.56  2009      0
  2676     42.12  2006      1
  2676     12.67  2007      0

lm(BMI ~ Year + Z + Year*Z, df)
I get NAs in my parameter estimates for interaction terms after fitting the model based on this method. Below is the reproduceable example.
###### Generate random heights and weights
heights <- rnorm(1000, mean = 170, sd = 10)  # Random heights (mean=170, sd=10)
weights <- rnorm(1000, mean = 70, sd = 5)   # Random weights (mean=70, sd=5)

###### Calculate BMI
bmi <- weights / ((heights / 100) ^ 2)

years       <- c(2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,  2019,  2020, 2021)
percentages <- c(0.1,  0.71, 0.4,  0.45, 0.58, 0.72, 0.15, 0.8,  2.35, 3.79, 3.82, 4.12, 4.59, 4.10, 5.87, 7.13, 8.67, 9.07, 16.23, 22.19, 2.28, 1.81)

Year <- sample(rep(years, round(length(years) * percentages)), 1000, replace = TRUE)

Z <- ifelse(Year > "2017",1,0)

testdf <- as.data.frame(cbind(bmi, Year,Z))

# fit linear model
lm(bmi ~ as.factor(Year) + as.factor(Z) + as.factor(Year)*as.factor(Z), testdf)

I like to learn where I am going wrong. Any suggestions are valued and much appreciated. Thanks in advance.
Comment: And you're keeping track of the subjects that do *not* visit the shop, and measuring their BMI over time?
Comment: Interesting topic. Are you assessing the individuals frequenting this donut shop over time, or are you looking at the average BMI of the residents living in the neighborhood pre-/post-opening of the donut shop?
Comment: @ThomasBilach, good question. How many times an individual visited the shop will be included in the model as a covariate. The actual dataset contains repeat observations for several subjects during pre-post period but it is very unbalanced. Some participants we have 3 observations during different years, for some participants only one.
Comment: @ThomasBilach, Hi Tomas this question has been bothering me.  The donut store was opened in 2017. Suppose I have pre 2017 BMI data from kids who never visited the donut store after it opened and post 2017 BMI data from kids who visited the donut store, does that make two way fixed effects completely useless ? because I cannot estimate the interaction term ?
Answer: The year effects are identified because you have people observed at multiple years in the data.
The post effect is identified because you have four separate post years.
The year-post effects are not identified because of perfect collinearity:
> table(testdf$Year,testdf$Z)

         0   1
  2000   1   0
  2001   4   0
  2002   4   0
  2003   8   0
  2004   7   0
  2005   7   0
  2007   6   0
  2008  28   0
  2009  34   0
  2010  44   0
  2011  39   0
  2012  52   0
  2013  42   0
  2014  50   0
  2015  72   0
  2016  78   0
  2017  99   0
  2018   0 163
  2019   0 214
  2020   0  28
  2021   0  20

You will need to have some people who are not exposed to the donut shop in the post years get these coefficients. Z cannot be a function of the year only.

Implementing 2way Fixed Effects


Can you use a Managed Identity to access an Azure Function from an Azure App Service?

Question: I have created an Azure Function that is called by a single Azure Web App I also created. I want to secure the Azure Function to only allow the Azure Web App to call it. I know how to do it by creating an App Registration and passing ClientID and Secrets. But I was wondering if I can do it by using the Azure Web App's Service Identity. That way I do not need to maintain the ClientID and Secrets.
Any advice would be appreciated.
Thank you.
Comment: Yes you can. You will have to assign the permissions manually through PowerShell though: https://joonasw.net/view/calling-your-apis-with-aad-msi-using-app-permissions.
Comment: Is the web app SPA or with backend?
Answer: As noted in the comments, you can use MSI for this.
As an alternative approach you could also lock it down on the network layer by integrating the App Service into a VNET and lock down incoming traffic on the Function to that one subnet of the App Service.


Erro de sintaxe jquery

Question: Olá estou tendo um problema quanto ao uso desse código. Quero que dentro da div com a class output mostre o seguinte código html, esse código está dentro de um for por isso usei o append, aqui está dando erro de sintaxe, vocês poderiam me ajudar?
 $('.output').append('

'+data2.display_name+'

'+data2.status+'

'+
 if(data.stream===null){+'
 on
'+} else{'+
 on
'+}+'
');

Comment: Você está tentando concatenar um `if` na *string*?
Comment: Cara, tira esse _if_ de dentro do _append_. Coloca essa sua _string_ dentro de uma variável, manipula ela fora e por último, adiciona a variável no _append_.
Comment: isso mesmo! @AndersonCarlosWoss
Answer: Não dá pra usar o if deste jeito, você precisa de um operador ternário.
Veja o exemplo abaixo. Se a condição for true será concatenado o que vem depois do ?, caso contrário, será concatenado o que vem depois de :.
Obviamente que dava pra organizar melhor o código e tratar isso fora, aliás, uma dica: dê uma organizada melhor no código.
De qualquer maneira, o problema mesmo é usar este if-else dentro da concatenação.
(data.stream === null
? '
 on
'
: '
 on
' )
+ '
';

Answer: Não podes usar if/else dentro de concatenação de strings dessa maneira.
Separa a parte do if/else e concatena só o resultado. Algo assim:
var stream = data.stream === null ?
  '
 on
' :
  '
 on
';

$('.output').append(
  '

' +
  data2.display_name + '

' +
  data2.status + '

' +
  stream +
  '
'
);

Comment: Não sabia que não podia usar if e else em concatenação, achei a sua solução de colocar em uma variável com uma condição ternária bem organizada.

Erro de sintaxe jquery


Sort a directory then find a file on lastwritetime in that dir

Question: I have a directory with lots of sub-directories which in turn have files.
The file I'm looking for mostly is in the latest modified sub-directory.
This setup is on UNC.
To find the file as quickly as possible I want to first sort the sub-directory on last modified time and then search.

Get-ChildItem | Sort lastwritetime -Descending
Get-ChildItem filename.txt -Recurse | Where-Object {$_.lastwritetime -gt "MM/dd/yyyy" -and $_.lastwritetime -lt "MM/dd/yyyy"}

I want to pass the output of 1 to 2.
Answer: Get-ChildItem | Sort LastWriteTime | Select -Last 1

will get the most recent file, you can change -Last for -First to get the oldest unmodified file.
you can also specify -File or -Directory on Get-ChildItem to make sure you get only files/dirs.
edit: after your question update.
$SearchIn = Get-ChildItem -Directory | Sort LastWriteTime | Select -Last 1
Get-ChildItem $SearchIn.FullName -Recurse | Where-Object {$_.lastwritetime -gt "MM/dd/yyyy" -and $_.lastwritetime -lt "MM/dd/yyyy"}


Information on TSQL DelBkUps parameter

Question: We are using a TSQL script to back up our database every night, and in this scrip it uses -DelBkUps 1DAYS to remove the old disk copy of the backup. However we are running out of disk space at the moment and I would like to change this to something like -DelBkUps 0.5DAYS
The documentation here http://msdn.microsoft.com/en-us/library/ms162827.aspx says that i should use time_span but gives me no examples of what is acceptable in this field.
Can some one here help me find acceptable documentation, or give me an example of how I would set the time_span to a number of hours instead of days?
Answer: The format for this is listed at the bottom of the syntax section of the web page referenced.
<time_period> ::=
number[minutes | hours | days | weeks | months]

Here are the samples.
number[minutes| hours| day| weeks| months]
Specifies the time interval used to determine if a report or backup file is old
enough to be deleted. number is an integer followed (without a space) by
a unit of time. Valid examples:
12weeks
3months
15days
If only number is specified, the default date part is weeks.

Comment: Well don't I feel silly now? I just missed that part when I read over it. I had to go back and re-read it again to catch it.

Information on TSQL DelBkUps parameter


Does Matsumura prove that finitely a generated algebra over a field is catenary?

Question: In Commutative ring theory, Matsumura states on page 30

Practically all the important Noetherian rings arising in applications are known to be catenary;

I think one of the important rings arising in algebraic geometry are finitely generated algebras (and localizations of those) over a field. However I didn't find a proof in Matsumura, that those are actually catenary.
I know that Eisenbud proves this in Commutative algebra with a view toward algebraic geometry, so I'm not looking for a reference, just curious if I missed this in Matsumura's book, or if this is really left out.
Answer: This is proven, but without drawing too much attention to it.

Theorem 17.9: Any quotient of a Cohen-Macaulay ring is universally catenary.

This appears in my copy on page 137. As a field is Cohen-Macaulay, the result follows.
Alternatively:

Theorem 31.7: For a noetherian local ring $A$, the following are equivalent:

$A$ is formally catenary
$A$ is universally catenary
$A[x]$ is catenary.

(Page 252 in my copy.) So it remains to show that $k[x]$ for a field $k$ is catenary. This is trivial as $k[x]$ is a PID.


MCMC inside Expectation Maximization

Question: I wish to optimize the following likelihood function for parameter $\Theta$:
$$p(D|\Theta)=\int_X\int_Y p(x, y, D|\Theta)dydx$$
where $X$ and $Y$ are latent variables and only $D$ is observed. I would like to use the Expectation-Maximization (EM) algorithm. If I understand it correctly, the E-step of the algorithm would be:
$$Q(\Theta|\Theta^{(t)})=\mathbb E_{X, Y|D,\Theta^{(t)}}[\log(p(X, Y, D|\Theta))]$$
However, I can only get samples from $p(X, Y|D,\Theta^{(t)})$ using Markov Chain Monte Carlo. So, I did the following:
$$Q(\Theta|\Theta^{(t)})=\mathbb E_{X, Y|D,\Theta^{(t)}}[\log(p(X, Y|D,\Theta))]+\log(p(D|\Theta))$$
Since $D$ is observed, I can calculate $\log(p(D|\Theta))$ from the data. Now here's my question: is it correct? And would you recommend any other way to do it or any other algorithm to use here?
Comment: Note: I corrected some $\Theta^{(t)}$ into $\Theta$ in the E-step.
Comment: Sounds like a case where you might apply variational inference
Comment: The decomposition you want to do might be this: $Q(\Theta|\Theta^{(t)})=\mathbb E_{X, Y|D,\Theta^{(t)}}[\log(p(D|, X, Y,\Theta)) + \log(p(X,Y|\Theta))]$, since in most cases we know the likelihood model D | X, Y and the prior for X, Y.
Answer: The proposal is apparently negating the point in running an EM algorithm since, were $p(D|\theta)$ available in an analytic form, the completion by the latent variables would not be necessary since a numerical maximisation algorithm could be called upon.
Even within the EM algorithm framework the decomposition does not help since
$$\log p(x,y|D,\theta) + \log p(D|\theta) = \log p(x,y,D|\theta)$$
does not modify the M-step.
When running a Monte Carlo EM (MCEM) version, the conditional expectation is replaced with
$$\frac{1}{T} \sum_{\iota=1}^T \log p(x_\iota,y_\iota,D|\theta)\qquad (x_\iota,y_\iota)\sim p(x,y|D,\theta^{(t)})$$
where the decomposition is not useful either.

MCMC inside Expectation Maximization


Problema com lógica ao usar LEFT JOIN

Question: Em meu sistema, há um sistema de postagens e outro de amizades. Segue a estrutura das tabelas:
postagens: id | usuario | conteudo | data | hora
amizades: id | usuario1 | usuario2 | status
E estou utilizando a seguinte query para retornar as postagens tanto do usuário atual, como dos meus amigos (quando status = 2):
SELECT
    postagem.id, usuario, conteudo, data, hora, usuario1, usuario2, status
FROM
    postagens postagem
LEFT JOIN
    amizades ON usuario = usuario1 OR usuario = usuario2
WHERE
    status = 2 OR usuario = Igor
ORDER BY
    postagem.id DESC
LIMIT 10

Está funcionando perfeitamente, retornando tanto as minhas postagens (usuário logado), quanto as dos meus amigos (relação entre usuários onde status = 2), e aqui está um exemplo de dados contidos na tabela amizades:
2 | Igor  | Joao | 2
1 | Lucas | Igor | 0

Nos dados acima, quando status = 2 significa que são amigos, e quando status = 0 significa que não são amigos.
Porém, como há o meu usuário (Igor) nas duas linhas da tabela, ele duplica as postagens de tal usuário, ou seja, se houvesse outra linha na tabela, da qual faria a relação de amizade entre o meu usuário e outro qualquer, passaria a triplicar as postagens. Como resolver isso?
Comment: Você está passando o usuário por id?
Comment: @AndréBaill na realidade, ali onde está `usuario = Igor` é `usuario = '$usuario'`, eu apenas alterei para vocês poderem entender melhor meu problema.
Comment: Sim, mas é o id do usuario ou nome de usuario? teria que passar o id_usuario se não for
Comment: É o nome mesmo...
Comment: Tem que passar o Id para poder usar o left join corretamente.
Comment: SELECT * FROM postagens AS c1 LEFT JOIN amizades AS c2 USING(id_usuario)

Com isto, conseguimos relacionar e saber quais são as amizades que o usuario em questão possui
Comment: Mas pq não pode ser pelo nome? Esse nome de usuário seria como um id, é único
Comment: Porque cada vez que você for relacionar ou trocar o nome de usuario, ele não conseguirá autenticar, e também por segurança e ainda por questões justamente como esta que você postou, de relacionamento de tabelas :)
Comment: Não há como trocar o nome de usuário, após registrado, não pode ser alterado
Comment: beleza então...
Comment: De qualquer forma, como fazer? Eu tentei substituir o `ON` pelo `USING` assim como você disse, mas não funcionou... Você poderia dar um exemplo da query e como ela ficaria?
Answer: Tens aqui uma query que deve responder ao teu problema.
SELECT  DISTINCT postagem.id,
        usuario,
        conteudo,
        data,
        hora
FROM postagens postagem
LEFT JOIN
(
    SELECT usuario1, usuario2
     FROM amizades
    WHERE status = 2
      AND ( usuario1 = 'Igor' or usuario2 = 'Igor')
) amizades
  ON amizades.usuario1 = postagem.usuario
  OR amizades.usuario2 = postagem.usuario
WHERE amizades.usuario1 IS NOT NULL
   OR postagem.usuario = 'Igor'
ORDER BY postagem.id DESC
LIMIT 10

Caso o anterior não funcione, faz desta maneira:
SELECT  postagem.id,
        usuario,
        conteudo,
        dt
FROM postagens postagem
LEFT JOIN
(
    SELECT usuario1    id_amigo
     FROM amizades
    WHERE status = 2
      AND usuario2 = 'Igor'
    UNION
    SELECT usuario2
     FROM amizades
    WHERE status = 2
      AND usuario1 = 'Igor'
) amizades
  ON amizades.id_amigo = postagem.usuario
WHERE amizades.id_amigo IS NOT NULL
   OR postagem.usuario = 'Igor'
ORDER BY postagem.id DESC
LIMIT 10

Fica aqui o Fiddle
Uma outra forma, conceptualmente mais simples de resolver o problema ainda que mais extensa, seria dividir os resultados em dois: Os teus posts e os posts dos teus amigos. Isso poderia ser feito assim
SELECT postagem.id,
       usuario,
       conteudo,
       data,
       hora,
       'Minha postagem' AS Descricao
FROM postagens postagem
WHERE usuario = 'Igor'

UNION ALL

SELECT postagem.id,
       usuario,
       conteudo,
       data,
       hora,
       'Postagem de um amigo'
FROM postagens postagem
INNER JOIN
(
    SELECT usuario1 id_amigo
    FROM   amizades
    WHERE status = 2
      AND usuario2 = 'Igor'

    UNION

    SELECT usuario2
    FROM   amizades
    WHERE status = 2
      AND usuario1 = 'Igor'

) amizades
   ON id_amigo = postagens.usuario
ORDER BY postagem.id DESC
LIMIT 10

Em relação às tuas questões:

As descrições entre aspas são apenas um auxilio visual para perceber a origem das postagens. Pode ser removido da query.
A ideia é combinar os teus posts com os posts dos teus amigos através do uso do UNION ALL - Combinar o resultado de duas queries
Da forma como escreveste a tua query, não existe diferença entre o LEFT JOIN e o INNER JOIN.

SELECT
    postagem.id, usuario, conteudo, data, hora, usuario1, usuario2, status
FROM
    postagens postagem
LEFT JOIN
    amizades ON usuario = usuario1 OR usuario = usuario2
WHERE
status = 2   <- Esta condição obriga a que exista um registo na tabela amizades sendo desta forma equivalente ao INNER JOIN

Comment: Ah sim, perfeito! Eu não havia notado a mudança do INNER para o LEFT, muito obrigado!
Comment: Podes usar o DISTINCT?
Comment: Alterei novamente. Acrescentei um DISTINCT na primeira alternativa que deve remover os duplicados. Caso isso nao funcione, coloquei ainda uma outra alternativa.
Comment: Agora sim, perfeito!
Comment: Nossa, pensei que daria pra utilizar a query que eu estava tentando, por parecer mais simples... Mas ao ver a sua, me surgiram algumas dúvidas: Qual a função das descrições entre aspas (Minha postagem / Postagem de um amigo)? Tudo isso é uma query dividida em dois `SELECT's` dentro dela? Por que, no lugar de `LEFT JOIN` utilizar o `INNER JOIN`?
Comment: Essa segunda solução realmente parece melhor, e funciona. Porém, se o usuário não possuir nenhuma amizade as suas próprias postagens não são mostradas
Comment: Continua não mostrando as próprias postagens se não há amigos :(
Comment: Acabei de verificar aqui, porém, agora ele continua duplicando. Se eu possuo, por exemplo, 1 amizade, ele não duplica como antes, mas, se eu tiver 2 amizades ele passa a duplicar... Lembrando que o usuário pode ser tanto usuario1 quanto usuario2, já que o usuário1 é quem manda o convite...
Comment: Desculpe, mas o que é isso?

Problema com lógica ao usar LEFT JOIN


Creating a table from a query in MySQL/PHP

Question: I'm not sure this is possible, but the code I tried using first is:
mysql_query ("CREATE TABLE My_table as ( SELECT * FROM CPE LEFT JOIN Greenwich_j20 ON CPE.cust_index = Greenwich_j20.cust_index LEFT JOIN Liverpool_j20 ON CPE.cust_index = Liverpool_j20.cust_index)")
 or die ("this certainly didn't work\n");

The query itself works fine, and the syntax for the table works fine, but the combination is what it really doesn't like.  Does it have a problem creating a table from a left join query?
Comment: Should that `AS` [really be there](http://dev.mysql.com/doc/refman/5.1/en/create-table.html)?
Comment: Hi Tomalak, I don't seem to get any error, just the "this certainly didn't work" when it dies
Answer: If there is any auto increment column in your existing tables that will be not remain auto_increment in the table to be created.
You could  try by selecting columns instead of * there should be an error of column Ambiguous.
Comment: Should this have been a comment?
Comment: @Tomalak: I think should not you should give some reaction time.
Comment: @Shakti: Huh? I can't understand what you said. If you're implying that I should have waited for you to complete compiling your answer, then my response is that perhaps you shouldn't post your answer until it _is_ an answer. It's not a race.
Comment: @Tomalak: That was not. I mean to say At the first sight what I thought I posted but another sight I take one another thing I posted that as well. I am not taking it like race. By the way in real life everything is race.
Comment: @Shakti: Sorry, I can't understand you. I think it's a language barrier between us.
Comment: I think I know what you mean...  There are columns in seperate tables with the same name...  So it can include them in a query, but not a table?
Comment: @Soop: when you are selecting columns with `*` query will including all the columns from all the tables mentioned in query with duplicate column name. This is fine if you have only select query but with the Create table it will give you the Duplicate column error


ConvertNumber do JSF valores negativos

Question: Os valores monetários negativos, ao usar o conversor nativo do JSF, <f:convertNumber type="currency"/>, ficam com o caractere '-', antes do R$.
Por exemplo: -R$ 56.134.726,99
.xhtml

    <h:outputText value="#{entidade.valorMonetário}">
        <f:convertNumber type="currency"/>
    </h:outputText>

Existe alguma forma de resolver esse problema sem eu ter que criar o meu próprio Converter?
Answer: Se o problema em usar simplesmente:

    <h:outputText value="#{entidade.valorMonetário}">
        <f:convertNumber type="currency"/>
    </h:outputText>

e o negativo('-') fica atrás do símbolo R$
ex: -R$ 56.134.726,99
Aplicando o seguinte pattern

    <h:outputText value="#{entidade.valorMonetário}">
        <f:convertNumber type="currency" currencySymbol="R$" locale="pt_BR" pattern="¤ ###,###,##0.00; ¤ -###,###,##0.00"/>
    </h:outputText>

O negativo('-') fica depois do símbolo R$.
ex: R$ -56.134.726,99
Comment: Se possível, desenvolva mais a explicação, explicar as partes da sua resposta, o que são cada atributo de , ¤, e como funcionam.? Da maneira redigida você simplesmente jogou códigos dizendo para aplicar tal *pattern*
Comment: Sim, melhorou muito a qualidade de sua resposta.

ConvertNumber do JSF valores negativos


If $f(z)$ is analytic at $z_0$ then it is continuous at $z_0$

Question: My Proof: Since $f(z)$ is analytic at $z_0$ then it is differentiable at $z_0$
1) $f(z_0)$ exists because $f(z)$ is differentiable at $z_0$
2) $\lim_{z\rightarrow z_0}[f(z)-f(z_0)]=\lim_{z\rightarrow z_0}\frac{f(z)-f(z_0)}{z-z_0}(z-z_0)=\lim_{z\rightarrow z_0}\frac{f(z)-f(z_0)}{z-z_0}\lim_{z\rightarrow z_0}(z-z_0)=f(z_0)\lim_{z\rightarrow z_0}(z-z_0)=0$
3) $\lim_{z\rightarrow z_0}f(z)=f(z_0)$
therefore $f(z)$ is continous at $z_0$.
Is this proof correct?
Comment: That $f(z_0)$ exists is part of the definition of $f$. The second line isn't really necessary, but if you want to emphasize that $f(z_0)$ is a well defined number, it makes more sense to say something like "Since $f$ is given as a convergent power series in a neighborhood of $z_0$, $z_0$ is in its domain of definition, so $f(z_0)$ is a well defined number".
Answer: It is, but because the limit that you wrote is a product of limits that exist and whose product makes sense. In case you want to avoid this, a somewhat cleaner alternative is the following.
Given $\varepsilon>0$, there exists $\delta>0$ such that
$$
\left|\frac{f(z)-f(z_0)}{z-z_0}-f'(z_0)\right|<\varepsilon\quad\text{when } |z-z_0|<\delta.
$$
Hence,
$$
\left|\frac{f(z)-f(z_0)}{z-z_0}\right|<|f'(z_0)|+\varepsilon
$$
and we obtain
$$
|f(z)-f(z_0)|=\left|\frac{f(z)-f(z_0)}{z-z_0}\right|\cdot|z-z_0|<(|f'(z_0)|+\varepsilon)|z-z_0|.
$$
Now just let $z\to z_0$.
Comment: Is the first point of my proof correct?  $f(z_0)$ exists because $f(z)$ is differentiable at $z_0$
Comment: Do you mean "$f(z_0)$ exists"? If so, yes, we only define the derivative in this case (at this level).


What do Windows Defender Powershell ThreatAction enums map to?

Question: I'm looking at a default Defender configuration in Server 2016.  I'm not sure how to discover what these '0' mean for the ThreatDefaultAction:
HighThreatDefaultAction                       : 0
LowThreatDefaultAction                        : 0
MAPSReporting                                 : 2
ModerateThreatDefaultAction                   : 0

I would think from the Set-MpPreference documentation that it's Quarantine, but that same bit of text references a Parameter set that starts with 'Clean', not 'Quarantine'.
This explanation of ThreatIdDefaultActions doesn't even have a 0 in it's list: Add-MpPreference
Answer: All HighThreatDefaultAction, LowThreatDefaultAction, ModerateThreatDefaultAction, SevereThreatDefaultAction and UnknownThreatDefaultAction are properties of [ThreatAction] enum type. Their value specifies which automatic remediation action to take for a given level threat.
Zero value of any above property should be considered undefined as successfully performed Remove-MpPreference cmdlet sets their value to zero. Demonstration in the following example:
PS C:\WINDOWS\system32> Import-Module Defender

PS C:\WINDOWS\system32> Get-MpPreference | Select-Object -Property *DefaultAction

HighThreatDefaultAction     : 2
LowThreatDefaultAction      : 0
ModerateThreatDefaultAction : 0
SevereThreatDefaultAction   : 2
UnknownThreatDefaultAction  : 0

PS C:\WINDOWS\system32> Remove-MpPreference -Force -HighThreatDefaultAction

PS C:\WINDOWS\system32> Get-MpPreference | Select-Object -Property *DefaultAction

HighThreatDefaultAction     : 0
LowThreatDefaultAction      : 0
ModerateThreatDefaultAction : 0
SevereThreatDefaultAction   : 0
UnknownThreatDefaultAction  : 0

However, there is another vagueness in the Set-MpPreference documentation; for instance:

-HighThreatDefaultAction
Specifies which automatic remediation action to take for a high level
  threat. The acceptable values for this parameter are:
Quarantine
Remove
Ignore

but an attempt of Set-MpPreference -HighThreatDefaultAction Ignore leads to InvalidData error as the [ThreatAction] type enumeration does not contain name of Ignore:
PS > Import-Module Defender
PS > [Microsoft.PowerShell.Cmdletization.GeneratedTypes.MpPreference.ThreatAction] |
    Get-EnumValue

Name                           Value
----                           -----
Clean                          1
Quarantine                     2
Remove                         3
Allow                          6
UserDefined                    8
NoAction                       9
Block                          10

Note the Get-EnumValue function comes from this my answer at CodeReview.
Comment: Interesting, since all the ThreatDefaultAction values are set at Zero, what option does that denote?
Comment: This the big question!  I'm going to work w/ Microsoft and try to get an answer on this.  It smells like a bug in the reported state of those default actions if this many people can look at this and not determine what Defender is doing.  Thanks!
Answer: Definitely looks like the documentation is lacking there. If you look at the example for SignatureScheduleDay it documents the mapping a lot better, which lines up exactly as the Parameter Set lists them.
Best bet would be that they map according to the "Parameter Sets." Which also maps the same as the Syntax part of the help file:
                             0   |      1     |   2    |   3   |      4      |     5    |   6
[-HighThreatDefaultAction {Clean | Quarantine | Remove | Allow | UserDefined | NoAction | Block}]

I checked to see if an issue for this was listed in github and didn't find one. You could definitely inquire more there: https://github.com/MicrosoftDocs/windows-powershell-docs/blob/master/docset/windows/defender/set-mppreference.md
Comment: Only problem with assuming 0 means 'Clean' is that Clean is not in their list of possible options (Quarantine, Remove, Ignore)....https://github.com/MicrosoftDocs/windows-powershell-docs/blob/master/docset/windows/defender/set-mppreference.md#-highthreatdefaultaction Opened an Issue on Github. https://github.com/MicrosoftDocs/windows-powershell-docs/issues/380
Comment: @Bob look at the "Parameter Sets". Clean is definitely listed.
Comment: I think you're not getting my point.  I see that Clean is in the parameter set.  However, the only integer map we can find for the parameter set shows that Clean is '1', not '0'.  Secondly, Only Quarantine, Remove, and Ignore are listed as possible options for these ThreatDefaultAction parameters, so Clean doesnt make much sense.
Answer: Ok, got the answer from Microsoft support.
0 is the same as NULL (example of the docs here), which means to follow the default that Microsoft applies to each threat.  In other words, every virus/malware/etc. has a severity and threat action (i.e. mitigation) attached.  A '0' in GetMpPreference means to use that mitigation and not whatever value you might have specified here to override that universally for each severity level.  Hope that helps any future travelers.
Comment: For future travellers: Read [JosefZ](https://serverfault.com/users/257436)'s [answer](https://serverfault.com/a/933828/536173) before reading this answer.


Jenkins tool installation fails with message: Installer "Run Shell Command" cannot be used to install "" on the node ""

Question: I have configured Jenkins using Jenkins > Manage Jenkins > Global Tool Configuration to ensure that agents have the required tool chain installed before they work on builds.
Why is a pipline build's tool-installation stage failing with the following error (for example)?
Installer "Run Shell Command" cannot be used to install "jdk-11.0.5" on the node "cent76-test401"

Answer: This error message can be returned when the Jenkins configuration for the tool installation is set to run only on certain labels, and your target agent does not have the required label.
In the question's example, the JDK installation's Run Shell Command configuration was limited to agents with the lin64 label, but the agent named cent76-test401 did not have this label.
See also timestamp 15:25:56 in this #jenkins IRC log https://echelog.com/logs/browse/jenkins/1487545200


Cannot add a 2012 server to a 2003 domain as it says forrest functional level is 2003 but domains and trusts shows it as 2003

Question: I have a single 2003 DC (Enterprise edition 32 bit SP2) that according to AD Domains and Trusts is at functional level Windows Server 2003.
According to AD Users and Computers my Domain functional level is also 2003.
However when I try add a 2012 R2 server as a domain controller on the domain it gives an error "Verification of replica failed. The forest functional level is Windows 2003"
When I run adfind -rootdse forestfunctionality I get back that it is 0 [Windows 2000 Forest Mode]
How do I get my current 2003 setup to correctly be on 2003 Forest Functional level so I can add in my 2012 Domain controller.
Comment: Do you have multiple domains?
Answer: Forest and Domain are two separate things.
Open the Active Directory Domains and Trusts MMC. Right-click on the top node in the tree (Active Directory Domains and Trusts [yourdc]) and pick "Raise Forest Functional Level...".
Comment: I agree forrest and domain are different - I have now added a picture so you can see it says I am at Server 2003 level yet my 2012 still thinks I am at 2000 level - any more suggestions?
Answer: Rob, the other two answers are correct. I think you have missed the difference between where you are clicking. See the screen shots below.
Forest: Right Click at the very top

Domain: Right click on the domain name
Comment: Thank you - its true a picture is worth a 1000 words - you have resolved my issue
Answer:
To raise the forest functional level

Open Active Directory Domains and Trusts.
In the console tree, right-click the Active Directory Domains and Trusts node, and then click Raise Forest Functional Level.
In Select an available forest functional level, click Windows Server 2003, and then click Raise.

https://technet.microsoft.com/en-us/library/cc780862%28v=ws.10%29.aspx
Comment: I have now added a picture so you can see it says I am at Server 2003 level yet my 2012 still thinks I am at 2000 level - any more suggestions?
Comment: You are clicking the wrong node. Click on "Active Directory Domains and Trusts" not "domain.local"
Comment: Thank you - you are correct - just had to move my mouse up a smidge - appreciate your help.


Restore blocks on Drupal 7

Question: How can I restore the original position (order and initial regions) of blocks on Drupal 7?
I need to make a button that you can restore the block position on Drupal 7. What function or table can be used?
Answer: The information about the blocks are saved in the "block" table. The block information is saved in the admin form handled by block_admin_display_form() using the following code.
  foreach ($blocks as $i => $block) {
    $key = $block['module'] . '_' . $block['delta'];
    $form['blocks'][$key]['module'] = array(
      '#type' => 'value',
      '#value' => $block['module'],
    );
    $form['blocks'][$key]['delta'] = array(
      '#type' => 'value',
      '#value' => $block['delta'],
    );
    $form['blocks'][$key]['info'] = array(
      '#markup' => check_plain($block['info']),
    );
    $form['blocks'][$key]['theme'] = array(
      '#type' => 'hidden',
      '#value' => $theme,
    );
    $form['blocks'][$key]['weight'] = array(
      '#type' => 'weight',
      '#default_value' => $block['weight'],
      '#delta' => $weight_delta,
      '#title_display' => 'invisible',
      '#title' => t('Weight for @block block', array('@block' => $block['info'])),
    );
    $form['blocks'][$key]['region'] = array(
      '#type' => 'select',
      '#default_value' => $block['region'] != BLOCK_REGION_NONE ? $block['region'] : NULL,
      '#empty_value' => BLOCK_REGION_NONE,
      '#title_display' => 'invisible',
      '#title' => t('Region for @block block', array('@block' => $block['info'])),
      '#options' => $block_regions,
    );
    $form['blocks'][$key]['configure'] = array(
      '#type' => 'link',
      '#title' => t('configure'),
      '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure',
    );
    if ($block['module'] == 'block') {
      $form['blocks'][$key]['delete'] = array(
        '#type' => 'link',
        '#title' => t('delete'),
        '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/delete',
      );
    }
  }

Using $form_state['values']['blocks'][$key]['module'], and $form_state['values'][$key]['delta'], you can find the block information from that table, and set the form fields to the  values found in the database. In the submission handler for that button, you should also set $form_state['#rebuild'] to TRUE to rebuild the form with the restored values.

Restore blocks on Drupal 7


How to exclude results in a query using a subquery as blacklist

Question: I'm trying to query a database for text, use a (sub)query as a "blacklist". The twist: if any of the results from the main query have an identical beginning as any of the results from the subquery, they should be skipped.
A bit of background: I'm building a filebrowser, and using a table of foldernames to maintain the "favorited" state of entries. These favorites are intended to work in a cascading manner, i.e. any paths below a "explicitly" favorited path will be "implicitly" favorited.
Consider the following data:

foldername
is_favorite
is_implicit_favorite

foo/
1
0

foo/bar/
1
1

foo/bar/baz/
0
1

foo/bar2/
0
1

foo/bar2/baz/
0
1

foo2/bar/
0
0

foo2/bar/baz/
0
0

Adding a favorite is easy: all folders below the given path will have their "implicit" state set. But I've found it hard to come up with a simple approach for the opposite - "unfavoriting" a folder. Because here, I want to the query to skip folders that should remain implicitly favorited (in the example above, unfavoriting "foo/" should skip folders below "foo/bar").
I've tried various solutions - the following approach comes close, but unfortunately only works with a single favorited subfolder:
SELECT DISTINCT folders.foldername FROM folders
JOIN (
    SELECT folders.foldername FROM folders
    WHERE folders.foldername LIKE 'foo/' || '%'
    AND folders.is_favorite = 1
) favs ON folders.foldername NOT LIKE favs.foldername || '%'
WHERE folders.foldername LIKE 'foo/' || '%'

It does what I want - unfavoriting "foo" should result in "foo/bar2/" and "foo/bar2/baz/" no longer being implicitly favorited, while "foo/bar/" (and its subfolders) remain unchanged:

folders.foldername

foo/

foo/bar2/

foo/bar2/baz/

View it online:
http://sqlfiddle.com/#!5/8a04e/14/0
EDIT: thanks to Christians answer pointing me to the EXCEPT operator, I was able to come up with the following modified version that also works for multiple favorites.
SELECT folders.foldername FROM folders
WHERE folders.foldername LIKE 'foo/' || '%'
EXCEPT
SELECT folders.foldername FROM folders
INNER JOIN (
    SELECT folders.foldername FROM folders
    WHERE folders.foldername <> 'foo/'
    AND folders.is_favorite = 1
) favs
ON folders.foldername LIKE favs.foldername || '%'

I'm very happy that it's even possible at all to express this in pure SQL - I was briefly tempted to make it a hybrid SQL/node.js solution.
And of course, I'd happily accept any advice on possible optimizations!
Thanks.
Answer: If you have a PostgreSQL database, you can use this:
SELECT folders.foldername FROM folders WHERE folders.foldername LIKE 'foo/%'
EXCEPT ALL
SELECT folders.foldername FROM folders
WHERE folders.foldername LIKE 'foo/%' AND folders.is_favorite = 1

Comment: Unfortunately that doesn't do the trick - it's not enough to check if the "is_favorite" property is set, it has to match against all subfolders of the favorited folder(s).
To verify this, I turned your query into a SQL fiddle: http://sqlfiddle.com/#!17/38836/2/0

But interesting with the EXCEPT statement, I was not aware of that one (works in sqlite too)
Answer: This might not be the most efficient query, but it does the trick:
SELECT folders.foldername FROM folders
WHERE folders.foldername LIKE 'foo/' || '%'
EXCEPT
SELECT folders.foldername FROM folders
INNER JOIN (
    SELECT folders.foldername FROM folders
    WHERE folders.foldername <> 'foo/'
    AND folders.is_favorite = 1
) favs
ON folders.foldername LIKE favs.foldername || '%'

(Updated my original post)


Sobre URL com token

Question: Boa noite!
Apenas tirar uma dúvida.
Estou desenvolvendo uma aplicação android e vai receber informações em json, mas ao fazer login, seria mais ou menos assim

http://exemplo.com/auth/login/senha

caso a verificação for correto receberá um token para ter acesso aos demais informações. Será trabalhado tudo com token.

http://exemplo.com/token/meu_perfil

Ou seja com token recebido terá permissão para ver a informação do perfil.
Isso é seguro? Ou ainda precisa ser melhorado?
Obs: não gosto usar Framework para evitar a compatibilidade, utilizo a linguagem PHP.
Answer: Seu approach esta OK, mas pode ser melhorado adicionando mais "segurança":

Utilizar Basic Auth na url de login, ou seja, enviar usuario e senha no formato "usuario:senha" codificado em Base64 no header da requisição.
Exemplo:
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("myuser","mypass".toCharArray());
}});
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
c.setUseCaches(false);
c.connect();

ou você pode simplesmente adicionar uma request property a conexāo:
c.setRequestProperty("Authorization", "basic " +
                Base64.encode("myuser:mypass".getBytes(), Base64.NO_WRAP));

Para as demais URL's, enviar o token no header das requisições e sempre se certifique que o token esteja criptografado e nāo somente codificado em Base64 o que possibilita a qualquer pessoa reutilizar um token mesmo que esteja expirado. Você pode utilizar o header "Authorization" para tanto.

Enfim, evite enviar qualquer informaçāo sensível sem estar criptografada e muito menos no path da URL.
Comment: Essa sintaxe é linguagem Java? Pode me falar um exemplo em javascript? O que eu entendi vc quis dizer enviar as informações no header do http com dados criptografado? Correto?
Comment: Sim, o exemplo é java e Android. Em JavaScript é igual, voce vai fazer uma chamada Ajax passando os Headers criptografados.

Sobre URL com token


How to structure repetitive data under an entity type bundle?

Question: I'd like to use Drupal in one of my projects but I'm wondering what's the best way to organize my content.
I have a list of authors. Each author has 3 properties:

name
bio
list of books

On the list of books each book has 6 properties(5 text fields + image).
I was thinking about creating entity type bundle 'Author' and 'Book', then assigning certain books to certain authors and displaying lists of books on author page.
The downside is that on the content list I'll have a list of many different 'pages' of content type 'Book' that can't be easily filtered by author. Also, creating new content type for each author is not a good solution.
1) Is it the best possible solution for Drupal?
2) Would it be possible to create:
a) hierarhical list of author/book_list posts or
c) content type Author with list of books on it's page(book field with book properties fields)
I'd appreciate your advice :).
Comment: Since your answer is very broad you may not get the level of detail you require in answers. If you need more detail you will get better results with more specific, individual questions.
Answer: You could always make another view to supplement or replace your content list.  If you're using Entity Reference for the relationship between book and author you can create an easily filterable list of books.
Alternatively, 2c is a viable option.  You can create a group of fields on Author called book using field collection and set the group to have unlimited instances.
Comment: thank you for your input. It's very valuable and I wish I could at least upvote it(I can't accept both answers).
Comment: It's all good =)
Answer: 1.
 There is no "best" possible solution for Drupal that can be given here.
There are many ways to do it and the best way for one site might not be the best way for another, so it depends on your specific requirements, which we don't know.
Having said that, the way you are planning to do it should work fine.
You can create a view with relationships that will allow you to easily filter by author.
From a data point of view it is also good because books and authors could logically be considered different content types.
An alternative solution is the field collection module but it works a little different to your proposed solution so your specific data and requirements will indicate which is the better solution.
If you don't have first hand experience with the different methods the best way forward would be to try any solutions you think may be good and then decide which is the best for you for this project.
2.

a. Yes. Depending on how you end up configuring the content types and your exact listing requirements the solution might be a little different and may require differing amounts of custom themeing or development but it is possible.
b. ?
c. Yes. What I wrote for point a above also applies here however generally speaking I would say this requirement will be easier to satisfy than a.


Required positional argument: self

Question: I'm new to Python, and I need some help. I was writing a blackjack program as homework, and I think I may have it working, but whenever I run it, it complains that I have not provided anything for 'self'. I thought I didn't have to? Here is the full code:
class BlackjackPlayer:
    '''Represents a player playing blackjack
    This is used to hold the players hand, and to decide if the player has to hit or not.'''
    def __init__(self,Deck):
        '''This constructs the class of the player.
        We need to return the players hand to the deck, get the players hand, add a card from the deck to the playters hand, and to allow the player to play like the dealer.
        In addition to all of that, we need a deck.'''
        self.Deck = Deck
        self.hand = []

    def __str__(self):
        '''This returns the value of the hand.'''
        answer = 'The cards in the hand are:' + str(self.hand)
        return(answer)

    def clean_up(self):
        '''This returns all of the player's cards back to the deck and shuffles the deck.'''
        self.Deck.extend(self.hand)
        self.hand = []
        import random
        random.shuffle(self.Deck)

    def get_value(self):
        '''This gets the value of the player's hand, and returns -1 if busted.'''
        total = 0
        for card in self.hand:
            total += card
        if total > 21:
            return(-1)
        else:
            return(self.hand)

    def hit(self):
        '''add one card from the Deck to the player's hand.'''
        self.hand.append(self.Deck[0])
        self.Deck = self.Deck[1:]
        print(self.hand)

    def play_dealer(self):
        '''This will make the player behave like the dealer.'''
        total = 0
        for card in self.hand:
            total += card
        while total < 17:
            BlackjackPlayer.hit()
            total += BlackjackPlayer[-1]
            print(self.hand)
        if self.hand > 21:
            return -1
        else:
            return total

When I run this, I get:
TypeError: get_value() missing 1 required positional arguments: 'self'

I would gladly appreciate any help, and this is my first time here, so I apologize if I broke the rules or something.
Comment: When you say "When I run this", what did you do :3?
Comment: I compiled Python from source, so I entered ./python /blackjack.py
Comment: Did you make an instance of the class? i.e, did you do something like `player1 = BlackjackPlayer('params')`
Comment: This is the entire program right here, if that is what you are asking. Come to think of it, I probably am trying to cram too many things into this class.
Comment: @user2384528, if that's the entire program, what are you expecting to run exactly? All that does is define a class.
Answer: I'm not sure the problem you have is in the code you've shown since you're not actually calling get_value() anywhere in there.
It will have to do with the way you're using this class. You need to ensure that you instantiate an object for this class and use it to call the function. That way, self is prefixed automagically to the argument list.
For example:
oneEyedJim = BlackJackPlayer()
score = oneEyedJim.get_value()

On top of that, your scoring doesn't seem to take into account the fact that Aces can be soft (1) or hard (11).
Comment: Thanks for the help! Embarrassingly enough, I wasn't calling it properly. Thanks for your time and help!
Answer: BlackjackPlayer.hit() may be the thing causing you troubles. If you want to use functions from a class, you have to create an instance of that class. However, as you're calling a function from the class, you can simply do:
self.hit()

Also:
total += BlackjackPlayer[-1]

I don't know what you're intending here, but if you wanted to access the hand list, do:
total += self.hand[-1]

Comment: Thanks for the tips, they were very helpful!

Required positional argument: self


Alterar o CSS com Jquery

Question: Como faço para que o usuário ao clicar em um determinado link, altere o CSS e o título? Por exemplo:
Tenho esse link: Contraste
Ao clicar nele, mudasse o CSS e o nome do link também, passando para Sem contraste.
Comecei fazendo algo, mas JQuery não é meu forte. Vejam abaixo:
Contraste (c)

    $('#contraste').click(function(){

      var acao = $(this.className);

        if(acao === "ativar"){
           // Alterar o CSS
          }else{
            // Voltar para o CSS atual
        }

    });

Comment: Você está usando alguma programação backend no seu código? Ficaria mais fácil se tivesse usando o PHP para compor o seu frontend.. Assim você poderia criar uma variável dentro da sua tag header.. Aí sim a condição poderia funcionar.. tipo, If ação ativar, altera o endereço de busca na herder, Else, continua o mesmo
Comment: Olá Francis. Na verdade ele não terá backend, é apenas um site institucional que terá  acessibilidade. Algumas coisas já consegui fazer, como aumentar a fonte, ir pra o conteúdo, usar o teclado, mas o contraste não estou conseguindo. Não teria como fazer com JQuery ou Javascript? Vi alguns exemplos usando 02 links, mas gostaria de usar apenas 1.
Answer: Pode verificar se o link contém a classe com o método .is:

$('#contraste').click(function(){

   var acao = $(this).is(".ativar"); // retorna true ou false

   if(acao){ // se for true
      $(this)
      .css({ // muda o CSS
         "font-weight": "normal"
      })
      .text("Sem contraste") // altera o texto
      .removeClass("ativar"); // remove a classe
   }else{ // se for false
      $(this)
      .css({ // muda o CSS
         "font-weight": "bold"
      })
      .text("Contraste (c)") // altera o texto
      .addClass("ativar"); // adiciona a classe
   }

});

Contraste (c)
Comment: Muito obrigado a todos. As duas soluções funcionaram.
Answer: Uma solução bem simples é utilizar a função toggleClass, que coloca a classe caso não lá esteja, ou tira caso já tenha. Por esse motivo vai alternar entre ter o CSS da classe e não ter, funcionando exatamente como pretende. O alternar do texto do link já tem de ser feito separadamente, ainda assim não passa de um if.
Exemplo:

$('#contraste').click(function(){
  $(".conteudo").toggleClass("contraste"); //coloca ou retira a classe contraste
  //troca o texto do link
  $(this).text($(this).text() == "Contraste (c)" ? "Sem Contraste (c)": "Contraste (c)");
});
.conteudo {
  background-color:lightGray;
  width:300px;
  height:100px;
}

.contraste {
  background-color:darkGray;
  color:white;
}

Contraste (c)

  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse feugiat, lorem vitae consectetur tincidunt, nisl erat porta ligula, et scelerisque elit lorem nec augue.

Alterar o CSS com Jquery


How search in UITableView with Array of model

Question: I want to search for some data in the table view using the search bar, but when I try to find data in my model, I'm not able to search that data.
I made a expand table view cell and created a search bar for searching data, but still I can't search the data in the model. How can I achieve that?
my code is here:
import UIKit

protocol SelectCurrencyViewControllerDelegate: AnyObject {
    func selectedCurrency(_ curency: CurrencyModel)
}

final class SelectCurrencyViewController: UIViewController {

    weak var delegate: SelectCurrencyViewControllerDelegate?

    //MARK: - Private properties

    private let currencyCellIdentifier = "currencyCell"
    private let currencyArray = [CurrencyModel(shortName: "RUB", fullName: "Russian ruble"),
                                 CurrencyModel(shortName: "USD", fullName: "US Dollar"),
                                 CurrencyModel(shortName: "EUR", fullName: "Euro")]

    //MARK: - UI elements

    private let currencyTableView = UITableView()
    private let searchBarCurrency = UISearchBar()

    //MARK: - Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    //MARK: - Private methods

    private func setupUI() {
        view.backgroundColor = .systemBackground
        view.addSubview(searchBarCurrency)
        view.addSubview(currencyTableView)
        settingsSearchbar()
        settingsCurrencyTableView()
        setConstraints()
    }

    private func settingsSearchbar() {
        searchBarCurrency.translatesAutoresizingMaskIntoConstraints = false
        searchBarCurrency.delegate = self
    }

    private func settingsCurrencyTableView() {
        currencyTableView.dataSource = self
        currencyTableView.delegate = self
        currencyTableView.register(CustomCurrencyCell.self, forCellReuseIdentifier: currencyCellIdentifier)
        currencyTableView.translatesAutoresizingMaskIntoConstraints = false
    }

    private func setConstraints() {
        NSLayoutConstraint.activate([
            searchBarCurrency.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            searchBarCurrency.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            searchBarCurrency.trailingAnchor.constraint(equalTo: view.trailingAnchor),

            currencyTableView.topAnchor.constraint(equalTo: searchBarCurrency.bottomAnchor),
            currencyTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            currencyTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            currencyTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
}

//MARK: - UISearchBarDelegate
extension SelectCurrencyViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        print(searchText)
    }
}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension SelectCurrencyViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        currencyArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: currencyCellIdentifier,
                                                 for: indexPath) as? CustomCurrencyCell
        cell?.configure(shortName: currencyArray[indexPath.row].shortName,
                        fullName: currencyArray[indexPath.row].fullName)
        return cell ?? UITableViewCell()
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        delegate?.selectedCurrency(currencyArray[indexPath.row])
        navigationController?.popViewController(animated: true)
    }
}

my code of CustomCell here:
import UIKit

final class CustomCurrencyCell: UITableViewCell {

    //MARK: - UI Elements

    private let currencyBoldLabel = UILabel()
    private let currencyNormalFullLabel = UILabel()

    //MARK: - Life Cycle

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: - Configure

    func configure(shortName: String, fullName: String) {
        currencyBoldLabel.text = shortName
        currencyNormalFullLabel.text = fullName
    }

    //MARK: - Private methods

    private func setupUI() {
        addSubview(currencyBoldLabel)
        addSubview(currencyNormalFullLabel)
        settingsCurrencyBoldLabel()
        setConstraints()
    }

    private func settingsCurrencyBoldLabel() {
        currencyBoldLabel.translatesAutoresizingMaskIntoConstraints = false
        currencyBoldLabel.font = UIFont.systemFont(ofSize: 14, weight: .bold)
    }

    private func setConstraints() {
        currencyNormalFullLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            currencyBoldLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            currencyBoldLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),

            currencyNormalFullLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            currencyNormalFullLabel.leadingAnchor.constraint(equalTo: currencyBoldLabel.trailingAnchor, constant: 8)
        ])
    }
}

and code of my Model:
import Foundation

struct CurrencyModel {
    let shortName: String
    let fullName: String
}

I try this add search data
private var searchDate = [CurrencyModel]()

and in searchBarDelegate try this:
extension SelectCurrencyViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
         searchDate = searchText.isEmpty ? currencyArray : currencyArray.filter {$0.fullName.contains(searchText)}
        currencyTableView.reloadData()
    }
}

Idk how cast my cell in cellForRowAt, cause I used configure
and if I trying
cell?.currencyNormalFullLabel.text = searchDate[indexPath.row].fullName

my app crashed
Comment: SearchDate should be your data source, in table view delegate instead of currency array use searchDate, also initialise searchDate similar to currencyArray
Comment: @vignesh Anyway my app crashed with `Fatal error: Index out of range` in `cell?.currencyNormalFullLabel.text = searchDate[indexPath.row].fullName
`
Answer: I have solved my problem:
In viewDidLoad I'm add
filtredData = currencyArray

and in numberOfRowsInSection change like this:
    extension SelectCurrencyViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        filtredData.count
    }

and cast my cell like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: currencyCellIdentifier,
                                                 for: indexPath) as? CustomCurrencyCell
        cell?.configure(shortName: filtredData[indexPath.row].shortName,
                        fullName: filtredData[indexPath.row].fullName)
        return cell ?? UITableViewCell()
    }

and finally edit my filter method:
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        filtredData = []

        if searchText == "" {
            filtredData = currencyArray
        }

        for word in currencyArray {
            if word.fullName.lowercased().contains(searchText.lowercased()) ||
                word.shortName.lowercased().contains(searchText.lowercased()) {
                filtredData.append(word)
            }
        }
        self.currencyTableView.reloadData()
    }


Como atualizar Dados de um Select com Ajax?

Question: Tenho a seguinte duvida, tenho uma modal, onde insiro uma cidade, feito isso preciso que meu select seja recarregado mostrando essa opção recém adicionada, sem que a página seja atualizada:
Select:

<select id="idMunicipio" name="idMunicipio" class="form-control">
  <?php foreach ($municipios as $municipio) : ?>
  <option value="<?=$municipio->idMunicipio?>"><?=$municipio->municipio?></option>
  <?php endforeach ?>
</select>
Answer: Adicione-o ao <select> desejado. Basta inserir um elemento <option>.
CÓDIGO
Essa seria uma requisição POST para fazer a inserção do município. Quero que se atente apenas a função de callback.
Supondo que o retorno da requisição seja um JSON da forma {"id_municipio": "...", "nome_municipio": "..."} então poderemos fazer o seguinte código:
$.post('algum_caminho', dadosMunicipio, function(data){
  $('select[name=idMunicipio]').append('<option value="'+data.id_municipio+'">'+data.nome_municipio+'</option>');
});

Onde algum_caminho seria para onde seria enviada a requisição (um arquivo PHP por exemplo), dadosMunicipio seriam os dados passados via requisição, coletados pela modal.


Flutter using cloud_firestore giving errors on IOS Simulator

Question: It's my first time trying to use Firebase cloud_firestore together with Flutter. It works fine with the Android emulator. But I am getting the following errors, when trying to Run on the IOS Sim:
<code>/Users/carsoncarbery/development/tools/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.8.2+3/ios/Classes/CloudFirestorePlugin.m:50:22: error: no visible @interface for 'FIRQuery' declares the selector 'queryWhereField:arrayContains:'
query = [query queryWhereField:fieldName arrayContains:value];
~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/carsoncarbery/development/tools/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.8.2+3/ios/Classes/CloudFirestorePlugin.m:221:29: error: no known class method for selector 'fieldValueForArrayUnion:'
return [FIRFieldValue fieldValueForArrayUnion:[self readValue]];
^~~~~~~~~~~~~~~~~~~~~~~
/Users/carsoncarbery/development/tools/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.8.2+3/ios/Classes/CloudFirestorePlugin.m:224:29: error: no known class method for selector 'fieldValueForArrayRemove:'
return [FIRFieldValue fieldValueForArrayRemove:[self readValue]];
^~~~~~~~~~~~~~~~~~~~~~~~
8 warnings and 3 errors generated.
Could not build the application for the simulator.
Error launching application on iPhone 6s.
</code>
I've also tried running the Runner directly from Xcode and get the same three build errors. Unfortunately I haven't been able to find an answer to this, so am posting the question. Any help would be gratefully received.
Thanks
Comment: is your GoogleService-Info.plist in ios/Runner folder?
Comment: Yes, its in the ios/Runner folder
Answer: I just experienced the same thing.
Following these guys
https://github.com/flutter/flutter/issues/24395
I did
<code>pod update Firebase/Firestore
</code>
And it worked for me.
I'm using latest version of cloud firestore: 0.9.0+1
I can compile from both Xcode and VS Code.
There are a lot of warning messages when running on simulator. some of them look pretty nasty, but it works.


Is CSF overkill?

Question: My server runs just my own sites (vBulletin forums - which are always patched with security fixes) and Rails sites using the latest version) so do I really need CSF? (http://configserver.com/cp/csf.html) Or is it unnecessary for this kind of server set-up?
I have already done the usual (disable SSH login, pub-key auth, very strong passwords everywhere else etc)
It was often recommended by users over at the cPanel forums - but I guess most of them are hosts there.
Answer: I wouldn't say it is overkill. I run CSF on all my web servers. It does a great job at auto banning port scanners, failed logins and general hacking attempts. It updates its self on a regular bases and gives you a nice report on how secure your server is. Ontop of that it is super easy to manage what goes in and out of your server by port control etc.
It is impossible to be overkill with security not unless you are blocking your main ports, 80 :).
Comment: "impossible to be overkill with security" - strongly disagree. Adding gratuitous complexity can be catastrophic for security. That is not to say CSF might be a bad idea here! Just don't try to make your fireplace more safe by stuffing it full of safety matches.

Is CSF overkill?


Flattening the distribution of scores in a game to provide a difficulty

Question: I have many routes a player can take in a game I'm making. Each route awards them a specific score (ranging from 4 to 31, but mostly concentrated around 8 and 11) and I need to decide which difficulty challenge to present the player with in order to progress through that route.
The difficulty of challenges I have range from 1 to 9. I want each difficulty to have as close to an equal chance of being chosen assuming the player takes a random route (so if take every score and put them through this formula to get the difficulty and count how many times each difficulty is chosen, all these counts would be as close as possible).
Is there any formulae I could use to find a corresponding difficulty for any route based on that route's score relative to the scores of all the other routes? Of course, the higher score a route awards, the higher difficulty I want the challenge to be.
I have plotted the current distribution of scores (x being the score, y being the occurrences of that score) on a graph to make them easier to visualize, and so I believe I'm looking for an equation that flattens this curve
Scatter graph.
Comment: https://en.m.wikipedia.org/wiki/Histogram_equalization
Comment: That looks like a good start but I'm struggling to find any more basic maths or code for it since it's mostly used for image processing. But I have now generated a histogram for my data which is probably more suitable for this https://i.imgur.com/cG25eER.png In making the histogram, I also found the skewness (3.42721) and kurtosis (17.79751) which could be useful if I can get an equation to generate a similar graph and then invert it to hopefully find the equation for the difficulty
Answer: Turns out I was really over-thinking this problem. The solution was to just produce a sorted list of all the available scores, find the index within this list of the target score and divide this by the number of routes per difficulty.
Mathematically, where $S(x)$ is the score at sorted index $x$, $s$ is the target score, $D(x)$ is a value for the difficulty $x$ and $d$ is the target difficulty, $d=D(\frac{S^{-1}(s)}{\vert D\vert})$
Keep in mind that the same score may appear multiple times in $S$, $S^{-1}(x)$ may have several solutions, so an average of these solutions must be taken. Since $S$ is sorted, the indexes will also be sorted and one apart and there will be no extreme solutions, so both a median and mean will produce the same fair result. Due to both this and the division by $\vert D \vert$, rounding may be required. I arbitrarily chose that, between just a few more lower or higher difficulties, I would prefer more lower difficulties, so I round down for .5


CoreGraphics on iPhone. What is the proper way to load a PNG file with pre-multiplied alpha?

Question: For my iPhone 3D apps I am currently using CoreGraphics to load png files that have pre-multiplied alpha. Here are the essentials:
<code>// load a 4-channel rgba png file with pre-multiplied alpha.
CGContextRef context =
CGBitmapContextCreate(data, width, height, 8, num_channels * width, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

// Draw the image into the context
CGRect rect =  CGRectMake(0, 0, width, height);
CGContextDrawImage(context, rect, image);
CGContextRelease(context);
</code>
I then go on and use the data as a texture in OpenGL.
My question: By specifying pre-multiplied alpha - kCGImageAlphaPremultipliedLast - am I - inadvertently - telling CoreGraphics to multiply the r g b channels by alpha or - what I have assumed - am I merely indicating that the format of the incoming image has pre-multiplied alpha?
Thanks,
Doug
Answer:
By specifying pre-multiplied alpha - kCGImageAlphaPremultipliedLast - am I - inadvertently - telling CoreGraphics to multiply the r g b channels by alpha or - what I have assumed - am I merely indicating that the format of the incoming image has pre-multiplied alpha?

Neither one. You specified the format of the destination (the context), not the source. The source—the image object—knows whether its pixels' colors are premultiplied or not, and Quartz will use that knowledge to do the right thing when it draws the source image into the context.
This means that the pixels in your buffer will be premultiplied—not twice, but they will be premultiplied in the context buffer, whether the source colors were premultiplied or not (if they were, no need to multiply again; if they weren't, then it will multiply for the first time).
I don't know enough OpenGL to know whether that's a problem, and if it is, there is probably no solution for this code: On the Mac, at least, Quartz does not support unpremultiplied colors in bitmap contexts.
You might try this storage format instead.
Comment: Thanks Peter. I actually prefer pre-mult as I am a Porter/Duff (inventors of pre-mult approach) devotee. Unfortunately OpenGL is *not* premult so I need to handle that myself. Cheers.
Answer: For future reference to anyone stumbling upon this problem, here's an example on how to de-premultiply:
<code>    /////// ....
    /////// load the image into imageRef up here
    /////// e.g. with CGImageSourceCreateWithURL(); and CGImageSourceCreateImageAtIndex(); on OS X
    /////// or, by accessing CGImage property of UIImage
    ///////
    /////// then extract bitsPerComponent, bytesPerRow and color space
    /////// ....

    // draw CG image to a byte array
    CGContextRef bitmapContext = CGBitmapContextCreate(img->data,
                                                       img->w, img->h,
                                                       bitsPerComponent,
                                                       bytesPerRow,
                                                       space,
                                                       kCGImageAlphaPremultipliedLast);

    CGContextDrawImage(bitmapContext,CGRectMake(0.0, 0.0, (float)img->w, (float)img->h),imageRef);
    CGContextRelease(bitmapContext);

    // let's undo premultiplication
    for(int i=0; i < bytesPerRow * img->h; i+=4)
    {
        for(int j=0; j<3; j++)
        {
            img->data[i+j] = img->data[i+j] / (img->data[i+3]/255.);
        }
    }
</code>
Comment: This won't work if the alpha was 0.  In fact for any pixel where the alpha approaches 0 it gets harder and harder to get the original rgb back out. You simply can't un-premultiply an alpha accurately.  However, you could replace the RGB with a 2nd version of your image that didn't have an alpha channel.  That might work.  like: `img->data[i+j] = img2->data[i+j]`


Sketching the contour map of a function with several variables

Question:
Consider the surface given by the function $f(x,y) = \frac{4}{\sqrt{x^2+y^2-9}}$. Draw the level curves for the values $c = 1$ and $c = 2$. Be sure to label your axes to indicate the scale. Note: When graphing the equation $f(x, y) = c$ here, you will want to rearrange the equation to put it in a recognizable form. Hint: Get the variables out of the denominator and eliminate the radical.
Determine any $y$-intercept(s) of $f$. Write the answer(s) as ordered triples and do not round any of the coordinates.

I was able to find and graph the domain of $f$ and was also able to find the range. However, I'm having a lot of trouble drawing the level curves -- I'm not even sure of the form I should rearrange the equation to. Would someone be able to point me in the right direction?
Answer: Hint
$$ \frac{4}{\sqrt{x^2+y^2-9}}=c \implies  \frac{16}{{x^2+y^2-9}}=c^2\implies x^2+y^2=9+\frac {16}{c^2}$$
Do you recognize something here ?
Comment: Thank you! I've got it now. In terms of y-intercepts, would there be none, since f(x, y) = z could never equal 0, and in order for there to be a y-intercept, z = 0 and x = 0? Also, would the range be 0 to infinity?


Future.cancel() is not cancelling the scheduled execution of ScheduledExecutorService

Question: I am scheduling a task as:
ScheduledExecutorService dataService = Executors.newScheduledThreadPool(1);
Future<?> dataTimerHandle = dataService.scheduleAtFixedRate(runnable, 100, freq, TimeUnit.MILLISECONDS);

This works fine without a flaw.
However, when a certain flag becomes true on user action, the task is no more required periodically, and needs to be executed just once. I then attempt cancelling the task and submitting it just once as follows:
if(!dynamicUpdate) {
    dataTimerHandle.cancel(true);
    dataTimerHandle = dataService.submit(runnable);
}
else { //Reschedule again:
    dataTimerHandle = dataService.scheduleAtFixedRate(runnable, 100, freq, TimeUnit.MILLISECONDS);
}

But seems like the runnable is still executing periodically and cancel() is not working as expected.
Is there an alternate strategy for this?
Comment: Please aso consider that the scheduling can be stopped and resumed whenever the flag 'dynamicUpdate' is re/set.
Answer: The problem is probably not in the Future's cancel() method. Here is a small runnable example that appears to be doing exactly what you want:
import java.util.concurrent.*;

public class CancelPeriodicTask {

    public static void main(String[] args) {

        ScheduledThreadPoolExecutor scheduler = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1);
        scheduler.setRemoveOnCancelPolicy(true);
        try {
            new CancelPeriodicTask().test(scheduler);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            int openTasks = scheduler.shutdownNow().size();
            println("Finished, open tasks: " + openTasks);
            // openTasks will be 1 when RemoveOnCancelPolicy is false
            // and the executor is closed within the scheduled task-period.
        }
    }

    private static long sleepTime = 25L;

    public void test(ScheduledThreadPoolExecutor scheduler) throws Exception {

        // sleep 5 times at scheduled interval
        SleepTask sleepTask;
        ScheduledFuture<?> scheduledSleep = scheduler.scheduleAtFixedRate(sleepTask = new SleepTask(), 0, 2 * sleepTime, TimeUnit.MILLISECONDS);
        sleepTask.sleepTimes.await();
        println("Cancelling scheduledSleep. Done: " + scheduledSleep.isDone() + ", cancelled: " + scheduledSleep.isCancelled());
        scheduledSleep.cancel(true);
        Thread.sleep(2 * sleepTime);
        println("Running sleepTask once.");
        scheduler.submit(sleepTask);
        Thread.sleep(2 * sleepTime);
        scheduledSleep = scheduler.scheduleAtFixedRate(sleepTask, 0, 2 * sleepTime, TimeUnit.MILLISECONDS);
        println("Re-scheduled scheduledSleep. Done: " + scheduledSleep.isDone() + ", cancelled: " + scheduledSleep.isCancelled());
        Thread.sleep(5 * sleepTime);
        println("Cancelling scheduledSleep. Done: " + scheduledSleep.isDone() + ", cancelled: " + scheduledSleep.isCancelled());
        scheduledSleep.cancel(true);
    }

    class SleepTask implements Runnable {

        public final CountDownLatch sleepTimes = new CountDownLatch(5);
        public int sleepCount;

        @Override public void run() {
            println("Sleeping " + (++sleepCount));
            try { Thread.sleep(sleepTime); } catch (Exception e) {
                e.printStackTrace();
            }
            sleepTimes.countDown();
        }
    }

    private static final long START_TIME = System.currentTimeMillis();

    private static void println(String msg) {
        System.out.println((System.currentTimeMillis() - START_TIME) + "\t " + msg);
    }

}

Comment: Thank you!!! Only one complain on answer. I think that `scheduler.setRemoveOnCancelPolicy(true);` is key factor here. That was issue in my case, where after `scheduledFuture.cancel(true)`I would get one more unwanted execution. I'm pretty sure that setRemoveOnCancel should be emphasised in answer, and answer should be marked as correct.
Comment: This was really helful. I tried running the scripts without scheduler.setRemoveOnCancelPolicy(true); and noticed no difference. If you could also help with the side effects of this. Also, is it really required to wait after each cancellation as you have mentioned by Thread.sleep() before each re-schedulling. I don't want to wait for the runnable to finish the run.
Comment: @abksrv It is not needed to wait after each cancellation, it is just there in the example to show the task is no longer scheduled. The RemoveOnCancel only affects when the task is removed from the pool: normally it is only removed from the pool at the next scheduled run.
Answer: This is expected since you are sending the cancel command to the wrong handle. When you call service.submit() it returns a handle for the newly created future, and you can't use the very same handle to send cancel messages to future's created via other calls
Obviously you can shut down the executor service via sevice.shutdown() to not start any runnable submitted after a certain moment
Comment: Please refer to the edited question above. I'm updating the handle. I'm not sure if I can use shutdown() because the flag 'dynamicUpdate' may become true any time and the schedular has to follow in that case.
Comment: You seem to reassign the handle every time the loop iterates, so it can't work. Please, post more code and more requirements
Answer: I think, future.cancel method interrupts the thread the RUNNING thread, so you would need to catch the InterruptedException in the runnable class & just return;


using EntityFieldQuery to find a custom taxonomy field

Question: I have the following query which works:
$query = "SELECT field_code_description_value FROM {field_data_field_code_description} WHERE bundle = :vocabulary AND entity_id in (SELECT entity_id FROM {taxonomy_term_data} WHERE name = :name)";
  $results = db_query($query, array(':name' => $fund_code, ':vocabulary'=>'fund_code'));
  $terms = $results->fetchObject();
I'm trying to convert this into using EntityFieldQuery, as it seems to be the best way to go (and I need some practice), but I couldn't get it right. This is what I tried:
$query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type','taxonomy_term')
      ->entityCondition('bundle','fund_code')
      ->fieldCondition('taxonomy_term_data','field_code_description', $fund_code, '=')
      //->propertyCondition('vid', $fundCodeTaxonomy->vid);

    $results = $query->execute();
    foreach($results['taxonomy_term'] as $tid) {
      $term = taxonomy_term_load($tid);
      dpm($term);
    }
How do I properly use EntityFieldQuery here?
Answer: The code_description field does not appear in the WHERE condition of the original query. Therefore there is no need to add a fieldCondition on the EntityFieldQuery neither.
$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'taxonomy_term')
  ->entityCondition('bundle', 'fund_code')
  ->propertyCondition('name', $fund_code) ;

$results = $query->execute();
foreach($results['taxonomy_term'] as $tid) {
  $term = taxonomy_term_load($tid);
  dpm($term);
  $items = field_get_items('taxonomy_term', $term, 'code_description');
  dpm($items);
}

However there is already the core function taxonomy_get_term_by_name doing exactly what you need here:
$terms = taxonomy_get_term_by_name($fund_code, 'fund_code');
foreach ($terms as $term) {
  dpm($term);
  $items = field_get_items('taxonomy_term', $term, 'code_description');
  dpm($items);
}