Does p-value ever depend on the alternative?

Question: Our tag definition of the $p$-value says

In frequentist hypothesis testing, the $p$-value is the probability of a result as extreme (or more) than the observed result, under the assumption that the null hypothesis is true.

I guess this is how Fisher thought about it, and I am comfortable with it. However, I think I have seen $p$-value being calculated differently in one-sided hypothesis testing. Outcomes that are not in the direction of the alternative do not get considered extreme.
E.g. assume $X\sim N(\mu,\sigma^2)$ and test
$$
H_0\colon\mu=0
$$
against
$$
H_1\colon\mu\neq 0.
$$
Using the empirical mean $\bar x$ as an estimator of $\mu$, the $p$-value is calculated exactly as defined above. If $\bar x$ is far from zero (to either side) in terms of the estimated standard deviation $\hat\sigma$, the $p$-value is low.
Now consider
$$
H_1'\colon\mu>0,
$$
I have seen $p$-value calculated as
$$
\text{p-value}=1-\text{CDF}(t)
$$
where $t:=\frac{\bar x}{\hat\sigma/\sqrt{n}}$ is the $t$-statistic and $\text{CDF}$ is the cumulative density function of $t$ under $H_0$. Then $p$-value is high when $\bar x$ is far to the left of zero, contrary to the case above. $\bar x$ being far to the left of zero is extreme in the perspective of $H_0$, but in an uninteresting direction from the perspective of $H_1'$.
Questions: Does the p-value actually depend on the alternative hypothesis? Or is $\text{p-value}=1-\text{CDF}(\bar x)$ nonsense? Or are there alternative definitions depending on whether one uses Fisher's perspective, Neyman-Pearson perspecitve or some mixture of the two?
Edit 1: The definition of the term extreme appears to be crucial. One way of defining extreme is w.r.t. the probability density of the null distribution at the observed result; the lower the density, the more extreme the result. I guess this is how Fisher would have thought (there was a discussion about it somewhere on CV and/or in some paper, I think; I need some time to find it). Another way is to refer to the alternative hypothesis and pick the "interesting" extremes among all, though in my understanding (which could of course be wrong) this would be in conflict with the CV's definition cited above.
Edit 2: Thanks to Alexis for a good catch: if we are to choose an alternative $H_1'\colon \mu>0$, then the null becomes $H_0\colon \mu \leq 0$, and so values of $\mu$ to the left of zero are no longer extreme under the null. So it appears my example was faulty. Let us switch to another example which hopefully illustrates the main point better. In a multiple linear regression model, consider an overall $F$-test $H_0\colon \beta=0$. The alternative is not one-sided, but the distribution of the test statistic under the alternative is to the right of the null distribution, hence only the right tail is "interesting". The questions remain the same.
Edit 3: Here is a quote from Rob J. Hyndman's blog that, among other things, led to my questions:

Another thing I dislike about statistical tests is the alternative hypothesis. This was not originally part of hypothesis testing as proposed by Fisher. It was introduced by Neyman and Pearson. Frankly, the alternative hypothesis is unnecessary. It is not used in the computation of p-values or for determining statistical significance. The only practical use for the alternative hypothesis that I can see is in determining the power of a test.

(Emphasis is mine.)

A related question: "Defining extremeness of test statistic and defining $p$-value for a two-sided test".
Comment: The alternative tells you which direction(s) more extreme is in.
Comment: "More extreme" is usually not defined or is mischaracterized.  That's why I made the effort to explain this at https://stats.stackexchange.com/a/130772/919. As far as I can tell, that post addresses all the questions you have posed here.
Comment: @whuber, a masterpiece of an answer! A remaining question is, is that a universal definition of $p$-value? Looks like hybrid Fisher-Neyman-Pearson to me as I think Fisher did not consider explicit alternatives.
Comment: @RichardHardy given the title literally assumed an alternative, I was explaining how it works in that specific situation, not giving a lengthy exposition that covers all the potential cases not mentioned by it.
Comment: I find the likelihood ratio a nice and intuitive measure of extreme.
Comment: For all frequentist hypothesis tests, the alternative hypothesis is what the test provides evidence for (upon rejection) or fails to find evidence for (failure to reject). Also note that in $H_{0}: \mu \le \mu_{0}$ with $H_{1}: \mu > \mu_{0}$ the *p* value "depends upon the alternative" in that $H_{1}$ is the complement of $H_{0}$ (so $H_{1}$ tells you everything about $H_{0}$, and vice versa. This complemetarity is also true when the null takes the form $H_{0}: \mu = \mu _{0}$, $H_{0}: \mu \ge \mu _{0}$, or $H_{0}: |\mu - \mu_{0}| \ge \varepsilon$.
Comment: That's right--there are several different theories related to p-values.  The only one in which I was able to find a logical basis for the meaning of "more extreme" is the N-P theory.
Comment: Another example could probably be $F$-test of joint significance of several regressors in a multiple linear regression. The alternative is not one-sided, but given sufficient estimation accuracy, the test statistic will be in the right tail of the distribution under the alternative (not in the left tail), so only the right tail is "interesting".
Comment: @Glen_b-ReinstateMonica, there is no alternative in CV's definition of the $p$-value, so your suggestion does not seem to be compatible with it. Do I understand that right? Also, I would guess Fisher's definition of *extreme* is w.r.t. the density of the null distribution; the lower the density, the more extreme.
Comment: @SextusEmpiricus, ratio ofexactly what to what? I like likelihood by itself in the context of my question, e.g. in contrast to how far in the tail an observation is, which is something that gets mentioned more commonly. But I am more interested in the established definition(s) whether I will like them or not.
Comment: In addition, I believe that extreme does not necessarily mean *most* extreme, and is used in a softer sense to mean a region of values that is large but unlikely under the null hypothesis. The point of is seems to me to test whether a certain theory is likely true by testing whether some observed values correspond to it. The underlying assumption is that if there is no absence of effect then it should be likely to get these extreme values. So, if there is an anticipated effect then sure, why not alter the region of 'extreme' values?
Comment: Likelihood ratio as used by Neyman and Pearson. You might also speak of 'relative extreme'.
Comment: @Alexis, good catch! When constructing the example, I stupidly missed that $H_0$ changes when we switch from $H_1$ to $H_1'$. Then it makes perfect sense to consider only one tail (the "interesting" one) as extreme. So it was a bad example. What about the second example provided in my first comment? Only the right tail is interesting w.r.t. the alternative. Still, an observation in the left tail is extreme (in the sense of low density) w.r.t. the null and so suggests the null might be wrong. (At the same time it provides even more evidence that the alternative is wrong).
Comment: @RichardHardy did Fisher not do so when performing the lady tasting tea test?
Comment: @SextusEmpiricus, cannot remember what happened there, read it a while ago. However, please see my reference to Spanos (1999) Chapter 14 under Vasilis' answer. (To be continued another day.)
Comment: @Glen_b-ReinstateMonica, thank you. I did not intend to confine the discussion to the case when there is an alternative, but my phrasing must have been poor. After all the answers and comments I got, I think I am less confused.
Comment: I'm having trouble seeing how it can "depend on the alternative" when there isn't one. To depend on the alternative, the alternative would need to be able to be different (and thereby, first to exist)
Comment: @Glen_b-ReinstateMonica, I wrote *ever*, not *always*, so I do not see a problem there (but I am not a native speaker). The alternative need not always exist for it to sometimes be relevant. But I take responsibility for not formulating my question well enough. Thank you for your help!
Comment: To answer whether it *ever* depends on the alternative, an answer would have to focus on cases with alternatives. Cases without alternatives - no matter how many you examine, unless it's every possible alternative - can't answer the question. Consider the question "is a car ever red?" -- it doesn't matter how many blue cars you look at, to demonstrate the answer is "yes" you need to seek red ones, and provide any red one as an example. Conversely to demonstrate that it can ever depend on the alternative, you examine pairs of cases with different alternatives, & find a single pair where it does
Comment: @whuber, I have taken the liberty to append the [tag info](https://stats.stackexchange.com/tags/p-value/info) of the $p$-value tag with an explication on what *extremity* means. There are two new sentences in parentheses. Would you please take a look and see if I did not write something stupid? Thank you!
Comment: I see nothing stupid about that characterization, Richard.  Others may take a broader view of the situation (going back to Fisher's approach) than I do, so I wouldn't be surprised if the community were to continue to refine this tag wiki.
Comment: @whuber, alright, thank you very much!
Comment: *"In a multiple linear regression model, consider an overall $F$-test $H_0\colon \beta=0$. The alternative is one-sided.* The alternative is sort of one-sided in terms of the F-statistic. Under the null hypothesis $H_0: E(y|x) = \hat y|x,\beta=0$ the F statistic is distributed according to a F distribution. The alternative hypothesis that $H_1: E(y|x) \neq \hat y|x,\beta=0$ results in a [non-central F-distribution](https://en.wikipedia.org/wiki/Noncentral_F-distribution).
Comment: @SextusEmpiricus, I guess we do not disagree. An different example where both tails of F-statistic are of interest arises in checking random number generators. On the one hand, we do not want the generated numbers to display pronounced patterns (right tail of F), as they are supposed to be random. But we do not want them to be "too random" either (left tail of F), as truly random numbers tend to show *some* patterns by chance. But this is another topic.
Comment: @RichardHardy that is actually an interesting connection. What is extreme randomness? If you wish to test some random number generator, then you have some likelihood function (and this is new to me, but this is by means of some F-score?), but this aggregates different classes of patterns....
Comment: @SextusEmpiricus, I studied this some 13 ago, and I do not remember the details anymore. I just remember looking for some prespecified patterns (like autocorrelation of some sort). If a pattern is completely absent (autocorrelations at multiple lags exactly = 0), this is too good to be true/realistic, hence the relevance of the left tail of the $F$-test.
Comment: ....a similar thing occurs when you wish to estimate the value $p$ for a Bernoulli distributed variable. The likelihood ratio can be summarized in terms of the sufficient statistic (the order of heads and tails is not important, and it is just the total number). So we do not look at the particular observation, but only at the sufficient statistic. Now I wonder what would be a sufficient statistic for a random number? I can only imagine that the choice of the patterns used to express the F-score are arbitrary. Finding some way to define 'extremeness' for random numbers might be useful here.
Comment: @RichardHardy: I think the paper you couldn't recall may be this one: [Christensen (2012), *Am. Stat.*,  **59**, 2, "Testing Fisher, Neyman, Pearson, and Bayes"](https://doi.org/10.1198/000313005X20871). Note the claim in Section 2 is not that a 'Fisherian' test *must* use density under the null to define *extreme*, but that it *can* do. I don't know that Fisher ever preached or practised that: see [How does Fisher calculate his p
-value?](https://stats.stackexchange.com/q/418437/17230)
Answer: The alternative hypothesis affects the test through the evidentiary ordering
The p-value of a classical hypothesis test depends on the alternative hypothesis only through the evidentiary ordering constructed for the test, which is what tells you what constitutes an "extreme" observation.  Any hypothesis test involves specification of an evidentiary ordering which is a total preorder on the set of all possible outcomes of the observable data. This evidentiary ordering specifies which outcomes are more conducive to the null or alternative hypotheses respectively.  Often this ordering is implicit on having defined a "test statistic" for the test, but the only role of the test statistic is to represent this ordering.

Hypothesis testing via an evidentiary ordering: Suppose we want to create a hypothesis test for an unknown parameter $\theta \in \Theta$ using an observable random vector $\mathbf{X} \in \mathscr{X}$ with a distribution that depends on this parameter.  For any stipulated null space $\Theta_0$ and alternative space $\Theta_A$ (and any particular logic for how the test works) we contruct a total preorder $\succeq$ on the outcome space $\mathscr{X}$.  The interpretation of the total preorder is:
$$\mathbf{x} \succeq \mathbf{x}'
\quad \quad \iff \quad \quad
\mathbf{x} \text{ is at least as conducive to } H_A \text{ as } \mathbf{x}'.$$
For brevity, we sometimes say that $\mathbf{x} \succeq \mathbf{x}'$ means that $\mathbf{x}$ is at least as "extreme" as $\mathbf{x}'$. This is a common shorthand, which is okay if you remember that "extreme" means "conducive to the alternative hypothesis" in this context. In practice, the evidentiary ordering for the sample space is often set implicitly by constructing a test statistic and having an ordering for the test statistic.$^\dagger$  This is essentially just the same thing, but removed one-level of abstraction away from the sample space in order to concentrate entirely on the ordering.
You should bear in mind that the situation is complicated slightly by the fact that the "test statistic" and "evidentiary ordering" may both be dependent on the parameter in the analysis.  In fact, the "test statistic" is typically not a statistic, but often instead a pivotal quantity that depends on the parameter value in the hypothesis.  In this case the evidentiary ordering will also be implicitly dependent on the parameter.  (In the case of a test with a simple null hypothesis this doesn't matter, because the test always conditions on this single value.  In the case of a test with a composite hypothesis it might matter, because the ordering now changes over the null space.)  Taking this dependence as implicit for now, once we have set the evidentiary ordering for the test, the p-value function is then defined as:
$$p(\mathbf{x}) \equiv \sup_{\theta \in \Theta_0} \mathbb{P}(\mathbf{X} \succeq \mathbf{x} | \theta)
\quad \quad \quad
\text{for all } \mathbf{x} \in \mathscr{X}.$$
If we denote the probabilistic model by $\mathbb{M} \equiv \{ \mathbb{P}(\cdot | \theta) | \theta \in \Theta \}$ then we can write the p-value function as a function $p = f(\mathbb{M}, \Theta_0, \succeq)$. We can see that the p-value function is fully determined by the model $\mathbb{M}$, the null space $\Theta_0$ and the evidentiary ordering $\succeq$.  Observe that the alternative hypothesis only affects this function through its contribution to the evidentiary ordering.
The structure of a classical hypothesis test is shown in the diagram below. This diagram illustrates the elements of the test that lead to the p-value function, which ultimately defines the test. As can be seen in the diagram, the alternative hypothesis has a limited role; combined with the logic of the test it affects the evidentiary ordering for the test, which then flows into determination of the p-value function.

One final thing that is worth noting in relation to this issue is that a classical hypothesis test is essentially an inductive analogue to the deductive proof by contradiction (see related answers here and here).
In a proof by contradiction we begin with a null hypothesis, show that this leads logically to a contradiction, and therefore reject the initial premise that the null is true (with certainty).  In a classical hypothesis test, we begin with a null hypothesis, show that this leads to a highly implausible result in favour of the alternative (so not quite a deductive contradiction, but close), and therefore reject the initial premise that the null is true (but with some uncertainty).  The p-value in this test is the probability of a result at least as conducive to the alternative hypothesis, assuming the null is true.  If this is low then it means that something very implausible happened (under the assumption that the null is true) which gives the "contradiction" in the "inductive proof by contradiction".  Just as with a proof by contradiction, this procedure is asymmetric in the hypotheses and it is only necessary to look at things conditional on the null hypothesis.  The alternative hypothesis contributes only to the extent that it helps us understand what is a more implausible result under the null hypothesis (i.e., through the evidentiary ordering) which allows us to inductively measure the "implausibility" that is analogous to a contradiction.

$^\dagger$ Often the evidentiary ordering is set implicitly using a test statistic $T: \mathscr{X} \rightarrow \mathbb{R}$ and then using the ordering defined by the correspondence $\mathbf{x}_0 \succeq \mathbf{x}_1
\iff
T(\mathbf{x}_0) \geqslant T(\mathbf{x}_1)$.  In this case the p-value reduces to $p(\mathbf{x}) = \sup_{\theta \in \Theta_0} \mathbb{P}(T(\mathbf{X}) \geqslant T(\mathbf{x}) | \theta)$.
Comment: That was technical... But helpful nonetheless. But technical!
Comment: @RichardHardy (&Ben) Just to add some confusion: A nice example in which evidential ordering is hard to define is sequential testing, in which one might stop conditionally on having achieved significance, but having not stopped at a certain time point (because at this point the result wasn't significant) one may achieve stronger significance later, all results adjusted appropriately. See https://itschancy.wordpress.com/2019/02/05/the-sev-function-just-plain-doesnt-work/
Comment: Courtesy to @RichardHardy, came across this ingenious treatment. This formalism should be universally used as the total ordering aka the _evidentiary ordering_ is more unequivocal than the more conventional _extreme_. Already +1.
Answer: The test statistic ($t$ in your example) and all calculations to reach that point depend only on the null hypothesis $H_0$ and nothing else.
The p-value is affected by the alternative hypothesis $H_1$ as the $H_1$ identifies which values are considered as "extreme" values and the p-value calculates the proximity of the final result (your $t$) to those values.
For instance, in your example of $H_0$ vs $H_1'$ you would reject $H_0$ only if $t>T_\alpha$ and for the example $H_0$ vs $H_1$ you would reject $H_0$ only if $t>T_{\alpha/2}$ or $t<-T_{\alpha/2}$.
Thus the p-value of $H_0$ vs $H_1$ would be the probability of the union of two sets whereas the $H_0$ vs $H_1'$ would be the probability of one set where the cut-off point is higher on the x-axis compared to the previous case.
EDIT: In response to what you mentioned about Fisher, I believe you are referring to the famous lady testing test. Which indeed doesn't have a strictly speaking alternative hypothesis but it is slightly different compared to the hypothesis tests that we usually conduct.
In this example, he only defined the null hypothesis $H_0$: She has no ability to distinguish the tea and he used the combination formula to measure the probability of all possible outcomes given that $H_0$ is true which is essentially the p-value of each data point.
The main difference/trick here that you might be looking for is that in Fisher's eyes, it would only take one incorrect guess to make her a liar and thus he wanted to identify the smallest amount of cups that he needs to give her to taste. In a sense, one might say that he tested the $H_1:$At least one incorrect guess and he looked for the smallest possible sample size for some pre-defined parameters.
This is a slightly different case to the way we usually conduct statistical hypothesis tests as we take sample from a population and we usually "allow" some non-$H_0$ cases. I guess the final answer to your question is that we want an $H_1$ or at least a "loose-definition" of it in order to define what are the "extreme-departures" from the $H_0$ (Even if you are Fisher and you hide it well enough).
Really good question by the way :)
Comment: Thank you for your answer! It is bridging the gap pretty nicely. However, I think Fisher did not consider an alternative hypothesis (it came later with Neyman-Pearson), yet he defined the $p$-value. Could you please comment on that?
Comment: Ah yes, I think you know to what you are referring to, let me edit my answer
Comment: Thanks for the update. I think I found what I was looking for, thanks to @whuber's comment and Spanos ["Probability Theory and Statistical Inference"](https://www.cambridge.org/core/books/probability-theory-and-statistical-inference/3E3044C9FC065D9A160A071DA209B2D9) (1999), Chapter 14. The latter chapter contains what I must have meant. According to my understanding of Spanos interpretation of Fisher, there is no alternative there, while the idea of the test is to see how well the data is compatible with $H_0$. ctd...
Comment: ...Fisher's implicit alternative is much broader than the explicit alternative of Neyman-Pearson (NP). Under the alternative Fisher allows for the model to be misspecified in an arbitrary way, while NP remain within the specified model. It is too late for me to continue today, but I will return. +1 anyway.
Comment: @RichardHardy I believe the goal is to test whether the data is incompatible with $H_0$. Fisher's hypothesis testing is a search for anomalies (with regard to the theory that there are no effects).
Comment: @SextusEmpiricus, sounds right. Thank you.
Answer: Here's how I see it.
Generally, the p-value is the probability, under the null hypothesis, to observe a result that is as far or farther away from what would be "typical" under the null hypothesis.
This is informal and imprecise, and in fact decisions need to be made in order to make this precise.
What needs to be chosen is a test statistic, and "far or farther away" needs to be defined. One way of doing this is to specify an alternative, and to ask, how does a test need to look like in order to achieve optimal power under the alternative while respecting the nominal level under the null hypothesis? This is Neyman and Pearson's approach to construct tests. Given the test statistic and the "direction" of the alternative (one-sided or two-sided, although in principle more complex "direction definitions" are conceivable), the p-value is computed using the null hypothesis and not the alternative. However the alternative has its impact through the determination of test statistic and direction.
Alternatively (!) one can choose the test statistic without specifying an alternative based on a (usually heuristic) idea of how to measure "closeness" to the null hypothesis (e.g., Kolmogorov-Smirnov or $\chi^2$-distance from the hypothesised distribution), preferably in such a way that the distribution of the test statistic under the null can be specified. This also requires a "direction", but this seems often trivial (e.g., large KS-distance => evidence against the null). In this case one could argue that there is no alternative and therefore no impact of the alternative on the p-value. One could however also say that a test constructed in this way implicitly defines an alternative, namely "all distributions that tend to generate test statistic values in the rejection direction".
As an example, consider the KS-test for normality. Though counter-intuitive at first sight, it is conceivable to reject normality in case of a too small KS-distance, which makes sense in an application in which it is suspected that researchers faked data in order to make them look "perfectly normal". The alternative to i.i.d. normality is then implicitly the family of distributions that tend to generate samples that look more normal than i.i.d. normal samples, for example because observations are dependent in such a way that a small KS-distance is enforced.
The p-value is then different from the standard KS-test. Not sure whether you'd say "this is because it depends on the alternative" because all these choices could be made without explicitly specifying an alternative, however it can for sure be related to the concept of an "implicit alternative" defined by the test.
Comment: That is helpful, thank you.
Answer: My direct answer to the question is that p-values clearly do not depend on the alternative hypothesis, as the alternative is not present in the calculations, but at the same time p-values are dependent on the alternative hypothesis insofar as a one-tailed p-value is different from a two-tailed p-value and we typically use the alternative hypothesis to specify the number of tails.
The difficulties here might be the result of some standard shortcomings of statistical description and definition and so I will address those as I unpack the direct answer a bit.
'Hypothesis'
What is a hypothesis for the purposes of a significance test? It's what I call a 'statistical hypothesis' rather than a hypothesis regarding the real world. To make that distinction clear, consider the hypothesis that the sun rises in the east because of the way that the Earth rotates around its north-south axis. That hypothesis is not one that can be plugged into a t-test, for example. Now consider the hypothesis that the mean number of bubbles in a typical pint of Guinness stout brewed with yeast A is equal to the number of bubbles in a typical pint Guinness brewed with yeast B. That hypothesis can be treated a statistical hypothesis that can be evaluated using a t-test because counts of bubbles from multiple pints can be converted into an observed value of the test statistic, t.
A statistical hypothesis is nothing more than a point or a region within the parameter space of the statistical model chosen for the analysis. For the t-test, the null hypothesis is present in the calculation of the t-value (although it is frequently omitted from textbook formulas, with dire consequences!). Let's use $\bar{x}_A$ to be the mean of the first group of bubble counts and $\bar{x}_B$ to be the mean of the second, $SED$ to be the standard error of the difference between those means, and $\delta_0$ to be the null hypothesis. The calculation of the observed t-value is then
$$t=\frac{(\bar{x}_A-\bar{x}_A)-\delta_0}{SED}$$
Yes, in this case $\delta_0$ is zero and so it can be left out of the formula without changing the numerical result, but it should never be omitted for two reasons: explicitness helps to reduce confusion, and the null hypothesis is not always zero (the dreaded 'nill-null') and so it cannot always be omitted!
The p-value is determined by finding the extremity of the observed t-value compared to the distribution of Student's t, and so that equation demonstrates immediately that the p-value depends on the null hypothesis. The absence of the alternative hypothesis similarly demonstrates the irrelevance of the alternative to the p-value. (Yes, it's relevance is still to come, read on.)
In the previous paragraph I rely on the undefined 'extremity' to do a lot of work and so I need to unpack it a bit. I will say initially that it is the statistical model that defines what is meant by extreme, and it does that by providing a theoretical sampling distribution of the test statistic against which the observed value of test statistic can be calibrated. If the observed test statistic value falls near the centre of that distribution then it is not extreme, but if it falls towards one or other edge of the distribution then it is extreme in some proportion to the nearness to the edge. (I'm deliberately ignoring the complications of multimodal distributions, and of two-tailed p-value because the evidential interpretation of a neo-Fisherian p-value is assisted by the routine use of one-tailed p-values.)
One way to express the extremeness of an observed test statistic is as the integral of the sampling distribution from the observed value out to the end. That integral is a probability and hence the usual definition of a p-value as a probability. However, because some of the most damaging misconceptions about p-values relate to (or depend on) its probabilistic nature, it can be helpful to think of a p-value as a fractional ranking of extremeness rather than a probability of observing something more extreme. I'll use a simple permutations test to illustrate that idea.
To perform a permutations test you delineate all possible arrangements of the data (with the statistical model being no more than the assumption of data exchangeability under the null hypothesis), and order those arrangements according to their corresponding test statistic values. (The test statistic is commonly the means, but can be the medians or any other interesting statistic calculated from each data arrangement.) Those ordered values of test statistic define the test statistic sampling distribution under the null hypothesis according to the model.
To get the p-value you simply determine the numerical rank of the test statistic value for the observed data arrangement within that distribution, and divide that rank by the total number of possible arrangements to get the p-value. In other words, the p-value is a fractional ranking and encodes how strange it would be to obtain the observed data arrangement according to the model if the null hypothesis is true.
Alternative hypothesis
There are two different alternative hypotheses that need to be considered. The first is the specific effect size that serves as the alternative hypothesis during the planning stage of a hypothesis test (e.g. the effect size to be plugged into the calculation of statistical power that can be used for sample size determination). That pre-data alternative has no effect on the data actually observed and hence no effect on the observed p-value.
The other alternative hypothesis is the one that the original question refers to. It is the complement of the null hypothesis and, given that a p-value depends on the null hypothesis, one might reasonably consider the p-value also depends on the alternative. However, I prefer to think that it is the null hypothesis that is doing the work, partly because the null appears (should appear!) in the formulation of a test statistic, but also because the alternative hypothesis is usually a range (or ranges) of parameter values in the statistical model whereas the null is usually a single point.
Comment: That is helpful! A nitpick: *If the observed test statistic value falls near the centre of that distribution then it is not extreme, but if it falls towards one or other edge of the distribution then it is extreme in some proportion to the nearness to the edge.* I prefer referencing the density over centrality. If the test statistic had a symmetric U-shaped distribution under $H_0$, I would choose the centre as my $\alpha\%$ rejection region. This does not matter for a $t$-test though, since the centre happens to coincide with the highest density region.
Comment: Arrgh, I should have read on. A couple of sentences down from there you explain this, too. But still I think it is pedagogical to refer to the density rather than centrality (just as you have insisted on keeping $\delta_0$ in the test statistic even though in many cases it is zero – and I fully agree with you there).
Comment: Still thinking about all this, I remembered a related question of mine: ["Defining extremeness of test statistic and defining $p$-value for a two-sided test"](https://stats.stackexchange.com/questions/483685). I have been looking for an intuitive answer to it for a long time.
Comment: @RichardHardy The simple answer is that you don't need to because you should use a one-sided p-value whenever you are doing a significance test. Two tails are only advisable when you are doing a hypothesis test where you have no firm expectation of effect direction, and you probably should never do that!
Answer: The reason that results in the wrong direction don't give small p-values is because they provide terrible evidence in favor of the alternative. Imagine a null hypothesis of a fair coin and an alternative of a bias towards heads. You then flip the coin 100 times and get 99 tails. You have terrible evidence in favor of your alternative hypothesis.
This can apply in other settings. Think of an F-test comparing the variances of two distributions. If you think the distribution with its variance on top has higher variance but wind up with a variance ratio $<1$, you have rather poor evidence that the distribution on top has higher variance than the distribution on the bottom.
$$F_0=s_1^2/s_2^2$$
If $s_1^2<s_2^2$, your evidence is quite poor that $\sigma_1^2>\sigma_2^2$.
Comment: Thank you for your insight! This is a great answer to a question that I did not ask. But what about the questions that I did ask?
Comment: I think it does answer your question. When you do one-sided testing in one direction, the p-value is the $1-CDF(\bar{x})$ that you posted. In the other direction, the p-value is just $CDF(\bar{x})$. I'll make an edit with some pictures later today or tomorrow.
Comment: Thank you, Dave. It is not that I do not understand your example. I also think I got the intuition. My questions are about the definition(s) of $p$-value and appropriate use of terminology.


Using POS Tags and NERs as Features for Text Classification or Sentiment Analysis

Question: I am trying to implement text classification and sentiment analysis from the documents.
I always use POS tags as features in the following way.

Mike is playing football

I would convert it into this format: Word_POS

Mike_Noun is_Verb playing_Verb football_Noun

I wanted to know what are the ways I can use NER as features. One of the ways I use is by taking count of NERs as Features. So my sentence would be

Mike_Noun is_Verb playing_Verb football_Noun 0 0

Where 0 is the number of ORG-organisations entities and another 0 is the number of e.g., DATE entities.
So I have 2 questions:
What are the other ways we can use POS tags and NERs as features in

Without deep learning?
With deep learning

Also it would be really helpful if you could share resources where I can learn more about feature engineering for Text Data.
Answer: I don't think adding counts of named entities can help. You should ask a question: do you think that you would be able to better decide about sentiment of a sentence if you knew the number of entities in the sentence? I don't think so. If you prefer more data-driven reasoning, you can try to plot your training data with respect to the features and have a look if the features seem to separate the classes in the train data. If not, they are probably useless.
When using deep learning, we usually do not explicitly introduce linguistically features. A typical approach would be using a pre-trained encoder (such as BERT) or pre-trained word embeddings and add an LSTM/CNN to get a single vector from the embeddings. The pre-trained components are usually somehow aware of the linguistic features and using them as an explicit input does not further improve the performance.
Comment: What is your opinion on using POS tags as features??Is there any other way?


Is cURL standard part of all Unix-like operating systems?

Question: I'm writing a shell script that needs to download some data from the Internet and I was just thinking if I can rely on cURL that it's pre-installed on all Unix distributions.
I know that wget is not on OS X by default but cURL is. I also remember me installing cURL on Ubuntu Server but it's already a year or two. I tried Ubuntu Server 12.04 today and it comes with cURL out of the box.
Comment: Tried it right now, Ubuntu Desktop 12.04 is not distributed with cURL.
Answer: No, cURL is not a standard part of operating systems.  It isn't ever standard on all Linux based distributions.
Comment: Is there some kind of list available for each distribution with all pre-installed commands?
Comment: As per https://daniel.haxx.se/docs/curl-vs-wget.html, "**curl** comes pre-installed on **macOS** and **Windows 10**. **Wget** does not."
Answer: Neither curl nor wget are "guaranteed" to be installed anywhere, especially on proper UNIX systems.
They are not POSIX standards.  Neither is ftp, ssh / scp / sftp, rsync, telnet, nc / netcat, openssl, or probably any related tool that comes to mind.
It seems like an odd oversight to me, but that is how it is.
Various GNU/Linux distros may include curl and/or wget, but YMMV.
FreeBSD comes standard with the "fetch" tool for cases like this and OpenBSD comes with a souped-up "ftp" client that can do the job with it's "AUTO-FETCHING" feature.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/
To my knowledge, there is no "tcp" file transfer tool defined by POSIX at all.  uucp is standard, but I do not know if you could even make that work without config changes on both ends.
You can test for both of them (as many have suggested), but to be certain you would either need to install something or write something.
A shell with tcp socket support (like ksh93 or Bash) should let you write a function call in a pinch. Of course, you still need the proper permissions to read / write to the socket.
GNU awk ("gawk") can do it, however the odds of having gawk and nothing more convenient like curl or wget seems slim to me.  Pretty sure POSIX awk does not support networking and I do not recall seeing anything about it in "The AWK Programming Language" but it has been a while.
Of course, Perl, Python, Ruby, C, etc. can do it also.
See:
https://www.shell-tips.com/bash/download-files-from-shell/
https://unix.stackexchange.com/questions/83926/how-to-download-a-file-using-just-bash-and-nothing-else-no-curl-wget-perl-et
https://unix.stackexchange.com/questions/336876/simple-shell-script-to-send-socket-message
Note: As of 2022, Project Gutenberg no longer seems to support insecure downloads over Port 80, so the script example below has been updated with a HTTP mirror.
#!/bin/ksh

## Tested with ksh93u+ and Bash v3.2x
## Not tested with Binary files
## HTTP/1.1 will cause output to hang

#xHOST=gutenberg.org
#xDOC=/files/84/old/84.txt

gpHOST=mirrors.xmission.com
gpDOC=/gutenberg/8/84/old/84.txt
gpPORT=80

exec 3<>/dev/tcp/${gpHOST}/${gpPORT}
printf "GET ${gpDOC} HTTP/1.0\r\nHost: ${gpHOST}\r\n\r\n" >&3

# drop the header from the data stream
while IFS= read -r line ; do
   [ "$line" == $'\r' ] && break
done <&3

cat <&3 | tee Frankenstein.txt

Pro tip: "tar" is not POSIX either!  Use "pax" instead.
Answer: Everything else equals I would say it being more likely that you have wget installed.
Yet, why not simply make a conditional, looking for both wget and curl in the PATH, and use whatever is available, if any? If you want to be ambitious, also feel free to through in lynx, w3m, etc in the mix.
Comment: Debian doesn't include either wget or curl
Answer: Since some machines tend to have curl and others tend to have wget pre-installed, I sometimes use this in Bash scripts :
# use curl or wget, depending on which one we find
curl_or_wget=$(if hash curl 2>/dev/null; then echo "curl -s"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi);

if [ -z "$curl_or_wget" ]; then
        echo "Neither curl nor wget found. Cannot use http method." >&2
        exit 1
fi

x=$($curl_or_wget "$url")

Answer: Thought I'd update here since the answers are almost 10 years old.
linuxize.com

The curl package is pre-installed on most Linux distributions today.

However when automating you probably don't want to expect it. I'm using this but improvements would be great :)
if glide --version; then
    echo "Glide is installed"
else
    echo "Installing Glide."
    if curl; then
        curl https://glide.sh/get | sh
    elif wget; then
        wget https://glide.sh/get | sh
    else
        echo "curl and wget are not available, install glide manually"
    fi
fi

Answer: It should be available for all major distributions, though not considered part of OS generally, and if it is not installed, any admin can install it. since it is opensource, even distros that don't have it can compile it. There is a windows port as well.
Answer: It doesn't come with AIX. It's a pain in the butt to install on AIX, lots of dependencies.


Why are Raiders stand higher than Chiefs after week 1?

Question: Each have one win. According to tiebreaking procedures, it should be:

H2H record (not applicable)
Record within division (not applicable)
Record in common games (not applicable)
Record within conference (equal)
Strength of victory (not applicable, none of the opponents have a record yet)
Strength of schedule. They have mostly same schedule, except 2 games where Chiefs are playing AFC division champions and Raiders are playing the second place teams.

So according to rule 6, Kansas has a stronger schedule. Am I wrong?
Answer: You're misinterpreting "strength of schedule" - that refers to the combined record of every opponent the team has played in the current season; it's nothing to do with how those teams performed last season. Quoting from SB Nation:

Strength of victory is calculated by combining the winning percentages of every opponent a team has beaten. Strength of schedule is similar, but combines the winning percentage of every one of a team's regular season opponents, win or lose.

As both teams have won their only game, their strength of victory and strength of schedule are identical. However, the next tiebreaker is now

Best combined ranking among conference teams in points scored and points allowed.

As the Chiefs beat the Patriots 42-27 they have a +15 ranking while the Raiders have a +10 ranking from beating the Titans 26-16, so I think the Chiefs should still be ranked higher than the Raiders. (Also, the Broncos should be ranked even lower, having won only 24-21).
I personally suspect the algorithms being used for generating the on-line standings just don't go that far down the tiebreaker procedures; I don't believe anything later than strength of victory has ever actually been needed.
Comment: Exactly. Chiefs scored more points, and have a better points differential. Regarding the Broncos, they have a better record in-division because they beat Chargers.


Variable elimination - can I start from the middle of the network

Question: I read that while doing inference in Bayesian network with variable elimination, any order will do.
Say I want to have the below network, where every random variable can be T/F with given probabilities given its parents state.
I want to calculate P(B=T), and I want to eliminate P(A) first. Can I do that?
I started by writing:
$P(B) = \sum_a\sum_e\sum_j\sum_mP(B=T)P(E)P(A|B,E)P(J|A)P(M|A) =$
$\sum_e\sum_j\sum_mP(B=T)P(E)\sum_aP(A|B,E)P(J|A)P(M|A)$
and now I'm stuck :(
Can you show me explicitly how do I do this first factoring step?
Thanks.
Answer: This is a good text for understanding how simple it is: UCLA.
Apparently, what needs to be done is:

multiply (cartesian-join) the factors (probability tables) $P(A|B,E)$, $P(J|A)$ and $P(M|A)$ by their common variables - in this case values of $A$, and multiply the 3 numbers for each row. We'll get a table with 5 columns and 2^5 = 32 rows.
Sum out A - sum each 2 records with same values of $B,E,J,M$ and different values for $A$ (T,F), and get a table with 4 columns and 16 rows.


SKAudionode and Swift3

Question: it been a year since I open my Xcode project and I see that there are some errors.
Now I'm having problem with my audio code. it use to work good a year ago but now it says "fatal error: unexpectedly found nil while unwrapping an Optional value"
sample
Comment: Post your code snippet as text in your answer instead of an image.
Answer: Try doing something like this
   self.run(SKAction.playSoundFileNamed("MexicanPause.wav", waitForCompletion: false))

I don't know where you're trying to implement your code, but this can be add in touchesBegan or maybe even touchesEnded.

SKAudionode and Swift3


URL routing in Node.js

Question: Homework done:
The Node Beginner Book
How do I get started with Node.js [closed]
Structuring handlers in Node
Backstory: I wanted to try and write my own framework but I'm running into some troubles, most likely due to not understanding it fully.
What I want to achieve is a syntax that looks like this:
var app = require('./app'); //this part is understood and it works in my current code.
app.get('/someUrl', function(){ //do stuff here });
app.post('/someOtherUrl', function(){ //do stuff here });

I know of the Express-framework that has this same syntax but reading their source code still eludes me.
This might be a trivial task to achieve but I simply can't produce it, yet.
Trying to require('./app'); in a file deeper in the application produces a undefined object, so I'm guessing that a server is a singleton object.
So what have I tried?
My current code looks like this, and somehow I feel like this is the way to go, but I can't apparently do it like this.
I'm omitting all the require(); statements to keep it more readable.
server.js:
var app = module.exports = {
    preProcess: function onRequest(request, response){
        processor.preRequest(request); //this object adds methods on the request object
        var path = urllib.parse(request.url).pathname;
        router.route(urls, path, request, response);
    },
    createServer: function() {
        console.log("Server start up done.");
        return this.server = http.createServer(this.preProcess);
    }
};

exports.app = app;

At the time of writing I'm experimenting with extending this object with a get() method.
index.js:
var app = require('./server');
app.createServer().listen('1337');

the router.route() bit basically sends the request onward into the application and inside the router.js-file I do some magic and route the request onward to a function that maps (so far) to the /urlThatWasRequested
This is the behavior I'd like to leave behind.
I know this might be a pretty tall order but all my code is easily discardable and I'm not afraid of rewriting the entire codebase as this is my own project.
I hope this is sufficient in explaining my question otherwise, please say what I should add to make this a bit more clear.
Thanks in advance!
Answer: I'm not exactly sure what your question is, but here's some thoughts:
1) You are creating a circular reference here:
var app = module.exports = {
    // some other code
}
exports.app = app;

You add app as a property of app. There's no need for the last line.
2) You need to hold handlers in app. You can try something like this:
var app = module.exports = {
    handlers : [],
    route : function(url, fn) {
        this.handlers.push({ url: url, fn: fn });
    },
    preProcess: function onRequest(request, response){
        processor.preRequest(request);
        var path = urllib.parse(request.url).pathname;
        var l = this.handlers.length, handler;
        for (var i = 0; i < l; i++) {
            handler = this.handlers[i];
            if (handler.url == path)
                return handler.fn(request, response);
        }
        throw new Error('404 - route does not exist!');
    },
    // some other code
}

Note that you may alter this line: if (handler.url == path) in such a way that handler.url is a regular expression and you test path against it. Of course you may implement .get and .post variants, but from my experience it is easier to check whether a request is GET or POST inside the handler. Now you can use the code above like this:
app.route('/someUrl', function(req, res){ //do stuff here });

The other thing is that the code I've shown you only fires the first handler for a given URL it matches. You would probably want to make more complex routes involving many handlers (i.e. middlewares). The architecture would be a bit different in that case, for example:
preProcess: function onRequest(request, response){
    var self = this;
    processor.preRequest(request);
    var path = urllib.parse(request.url).pathname;
    var l = self.handlers.length,
        index = 0,
        handler;
    var call_handler = function() {
        var matched_handler;
        while (index < l) {
            handler = self.handlers[index];
            if (handler.url == path) {
                matched_handler = handler;
                break;
            }
            index++;
        }
        if (matched_handler)
             matched_handler.fn(request, response, function() {
                 // Use process.nextTick to make it a bit more scalable.
                 process.nextTick(call_handler);
             });
        else
             throw new Error('404: no route matching URL!');
     };
     call_handler();
},

Now inside your route you can use
app.route('/someUrl', function(req, res, next){
    //do stuff here
    next(); // <--- this will take you to the next handler
});

which will take you to another handler (or throw exception if there are no more handlers).
3) About these words:

Trying to require('./app'); in a file deeper in the application produces a undefined
  object, so I'm guessing that a server is a singleton object.

It isn't true. require always returns the reference to the module object. If you see undefined, then you've messed up something else (altered the module itself?).
Final note: I hope it helps a bit. Writing your own framework can be a difficult job, especially since there already are excelent frameworks like Express. Good luck though!
EDIT
Implementing .get and .set methods is actually not difficult. You just need to alter route function like this:
route : function(url, fn, type) {
    this.handlers.push({ url: url, fn: fn, type: type });
},
get : function(url, fn) {
    this.route(url, fn, 'GET');
},
post : function(url, fn) {
    this.route(url, fn, 'POST');
},

and then in routing algorithm you check whether type property is defined. If it is not then use that route (undefined type means: always route). Otherwise additionally check if a request's method matches type. And you're done!
Comment: Thanks for the answer! :) My question was "How to produce a .get(), .post() syntax". I might have to alter the question. The reason why I would like to refrain from checking for get/post in the function itself is just because of preference in this project. Thanks for pointing me in the right direction! :) Also I have a handlers-file where I add a lot of handlers that map to functions by the same name I'm not sure if that was made clear.
Comment: this might be a totally moronic question but the handlers property isnt instatiated, it's just undefined.
Comment: @limelights I've defined it at the begining of **2)** section: `var app = module.exports = { handlers : [],`.
Comment: yeah, i know, but it still turns up as undefined when i run it and i cant for the love of god see why it does it.
Comment: @limelights Hmm, maybe you should post the code you've written and we look at it?? Remember that `this` always refers to the **holder** of a function (`app` in my case). Also you may trying converting the code into classical `function()` style, instantiate it and export it.
Comment: I'll try to the export but otherwise im using your exact code for testing purposes. :)
Comment: i got it to work! I did learn a lot as well! :) Thanks for the excellent answer! :)

URL routing in Node.js


Use for loop variable as dictionary key in Django template

Question: I am trying to get the following working.
The count loop needs to loop through for all values, and there may not be a user associated with each count, but the count value i needs to be used in each loop to pass to the JavaScript.
python part:
users = {}
users[1]={}
users[1][id]=...
users[1][email]=...

...

count=[1,2,3,4,5,6,7,8,9,10]

Django template part:
{% for i in count %}
do some stuff with the value i using {{i}} which always returns the value, i.e. 1

email:{% if users.i.email %}'{{users.i.email}}'{% else %}null{% endif%}
{% endfor %}

This returns nothing for email.
When I substitute the number 1 for i in {% if user.i.email %} email returns the users email address.
I'm using the data in JavaScript, so it needs to be implicitly null if it doesn't exist.
I can't seem to get Django to recognize the i variable as a variable instead of the value i.
using [] doesn't work, as it throws an invalid syntax error
email:{% if users.[i].email %}'{{users.[i].email}}'{% else %}null{% endif%}

I have tried using "with" statement
{% for i in count %}{% with current_user=users.i %}...

and then using current_user.email, but returned nothing
Have also tried
{% for i in count %}{% with j=i.value %}...

just in case it would work, and then trying to use j, but same result.
I have thought about creating an inner for loop that loops over the user object and check if i is equal to the key/value, but that seems expensive and not very scalable.
Any ideas how I can force Django to view i as a variable and use it's value as an index, or any other way get around this?
Thanks
Jayd
*Edit:
I tried the extra for loop, as suggested by Abhi, below.
{% for i in count %}
  {% for key, current_user in users.items %}
      do some stuff with the value i using {{i}} which always returns the value, i.e. 1
      email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}
  {% endfor %}
{% endfor %}

This sort of works, but now it will repeat do some stuff with the value i for every value of users. and if I put in an if:
{% for i in count %}
  {% for key, current_user in users.items %}
    {% if i == key %}
      do some stuff with the value i using {{i}} which always returns the value, i.e. 1
      email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}
    {% endif%}
  {% endfor %}
{% endfor %}

That ignores the loops when a count doesn't have a particular user.
The only way I can see around this is the have the user loop at each place that I want to use current_user.
{% for i in count %}
      do some stuff with the value i using {{i}} which always returns the value, i.e. 1
      email:{% for key, current_user in users.items %}{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}{% endfor %}
{% endfor %}

And this seems very expensive to do.
Any ideas?
I was thinking of maybe writing a filter that returns the values for users using i as the key:
{% with current_user=users|getuser:i %}

But I don't know if this will work or I will get the same error, where i is being passed as the value 'i' instead of a variable name.
I will give it a try so long.
*Edit
This didn't work.
The  filter worked using {{}} returning the object, but it didn't work inside the {% %}
.
Thanks for the input
Comment: Why are you using a dictionary for the outer collection instead of a list?
Comment: Why did you use **bold** formatting throughout this question, instead of following the clear formatting prompts to the right of the edit box?
Comment: Apologies for the formatting, I didn't see the prompts when I made the post.
Answer: As Joe says in his comment, a dictionary is clearly the wrong data structure for the outer container. You should be using a list.
users = []
user = {'id':..., 'email': ...}
users.append(user)

...
{% for user in users %}
    {{ user.email }}
{% endfor %}

If you need the number of each user within the loop, you can use {{ forloop.counter }}.
Comment: I need to have the count start at 1, which is why I used a separate count list, and also some counts will exist that don't have users associated with them. So there might only be 2 users and I might need to loop and do the other things with the count number 6 or 8 times.
Comment: @Jayd Still not sure I fully understand the use case, but these are details that would have been good to put in the question upfront so we know what real problem you're trying to solve.
Answer: Try this:
{% for key,val in user.items %}
ID is {{ val.id }} and Email is {{ val.email }}
{% endfor %}

Comment: Note- You've used the dictionary "users" at the top and user[i] in an attempt to access index i. Ensure that the dictionary names match up when you try to access user.items
Comment: Thanks for this answer. Yes, this is the work around that I was talking about, having a for loop inside the other for loop. It has worked in the code, but is there any other way to do this that doesn't require loops in loops, but rather to call the users object by i?
Comment: Thanks for the comment on user and users. I went back and changed it.
Answer: I have come up with the following as a work around:
This is the filter code:
@register.filter(name='dict_value_or_null')
def dict_value_or_null(dict, key):
    if key in dict:
        return dict[key]
    else:
        return 'null'

Here is the template code
{% for i in count %}
      do some stuff with the value i using {{i}} which always returns the value, i.e. 1
      email:'{{users|dict_value_or_null:i|dict_value_or_null:'email'}}'
{% endfor %}

This works well, so I suppose that have my solution. But I still think this all could have been much easier if there was a way in the template system to force values inside {% %} to be treated as variables, instead of literals.
Is there a way to do this?
Thanks for the input
Comment: Probably not the answer you're looking for, but you could take a look at Jinja2 for your templating engine.  It plugs into Django fairly well, has a very similar syntax, but is designed a little better (IMHO) especially when it comes to being able to use Python-like constructs in your templates.
Answer: to make Jayd Code usable with either list or dict, try this
@register.filter(name='dict_value_or_null')
def dict_value_or_null(dict, key):
    try:
        return dict[key]
    except:
        return 'null'


Downtimeless MySQL backups on a budget

Question: My current MySQL backup scenario is to replicate our db to a second server and run mysqldump on that server to remove any downtime from table or row locking. This is working well but costs $150 per month for the second server (Australian hosting is a lot more expensive than US.)
I read a lot of questions on here about this, most people need help with the scheduled backups and whatnot which is not what I need. I need to mysqldump (preferably every 4hrs) with no downtime. The db is ~7GB uncompressed, so the mysqldump can take some time depending on the server.
I have considered replicating to the same machine, but I didnt want the slave to eat into much needed memory. I'm not sure I can constrain memory usage on a per db basis? Either way, this will put load on the server while its dumping the db.
I just read this http://www.zmanda.com/quick-mysql-backup.html and it looks good, $300 per year is ok, that saves me a lot.
Unfortunately I can't replicate to Amazon's RDS but I could replicate to a micro RC2 instance but the replication would take place over-net and the ping is ~220ms.
I saw a few people on here talking about LVM snapshots which might be a good option. I dont know a great deal about this option tho.
Opinions would be greatly appreciated.
Comment: What is the website? Give a description of what it does
Comment: You can buy servers for a lot cheaper than $150 a month. 7GB does not sound like that much data. You can buy [disposable 128MB servers](http://lowendbox.com) for as little as $1.50 a month and more impressive 1GB ones for about $20. Since there is no need for a query cache you can easily handle plenty of writes with a GB of RAM and a server with an SSD.
Comment: LVM snapshots will not give a consistent image unless you shutdown the server first. You can do hot snapshots - and try to rebuild the files - but it's risky.
Answer: If you use innodb tables, you can use
http://www.percona.com/docs/wiki/percona-xtrabackup:start
That will take a dump of your database that can be imported by their tools also without locking. I believe if you have myisam tables it locks those.
Comment: I do have some MyISAM tables but they are not used frequently, so locking those is ok. Thanks for the comment, will check that out.
Comment: Percona rocks btw!
Answer: If you are using innodb or another backend that's fully transactional, you can use mysqldump --single-transaction ....  I have used this on fairly large (~100GB) databases with good results; if the database is under heavy load it can take hours but it does work without locking your tables.  Replication is generally better but sometimes you want a nice solid dump file.  Keep in mind that you can dump a mysql replication slave as well.
From the mysqldump page (note the caveates about operations that will leak into the transaction):
 ·   --single-transaction

   This option sends a START TRANSACTION SQL statement to the server
   before dumping data. It is useful only with transactional tables
   such as InnoDB, because then it dumps the consistent state of the
   database at the time when BEGIN was issued without blocking any
   applications.

   When using this option, you should keep in mind that only InnoDB
   tables are dumped in a consistent state. For example, any MyISAM or
   MEMORY tables dumped while using this option may still change
   state.

   While a --single-transaction dump is in process, to ensure a valid
   dump file (correct table contents and binary log coordinates), no
   other connection should use the following statements: ALTER TABLE,
   CREATE TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE. A
   consistent read is not isolated from those statements, so use of
   them on a table to be dumped can cause the SELECT that is performed
   by mysqldump to retrieve the table contents to obtain incorrect
   contents or fail.

Comment: Joshua, I notice your typo of 'myself' and note that I find it *so* hard to type 'myself' because I just naturally type mysql. I currently do a mysqldump 4 hourly on the slave machine. single-transaction looks like a good option, thanks!
Comment: Doh.  Nice catch. :)
Comment: I do not think that mysqldump is a good option on such a large database. If it takes hours to dump, it can take weeks to restore. Test your restore time and the resources needed to complete it!
Comment: Thanks Baron, it does take a little while to restore - not weeks, but still a considerable time. I will see how long it takes when I get my new server. Maybe a copy of the files will work out to be much more effective.
Answer: I don't see much of a problem replicating over a high latency connection to a cheap VPS in the U.S.  The high latency should not really be that much of a problem.  Replication is designed to be able to catch up quickly even when a slave falls hours behind, ie it can operate asynchronously.
As long as you can stand that much outgoing bandwidth on your Australian hosting plan.
Here is a much more detailed response to whether the high latency would matter
Comment: I would have no idea how much bandwidth it would even use. Maybe I should monitor the traffic between the boxes I have now to see how much is used.
Comment: You may be "disappointed" with trying to run mysql on top of EBS.  I highly suggest you test the performance before trying to use it for replication.
Comment: Thanks for that, will definitely get a feel for it before I start relying on it - if this is the approach I take.
Answer: Realistically, only the time it takes to actually export the database will be downtime. Do it during a slow enough time period and there shouldn't be ANY problem. What is an IT department on that budget really expecting?
You should be able to mysqldump a 7GB database in 5-10 minutes MAX, take off the read/write lock and the downtime will be over. You can then find the most bandwidth effective way to the 7GB file to the new server (read: HIGH COMPRESSION). You have plenty of time to get the file transferred and imported into MySQL on the new server. Then, enter the masterlog information and start replication. Should be a piece of cake!
MySQL documentation is fantastic:
http://dev.mysql.com/doc/refman/5.0/en/replication.html
Comment: And I meant to add, replication does not use much bandwidth. It is no doubt a better call than mysqldump-ing every four hours!!!
Comment: Who mentioned IT department? This is just my website. :) And I am currently replicating for backups but not sure its the best approach at $150 p/m. As stated the option of an EC2 micro instance is there.
Comment: @Christian what's p/m? I don't know what it is, but 150$ for a single p per m seems expensive 8-|
Comment: @TehShrike, p/m = per month. Australian hosting is a lot more pricey than US hosting. Also, I was trying to keep the second server on the same network for speed and transfers not counted against bandwidth allowance.
Answer:
I'm not sure I can constrain memory usage on a per db basis

Of course you can - you just need run the slave with a different /etc/my.cnf
You can even do stuff to manipulate the scheduling priority / CPU affinity on the master and slave using nice/renice and taskset (assuming it's a Linux server).

but the replication would take place over-net and the ping is ~220ms

Latency is pretty much irrelevant - the important thing is bandwidth - and the database bandwidth (assuming you're not replicating session data) is several orders of magnitude less than the HTTP bandwidth.

I need to [create a consistent backup of the database] (preferably every 4hrs) with no downtime

But the strategies you discuss don't allow for recovery at anything like that time.
I think the cheapest option would be a slave on the same machine - and if it's adversely affecting performance beyond what you can reconfigure then upgrade the current hosting package.
You might also consider running a disconnected slave: enable bin logs on the current server. Get a backup, restore the backup on a local machine then copy down the bin logs as they are rotated and roll them forward on the local DBMS.
Comment: Nice response, thanks for that. The new server I am looking at getting would have enough memory to allow for a slave on the same machine, but I really like the idea of the binlogs being copied / rolled forward. Thanks again!
Answer: My suggestion:
1 - keep your second account/server and implement replication to a database in your original account/server.
2 - stop replication to the second account/server.
3 - monitor performance for a few days. Make sure you monitor it long enough to include your busiest periods.
4 - be ready to switch over to your old setup if there is a major performance problem. This is the reason why you kept the second account.
5 - buy more capacity/upgrade server in your original account. This should be cheaper than paying for two servers I believe.
6 - cancel second account.
Good luck!

Downtimeless MySQL backups on a budget


JSON parse error while parsing text

Question: Hi I am getting a json object as this.
{"messages":["operator  will assit you"]}

I am getting SyntaxError: JSON.parse: unexpected character
 I am using $.parseJSON(json);
I couldn't identify any syntax error in this.
Comment: Seems a problem of encoding. The ¿name? of the operator isn't show
Answer: Pasting your code snippet would be helpful. I would guess you aren't parsing the string you are parsing the already parsed out object. You need to do one of the following:
var json = {"messages":["operator  will assit you"]};

or
var json = '{"messages":["operator  will assit you"]}';
$.parseJSON(json);

Comment: Of course, if you already have the object, you wouldn't want or need to call `parseJSON`.
Answer: Chris Farmer was right. The object is already in parsed state.
i just used
object.messages
and it worked like charm
Comment: Be sure to mark correct answer and close question. You should add this information as an edit instead of an answer.

JSON parse error while parsing text


Show that $V$ is $T$-invariant.

Question: Let $T:W \to W$ be a linear operator vector space $W$ over $\mathbb{F}$. such that $w \in W$ where $$\{w, T(w) ,T^2(w)\}$$ is linearly independent and $T^3(w)= w +T(w)+T^2(w)$.
Show that $$V := \operatorname{span}{\{w ,T(w),T^2(w)\}}$$ is $T$-invariant.
My attempt
First w implies that $T^3(w)= w +T(w)+T^2(w)$ and is a element $W$
Then $$T^3(T(w)) = T(w) + T^2(w) + T^3(w) = T(w) + T^2(w) + [w +T(w)+T^2(w)]$$ and is a element W
I don't know what to do with $T^2(w)$ because then $$T^3(T^@(w))=T^2(w) + [w +T(w)+T^2(w)] + T^2(T^2(w))?$$
Comment: A general element $x$ of $V$ is of the form
$$x=aw+bT(w)+cT^2(w).$$ Can you show that $T(x)$ is of the same form (with some other constants in place of $a,b,c$)?
Answer: Let $v \in V$, $v$ can be expressed as $aw + bT(w) + cT^2(w)$. Now it follows:
$\begin{align*}
T(v) &= T(aw + bT(w) + cT^2(w)) \\
&= aT(w) + bT^2(w) + cT^3(w) \;\mbox{by linearity}\\
&= aT(w) + bT^2(w) + c\left[w + T(w) + T^2(w) \right] \; \mbox{by the hypothesis} \\
&= cw + (a+c)T(w) + (b+c)T^2(w) \in V
\end{align*}$
so $V$ is $T$-invariant.
Answer: Let $v \in V$. Show $T(v) \in V$:
$$T(v) = T(\alpha w + \beta T(w) + \gamma T^2(w) = \alpha T(w) + \beta T^2(w) + \gamma T^3(w)$$
by requirements we have
$$T(v) = \gamma w + (\alpha + \gamma) T(w) + (\beta + \gamma) T^2(w) \in V$$
$$\Rightarrow T(V) \subset V$$
q.e.d.

Show that $V$ is $T$-invariant.


Adding multiple people to a conference call from caller Twilio

Question: I read lots of articles about Twilio conference call. I created a php function which creates a Twilio conference which can add any one who have access to that link to the Conference with this link. so then I read this article about Dialing Multiple Numbers Simultaneously with Twilio.
This article shows how to dial multiple clients or numbers in the same time but the first one who accept the call will connect  while the others will be hung up on.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Number>877-555-1212</Number>
    <Number>877-999-1234</Number>
    <Number>877-123-4567</Number>
  </Dial>
</Response>

So now my question is that can I add all of them to a conference call troughs a twilio php function?
I also checked this question on stack overflow but the different is that I am using TwiML and then I thought maybe there is a function to add all clients to same room whe he/she calls a list of them.
 $dial->conference('My conference', array(
            'startConferenceOnEnter' => True,
            'endConferenceOnExit' => True
            ));

Answer: I opened a ticket in twilio, one of its developer said make your call trough REST api and add all of clients or numbers to to same conference
but in my case My android app is pointing to a twilML so I decided to Add the caller itself to a conference call and then make my REST call to that conference call.
so now it worked for my case.
here is my codes
......
//some php codes to configure the Twilio and get the from and to caller ids

//this part belongs to my caller. I added this php file url to my TwiML app
//so when my user hit the dial button it will sent the caller to this conference room and waits for others.
$response = new Twiml;
$dial = $response->dial();
$dial->conference('Room 123', array(
                'startConferenceOnEnter' => True,
                'endConferenceOnExit' => True
                ));
print $response;

//this is the part that make a call other participants and will  add them to the same conference room that caller is.
$call = $client->calls->create(
    "yourClient", "youtwiliophonenumber",
    array("url" => "http://domain/conference.xml")
);

And then I added this xml file to the url of REST call api
here is my XML file
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Conference startConferenceOnEnter="true" endConferenceOnExit="true">Room 123</Conference>
  </Dial>
</Response>


Solve $ax^t = by^t$ for $ t$

Question: I use to know how to do this but a friend of mine asked for help and I cannot remember.
Can anyone help me solve $ax^t = by^t$ for $t$?
Answer: $$ax^t = by^t$$
$$(x/y)^t=b/a$$
$$t\log(x/y)=\log(b/a)$$
$$t=\frac{\log(b/a)}{\log(x/y)}$$
Answer: HINT :
Rewrite it as : $$\frac{a}{b} = \Big(\frac{y}{x}\Big)^t$$
Do you know the $\ln$ function? You can use under certain constraints of your constants. Basically need fractions to be positive.
Answer: Assuming $x,y>0$ and either  $a,b>0$ or $a,b<0$, you can divide by $b$ to obtain
$$ \frac ab x^t= y^t$$
Now take logs
$$\ln\frac ab+t\ln x = t\ln y.$$
You should be able to solve this for $t$, namely
$$t=\frac{\ln\frac ab}{\ln y-\ln x}.$$
If $x,y>0$ and one of $a,b$ is zero or $a,b$ have different signs, there is no solution.
If $x,y>0$ and $a=b=0$ then all $t\in \mathbb R$ are solutions.
If one of $x,y$ is $\le 0$ you may have trouble defining the powers for all $t$ in the first place, but for certain combinations solutions still exist ...

Solve $ax^t = by^t$ for $ t$


Set different appSettings key in production server

Question: How can I set a different value when I publish my MVC5 do production server?
Example:
In dev I have
<appSettings>
    <add key="XLSFile" value="C:\\temp\\file.xls" />
</appSettings>

And when I publish the project I want to set a different path:
<appSettings>
    <add key="XLSFile" value="C:\\projectname\\file.xls" />
</appSettings>

Comment: You're going to want to use an XmlUpdate task in MSBuild.  What method are you using for deployment?
Comment: Hi, I use Visual Studio Publish tool.
Answer: You'll want to use a config file transformation. Essentially you will override the config file for your release build.
For example:
<add key="XLSFile" value="C:\\projectname\\file.xls" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

Comment: Hi, in your link they use xdt:Transform="SetAttributes" and you are using Replace, why?
Comment: Force of habit. In this case either should work. Replace will replace the whole key and its children. SetAttribute will set the attribute and may be the better option in this case
Answer: You can use two config files one with name web.Debug.config and second web.Release.config. First one for development and second for production server when published and now specify different value of key in both files as required


Forçar arquivo .EML abrir direto no cliente de e-mail

Question: O Thunderbird permite que uma 'imagem' do e-mail seja salva em um arquivo, esse arquivo vem no formato '.EML', e em meu sistema, os usuários carregam esse arquivo no sistema junto com o que eles estão fazendo relacionado a determinada empresa.
Mecanismos como Steam, Mega, e sites de torrents pedem para o usuário se eles desejam executar um programa específico ou um programa padrão para o tipo de arquivo.
Acredito que seja algo simples e com HTML mesmo, porém não consegui localizar como fazer isso.

Como abrir um ext .EML direto no client de e-mail ?

OBS: Se trata de um sistema intranet não há problema em ser invasivo.
Answer: Isto é apenas um adicional a resposta. Uma ferramenta bacana para isto seria o http://filext.com, se bem que tendo um sistema baseado em Unix ou baseado em Linux é possível fazer isto via linha de comando.
Supondo que você venha a desejar detectar outros tipos de mime-type, em Ubuntu existe o comando file, exemplo de uso:
file --mime-type arquivo.eml

Em PHP (já que foi a solução proposta em sua resposta) pode-se usar o fileinfo, algo como:
function mimeType($file)
{
    $mimetype = false;

    if (class_exists('finfo')) {//PHP5.4+
        $finfo     = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype  = finfo_file($finfo, $file);
        finfo_close($finfo);
    } else if (function_exists('mime_content_type')) {//php5.3 ou inferiror
        $mimetype = mime_content_type($file);
    }

    return $mimetype;
}

O uso seria algo como:
$mime = mimeType($file);

if (!$mime) {
    die('Formato desconhecido');
} else {
    $filename = urlencode(basename($file));

    header("Content-Disposition: attachment; filename=$filename");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Type: $mime");
}

Note que codifiquei o filename= deve conter somente o nome (não tenho certeza se passou o nome completo) seria interessante passar pelo basename($file) para obter somente o nome mesmo.
Também é necessário codificar, acaso tenha espaçamento entre os caracteres do nome do arquivo urlencode(basename($filename)).
Answer: Solução foi apenas com headers mesmo, ele não funcionava apenas porque apontava o programa padrão pelo 'content-type' estar setado errado, com 'message/rfc822' funcionou corretamente (força o download e sugere o programa, ja é o suficiente):
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: message/rfc822");
header("Content-Transfer-Encoding: binary");
readfile($file);


Consider a man who travelled exactly 2 km in two hours. Is there a one-hour interval when he traveled exactly 1 km?

Question: Consider a man who travelled exactly 2 km in two hours.
Is there a one-hour interval when he traveled exactly 1 km?
Can we make a mathematical argument?
I have written my attempt in an answer below. Does anyone else have a better approach?
Are my assumptions not necessary, or we can produce counter examples without them? Can we make-do with some intermediate assumptions?
(This is not the same as the Universal Chord question because there's no $f$ with $f(0)=f(1)$ and there's no continuity assumption. Answers to the cyclist question are not satisfactory.)
Comment: No continuity assumption? Can we suppose he was simply teleported to the destination at some point in time?
Comment: @Mazura: I believe the question is whether, for all travels that meet the criteria described in the question, there *must* be such an interval; your comment merely points out that there is *some* travel that has such an interval (and, as the answers show, we can actually say much even if the velocity was not constant).
Comment: Interestingly, you need that the total distance is an integer for this to be true. For example, cover 1.5km in 1.5 hours by covering 0.5km segments steadily in 20 minutes, then 50 minutes, then 20 minutes. Every 1km segment is traversed in exactly 70 minutes, although the average pace is 1km/hr. (Note every 1km segment contains the middle 0.5km plus a total of 0.5km from the first and last portions.) Discussed here: https://arxiv.org/abs/1507.00871
Comment: @whoisit, the distance function $d$ _does_ have to be continuous. (What I think you mean is that it is not necessarily differentiable.) So the Universal Chord Theorem applies here to the function $f(t)=d(t)-t$.
Comment: While the upvoted answer is certainly correct, the short answer already appears in the comments: this is *exactly* the Universal Chord Theorem, applied to $f(t)=d(2t)-2t$.  You have $f(0)=f(1)=1$ and, yes, $f$ is continuous.  You can conclude that there are time intervals of length exactly $1$ hour, $40$ minutes, $30$ minutes, $24$ minutes, $20$ minutes, etc., over which the runner's average velocity is $1$ km/hr.
Comment: @MarcvanLeeuwen I meant that about velocity
Comment: @EricDuminil right, "integer" should be "integer multiple of the subinterval length". If the sub-interval is one kilometer as in this question, then the total length should be an integer number of kilometers.
Comment: After applying a quantum distribution on a closed 2km box with a base energy level that has its smallest factor being 3; I find no 1km distance exists; therefore I have found a counterexample.
Comment: Consider a man who traveled exactly *1 km an hour for two hours*. Is there a one-hour interval when he traveled exactly 1 km? Um, yeah: any interval that's an hour long. Don't need math unless it wasn't a constant velocity, and if it wasn't and we don't have what it was, then we can't say much.
Comment: @usul: Thanks for the link and interesting article. "Interestingly, you need that the total distance is an integer for this to be true." something's missing in your description, though. 2km is ~1.24 miles, so is it "an integer"? The description in the article is "there must be a subset covered in the average speed if and only
if the length of the entire race is an integer multiple of the length of the
subinterval of interest".
Answer: Let $d(t)$ be the distance travelled at time $t$ (in hours). We have $d(0) = 0$ and $d(2) = 2$. It is safe to assume that $d$ is continuous.
We want to find a $t\in[0;1]$ with
$$d(t+1) - d(t) = 1.$$
Define an auxiliary function
$$f(t) = d(t+1) - d(t) - 1.$$
Thus, our condition becomes $f(t) = 0$.
We have that
$$\begin{align*}
f(0) &= d(1) - d(0) - 1 = d(1) - 1 \\
f(1) &= d(2) - d(1) - 1 = 1 - d(1) = -f(0)
\end{align*}$$ Thus:
If $f(0) = f(1) = 0$, then we are done.
If $f(0) \neq 0$, we have that $f(0)$ and $f(1)$ have different signs. By the intermediate value theorem, there is a $t\in (0;1)$ with $f(t) = 0$, as desired.
Answer: The velocity $v(t)$ is either continuous or has discontinuities at a finite number of points.
Thus, let's assume $v(t)$ is an integrable function and hence its integration is continuous.
For $t \in [0, 1]$, we define the distance traveled in the next hour: $$ d(t) = \displaystyle\int_{t}^{t+1hr} v(t) dt  $$
Easier way to define $d$ without any velocity assumption: Let $p(t)$ be the position of the man - and define $d(t)=p(t+1)-p(t)$. As teleportation is not allowed, $p$ is continuous, thus so is $d$.
We want to find whether there is always a $t$ where $d(t)$ takes the value 1 km.
We know that $d(0)+d(1)=2$.
WLOG assume $m = d(0) \leq d(1) = n $.
Then, either $m=n=1$ in which case we're done, or we have
$m<1<n$, in which case we can use continuity of $d(t)$ to invoke Intermediate Value Theorem to claim that there was some $t$ in between where the value was $1$.
Comment: I find this answer much more intuitive than Tobius'. However, the bit about integrals is superfluous. You can define $d(t)$ as "distance traveled between $t$ and $t+1$" and everything in your answer remains correct, and this correctly proves the existence of $t$ such that $d(t) = 1$. You don't need to know that $d(t) = \int_{t}^{t+1}v(t) dt$ and so you don't need any assumptions about integrability or continuity of the velocity (or even *existence* of a velocity function at all). All you need to know is that $d(t)$ is continuous, which is equivalent to "the traveler did not teleport".
Comment: You don't need any assumptions about the velocity, as Tobius' answer shows.
Comment: "or has discontinuities at a finite number of points" - and the question has no meaningful answer? - At 2000mph that takes 4 seconds. So between 0:0:02 and 1:0:02 they went one mile. That's *assuming* they didn't just stand there between 0 seconds and 1:59:56 hours.
Answer: With the continuity assumption, define a function on the interval $t \in [1,2]$ by $d_1 (t) \equiv d(t) - d(t-1)$, the distance covered within the last hour, where $d(t)$ is the position at time $t$. Then $d_1 (1) = d(1) - d(0) = d(1)$, and $d_1 (2) = d(2) - d(1) = 2 - d(1)$. The mean of the two boundary values $d(1)$ and $2 - d(1)$ of the function $d_1 (t)$ is $1$, therefore $1 \in [d_1 (1), d_1 (2) ] $ no matter what $d(1)$ is, and by the intermediate value theorem there exists a $t_0 \in [1,2]$ such that $d_1(t_0 ) = 1$.


optimize nginx for large file downloading

Question: Hey, I'm wondering what are some general options I should look into for optimizing an nginx server for large file downloading (typically 100mb to 6gb). I just migrated from lighttpd and I'm noticing that during downloads, speeds fluctuate a lot very quickly. I'm familiar with fluctuating speeds, but not at this rate, lighttpd didn't fluctuate nearly as much. I was wondering if there were some general things I should look into, being new to nginx. Should I up the worker pool count, etc.
I was going through the wiki page for the HttpCoreModule and I found something such as the directio option:

The directive enables use of flags O_DIRECT (FreeBSD, Linux), F_NOCACHE (Mac OS X) or directio() function (Solaris) for reading files with size greater than specified. This directive disables use of sendfile for this request. This directive may be useful for big files

would that be an option to try out? Thanks guys, I appreciate the help.
I know my question may be pretty broad, but like I said, being new to nginx I'm wondering what kind of options I can look towards to optimize the server for file downloading. I know a variety of things play a part, but I also know lighttpd didn't fluctuate as much on the exact same server.
Thanks!
Answer: How much ram do you have?
What kind of workload in your server? Does is serve only big files, or serving smaller files and/or proxying requests as well?
DirectIO is usefull when set of active files is larger then RAM, so they won't fit in cache, and caching them is useless - it's better to read them directly from disk and leave cache for something else.
As for flucations - this is probably caused by nginx workers locking on disk operations (by default they are synchronous). Try increasing number of workers or try using async i/o (aio on). But be carefull: too asyncronous io, or large number of workers might cause much bigger seek ratio, so overall speed might decrease dramatically.
Comment: Thanks! I have 4 gigs of ram. there's a light interface to it which is a rails app, but mainly it serves as a data repository. workload isn't too heavy, aside from transferring fairly large files, it's not overly active or anything, used by about ten people, at least one of which downloads at least one (usually two) such files daily.

so I'm not necessarily looking to handle a ton of simultaneous downloads to a huge file, I'd just like to optimize the transfer of a large file. perhaps you can give additional advice with this information?
Comment: Ok, then you might try to set number of workers to max number of simultaneous downloads (+1 for dynamic), so each download might be handled by separate worker. This should help to avoid blocking workers on disk i/o. Also, use directio for files larger then 500m to avoid useless disk cache poisoning.
Comment: so directio 500m; thanks man I'll try this.


Problemas com o getSupportActionBar() para botão (seta) Home e seta back ←

Question: Estou com problemas para dar a ação para o botão de home, estou conseguindo implementar sem problemas o botão de acordo com o código ↓
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

Mas não sei como definir para qual activity ele deveria ir e como fazer ele "matar" a activity em que está.
O descrito até aqui é o indicado na imagem abaixo como 1

Gostaria de ajuda também com o descrito como 2, ele funciona para voltar na tela (activity) anterior, mas eu gostaria de defini-lo para voltar na tela principal, mas ainda não encontrei algum método para tratar este botão.
A ideia é que, o botão 1 volte para a activity anterior e o botão 2 volte para a activity principal "Home" e que sempre que volte para qualquer lugar a pagina fechada seja "matada", assim não consumindo processamento com o que não estaria sendo usado.
Answer: Para redefinir o que acontece quando você clica no botão Back da Navigation Bar (2), você deve reescrever o seguinte método na sua activity:
@Override
public void onBackPressed(){

    Intent mIntent = //Crie a intent para chamar a activity principal

    startActivity(mIntent);

    finish(); // Finaliza a Activity atual

    return;
}

Para definir o que acontece quando você clica no botão Back da Action Bar (1) você deve escrever o seguinte código na sua activity
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

     switch (item.getItemId()) {

         case android.R.id.home:

             Intent mIntent = //Crie a intent para chamar a activity anterior

             startActivity(mIntent);

             finish(); // Finaliza a Activity atual

         break;

         default:break;
    }

    return true;
}

No caso de voltar para a activity anterior, em alguns casos você pode simplesmente chamar o método finish(), pois assim a activity atual será finalizada e a activity anterior a ela será chamada.
Comment: Como eu poderia definir uma ID para o botão superior (**1**)?
Considerando que eu obtenho ele chamando a função:
 `getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);`
E não tem nenhuma ID (pelo menos não visível)... Obrigado :)
Comment: @GuilhermeHS, Procure pelo método `onCreateOptionsMenu()` na sua activity que exibe a toolbar e veja qual é o arquivo de layout que é utilizado para definir o menu. Em seguida, acesse esse arquivo e veja qual é o id do botão de back
Comment: Não consegui encontrar este método `onCreateOptionsMenu()` tem algum lugar especifico que ela fica?
Comment: Ele fica na Activity que contém a toolbar. Se estiver meio complicado de achar, abra o arquivo da activity e aperte **Alt+7** para abrir o painel de *estrutra da classe* e em seguida clique no ícone do **az** na parte superior desse painel. Agora é só procurar pelo método `onCreateOptionsMenu()`
Comment: Então, esse que é o problema, não tem na minha _activity_ este método, [olha no print](http://imgur.com/aPJobJR) como tá minha _structure_, o que tá selecionado com o mouse é o que "invoca" o botão **1**
Comment: @GuilhermeHS, já entendi o que você fez, vc não está usando uma Toolbar, mas sim a Action Bar padrão do Android. Reeditei a resposta. basta trocar o **id.do.botao.back.da.toolbar** por **android.R.id.home**
Comment: Essa eu juro que é a ultima pergunta :/ ..... Quando eu uso o `finish();` ele não "mata" a _activity_ que eu estou, ele apenas vai para a outra e ela permanece ativa, tanto que, quando chega na tela principal se eu apertar o botão para voltar (**2**) ele vai começar a fechar de fato as telas que antes ele só trocou por outra, ou seja, ele apenas está trocando de tela, o `finish()` não está tendo o efeito de fechar propriamente dito, ele apenas está substituindo, se é que você me entende :/ (já tentei usar o `ActivityAtual.this.finish();` mas dá erro.
Answer: De todas as formas acredito que esta seja a mais simples.
No metodo onCreate da Activity insira o seguinte codigo:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Agora vem o segredo, no arquivo AndroidManifest defina o pai da sua activity, ou seja para onde você quer que sua activity va quando precionado "Action Bar (1)".
Basta add a seguinte linha: android:parentActivityName=".HomeActivity"
Ex:
<activity
        android:name=".ProdutosActivity"
        android:label="@string/title_activity_produtos"
        android:parentActivityName=".RankingActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />

Answer: Cara seguinte, jeito fácil, cria um xml e nele você cria um toolbar(ao invés de Linear ou Relative Layout) mas vai precisar ser assim(android.support.v7.widget.Toolbar), pode ser bem simples:
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/cardview_light_background"
    android:elevation="4dp">

Depois de criar você insere ele no xml que você quer que ele apareça, aquela onde deve aparecer o botão voltar:
<include
    android:id="@+id/app_bar"
    layout="@layout/menu_back_button"
    />

Perceba que dei um id ao include, agora vá na activity onde o menu deve aparecer e insira o seguinte código, logo abaixo de onCreate:
 Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

Feito isso a barra deve funcionar.


How read in template my json

Question: I would like read in template angular json ,
this is my data:

  //public test:any; in class

  let liste = [
      {id:0,plage:'8H-9H',isReserver:false},
      {id:1,plage:'9H-10H',isReserver:false},
      {id:3,plage:'9H-10H',isReserver:true},
    ];

    this.test = '{"nomJour":' + 'test' + ', "occupe" : "'+ liste+'" }';
json : {{test}}

nom: {{test.nomJour}}

isBusy:

    {{plage.id}}

the name and isBusy is empty ...
How I must correctly build my json ?
thank's
Answer: You don't need to manually create a json representation by concatenating strings.
Create your object and then use JSON.stringify to make it a valid json.
let liste = [
   {id:0,plage:'8H-9H',isReserver:false},
   {id:1,plage:'9H-10H',isReserver:false},
   {id:3,plage:'9H-10H',isReserver:true},
];

this.test = JSON.stringify({nomJour: 'test', occupe: liste});

Comment: thank's for reply, i try the snippet with stringify, but the isBusy is no change is empty.
Comment: In your object you haven't `isBusy` property. If it is the `occupe` be sure that it has data
Answer: test is an object tranform to string . You cannot access {{test.nomJour}} from a string. make it object
this.test = {"nomJour": 'test', "occupe" : liste };

Demo

How read in template my json


How do I share configuration between custom admin form and node type edit form?

Question: I have a module that requires some settings per content type. The module is to also provide one admin page where all the settings can be applied across content types at once.
Essentially I'm trying to share configuration information between a custom Admin Settings form and the node type edit form. I had this all working with Drupal 8 Beta 10 or something like that, but I'm trying to get it working with the latest stable release (8.0.4).
I originally used the method described here for my custom Admin form using the following code:
<code>$config = $this->config('mymodule.settings');
'#default_value' => $config->get('my_setting'),
</code>
Similarly, for the node edit form alter function, I used:
<code>$config =  \Drupal::config('mymodule.settings');
'#default_value' => $config->get('my_setting'),
</code>
But the above stopped working for the node type settings form at some point, so I started looking at using Third Party Settings:
<code>$node_type = $form_state->getFormObject()->getEntity();
'#default_value' => $node_type->getThirdPartySetting('mymodule', 'setting', 'default_value'),
</code>
This works great for the node type edit form, but I don't know of a way to pull the third-party settings into my custom Admin form partially because getEntity() method isn't available.
So my question is, what is the best practice for sharing configuration settings between a custom admin form and the content type edit form?
Answer: So the bottom line appears to be that Third Party Settings is a great way to handle content type settings without really caring which content type you're dealing with, but there doesn't seem to be a way to get this working with a custom admin settings form for the reason described in the question.
If I didn't want to have a separate configuration form that handled the content type settings for all content types on one form, I would definitely have used Third Party Settings. However, since I had this extra requirement, I wound up fixing whatever broke along the journey from D8 beta to stable release.
Now the node type gets included with each of the desired settings using the following code:
<code>function mymodule_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
  $node_type = $type->get('type');
  $config = \Drupal::getContainer()->get('config.factory')->getEditable('mymodule.settings');
  $config->set('my_setting_'.$node_type, $form_state->getValue('my_setting'));
  $config->save();
}
</code>