Encerrar timer com um alert

Question: Eu tenho esse timer de 10 segundos em JS.

function startTimer(duration, display) {

  var timer = duration, hours, minutes, seconds;

  setInterval(function(){
    seconds = parseInt(timer % 10, 10);
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = seconds;

    if (--timer < 0) {
      timer = duration;
    }

  }, 1000);
}

window.onload = function() {
  var duration = 3600 * 2; // Conversao para horas
  var display = document.querySelector("#timer"); // Exibir o timer

  startTimer(duration, display);
}

Que chama este campo para começar a contagem.

Eu queria que, quando ele zerar exibir um alerta informando que o tempo acabou!
Answer: O jeito mais simples é adicionar uma verificação para saber quando o tempo acabou, assim:
setInterval(function(){
  // ...
  if (seconds == 0) {
    alert('tempo acabou!');
  }
}, 1000);

Encerrar timer com um alert


Suppose $X \mid Y$ and $Y$ are normally distributed. Does it follow that $Y \mid X$ is normally distributed?

Question: Suppose $X \mid Y$ and $Y$ are normally distributed. Does it follow that $Y \mid X$ is normally distributed?
And if so, how would one prove this?
Comment: Consider what the two known facts imply (if anything) about joint normality
Answer: Here, as a counterexample, is a sample of points where $Y$ has a standard Normal distribution and the conditional distribution of $X$ is always Normal, too.

This is obviously non-Normal.
To guarantee Normality, you need (almost surely) that
(1) $E[X\mid Y]$ must be a linear function of $Y$ and
(2) $\operatorname{Var}(X\mid Y)$ must be constant.
These are both characteristics of any Bivariate Normal distribution, so they are necessary conditions.  When you write down the joint distribution implied by both these conditions, it will be Gaussian: that is, Bivariate Normal.
These R commands generated the example.  The conditional variance $Y^4$ is not constant.
n <- 1e3
y <- rnorm(n)
x <- rnorm(n, y, y^2)
plot(x,y, col = "#00000040") # Semi-transparent points

Comment: These necessary conditions are described in most textbooks that cover ordinary least squares regression.  It's explicit (and given very clearly, without formulas!) in any edition of Freedman, Pisani, & Purves, *Statistics.*  That they suffice is demonstrated as I describe: simply write down the density.
Comment: Thank you for this wonderfully thorough response. But can you elaborate on (or provide a reference to) the conditions (1) and (2) you mention, which if I understand you correctly, together imply that $(X,Y)$ is bivariate normal?

Apologies if this is a trivial question; I'm new to this level of math and not very good.
Answer: whuber shows, by means of a counterexample, that the product of a Gaussian $X|Y$ times a Gaussian r.v. $Y$ does not necessarily lead to a joint Gaussian distribution, which in this case certainly doesn't have a conditional Gaussian.
Here is a (numerical) counter-example for your claim. Let $Y|X=x \sim N(x^3, 1)$ and $X \sim N(0,1)$. Then $(X,Y)$ is not jointly normal. The marginal of $Y$ is
$$
f_Y(y) = \frac{1}{2\pi}\int_{-\infty}^{\infty} e^{-\frac{1}{2}(y - x^3)^2 - \frac{1}{2}x^2}dx.
$$
Unfortunately this integral (as far as I know) cannot be computed analytically, but we can provide a fairly good approximation via adaptive quadratures. Thus the conditional we require is
$$
f_{X|Y} = \frac{e^{-\frac{1}{2}(y - x^3)^2 - \frac{1}{2}x^2}}{\int_{-\infty}^{\infty} e^{-\frac{1}{2}(y - x^3)^2 - \frac{1}{2}x^2}dx}.
$$
The result is disproved provided we are able to show that this conditional is not Gaussian. Toward this aim, let's fix $y = -1$. An elegant proof may be given by studying the properties of this function but here  I am taking a brute-force approach in which I approximate this beast numerically. If the conditional is gaussian we expect it to be unimodal and symmetric. This is the contradiction I'll be looking for.
 # propto the joint distribution
f_yx <- function(y, x) {
  exp(-0.5*(y-x^3)^2 - 0.5*x^2)
}

# the marginal of Y
f_y <- function(y) {
  integrate(function(t) f_yx(y, t), lower = -Inf, Inf)$value
}

# the conditional of x given y
x_given_y = function(x, y) f_yx(y,x)/f_y(y)

# fix y
y = -1

x <- seq(-3, 3, len=100)
cond_y <- sapply(x, x_given_y, y=y)
plot(x, cond_y, type ="l", lwd=2)

If we can trust numerical integration (I'm using the integrate function here which is extremely robust!), we can see from the plot that this conditional density is definitely non-Gaussian. This contradicts the claim.
Side comment. There exist non-Gaussian bivariate distributions which have Gaussian conditional densities. One example of this is
$$
f(x,y) = C\exp\left(-(1+x^2)(1+y^2)\right),\quad -\infty <x,y<\infty,$$
where $C$ is the normalising constant. You can check that both $Y|X=x$ and $X|Y=x$ are Gaussian with suitable parameters.
Comment: Thanks, your updated example seems working. However, as my commented before, how can $X|Y$ non-Gaussian if you stated $(X, Y)$ is jointly Gaussian? (this may be just another typo...)
Comment: Maple gives me  `int( exp( -(1/2)*(y-x^3)^2 - (1/2)*x^2 ), y=-infinity..infinity ) assuming x,real;
                      /  1  2\  (1/2)   (1/2)
                   exp|- - x | 2      Pi
                      \  2   /

`  por withn latex ${\mathrm e}^{-\frac{x^{2}}{2}} \sqrt{2}\, \sqrt{\pi}$
Comment: What do you mean by "*this joint Gaussian certainly doesn't have a conditional Gaussian*" -- it is well known that if there is joint Gaussian, then all conditionals are Gaussian.  By the way, the example you gave seems cannot negate OP's conjecture -- as he is questioning the conditional normality, not the joint normality?
Comment: Yep, Corrected +1.
Comment: @kjetilbhalvorsen but we need the marginal of y here. thus int should be for 'x=-inf..inf' right? Many maple can do this as well? I checked with Wolfram online without success : 'computing time exceeded std. time'
Comment: @utobi: Maple cannot do that, ...
Comment: Thanks for the feedback @kjetilbhalvorsen.
Answer: Here I will augment the excellent answer by whuber by showing the mathematical form of your general model and the sufficient conditions that imply a normal distribution for $Y|X$.  Consider the general hierarchical model form:
$$\begin{align}
X|Y=y
&\sim \text{N}(\mu(y),\sigma^2(y)), \\[6pt]
Y
&\sim \text{N}(\mu_*,\sigma^2_*). \\[6pt]
\end{align}$$
This model gives the joint density kernel:
$$\begin{align}
f_{X,Y}(x,y)
&= f_{X|Y}(x|y) f_{Y}(y) \\[12pt]
&\propto \frac{1}{\sigma(y)} \cdot \exp \Bigg( -\frac{1}{2} \Big( \frac{x-\mu(y)}{\sigma(y)} \Big)^2 \Bigg) \exp \Bigg( -\frac{1}{2} \Big( \frac{y-\mu_*}{\sigma_*} \Big)^2 \Bigg) \\[6pt]
&= \frac{1}{\sigma(y)} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \Big( \frac{x-\mu(y)}{\sigma(y)} \Big)^2 + \Big( \frac{y-\mu_*}{\sigma_*} \Big)^2 \Bigg] \Bigg) \\[6pt]
&= \frac{1}{\sigma(y)} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{(x-\mu(y))^2 \sigma_*^2 + (y-\mu_*)^2 \sigma(y)^2}{\sigma(y)^2 \sigma_*^2} \Bigg] \Bigg) \\[6pt]
&\overset{y}{\propto} \frac{1}{\sigma(y)} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{(\mu(y)^2 - 2x \mu(y)) \sigma_*^2 + (y^2-2y\mu_* + \mu_*^2) \sigma(y)^2}{\sigma(y)^2 \sigma_*^2} \Bigg] \Bigg), \\[6pt]
\end{align}$$
which gives the conditional density kernel:
$$\begin{align}
f_{Y|X}(y|x)
&\overset{y}{\propto} \frac{1}{\sigma(y)} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{(\mu(y)^2 - 2x \mu(y)) \sigma_*^2 + (y^2-2y\mu_* + \mu_*^2) \sigma(y)^2}{\sigma(y)^2 \sigma_*^2} \Bigg] \Bigg). \\[6pt]
\end{align}$$
In general, this is not the form of a normal density.  However, suppose we impose the following conditions on the condtional mean and variance of $X|Y$:
$$\mu(y) = a + by
\quad \quad \quad \quad \quad
\sigma^2(y) = \sigma^2.$$
These conditions mean that we require $\mu(y) \equiv \mathbb{E}(X|Y=y)$ to be an affine function of $y$ and we require $\sigma^2(y) \equiv \mathbb{V}(X|Y=y)$ to be a fixed value.  Incorporating these conditions gives:
$$\begin{align}
f_{Y|X}(y|x)
&\overset{y}{\propto} \frac{1}{\sigma(y)} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{(\mu(y)^2 - 2x \mu(y)) \sigma_*^2 + (y^2-2y\mu_* + \mu_*^2) \sigma(y)^2}{\sigma(y)^2 \sigma_*^2} \Bigg] \Bigg) \\[6pt]
&= \frac{1}{\sigma} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{((a + by)^2 - 2x (a + by)) \sigma_*^2 + (y^2-2y\mu_* + \mu_*^2) \sigma^2}{\sigma^2 \sigma_*^2} \Bigg] \Bigg) \\[6pt]
&= \frac{1}{\sigma} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{(b^2 y^2 + 2ab y + a^2 b^2 - 2xa - 2xb y) \sigma_*^2 + (y^2-2y\mu_* + \mu_*^2) \sigma^2}{\sigma^2 \sigma_*^2} \Bigg] \Bigg) \\[6pt]
&\overset{y}{\propto} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{(\sigma^2 + b^2 \sigma_*^2 ) y^2 + 2(b(a - x) \sigma_*^2 - \mu_* \sigma^2) y}{\sigma^2 \sigma_*^2} \Bigg] \Bigg) \\[6pt]
&\overset{y}{\propto} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{y^2 + 2[(b(a - x) \sigma_*^2 - \mu_* \sigma^2)/(\sigma^2 + b^2 \sigma_*^2) ] y}{\sigma^2 \sigma_*^2/(\sigma^2 + b^2 \sigma_*^2 ) } \Bigg] \Bigg) \\[6pt]
&\overset{y}{\propto} \cdot \exp \Bigg( -\frac{1}{2} \Bigg[ \frac{1}{\sigma^2 \sigma_*^2/(\sigma^2 + b^2 \sigma_*^2 )} \cdot \Big( y - \frac{b(a - x) \sigma_*^2 - \mu_* \sigma^2}{\sigma^2 + b^2 \sigma_*^2} \Big)^2 \Bigg] \Bigg) \\[6pt]
&\overset{y}{\propto} \text{N} \Bigg( y \Bigg| \frac{b(a - x) \sigma_*^2 - \mu_* \sigma^2}{\sigma^2 + b^2 \sigma_*^2}, \frac{\sigma^2 \sigma_*^2}{\sigma^2 + b^2 \sigma_*^2} \Bigg). \\[6pt]
\end{align}$$
Here we see that we have a normal distribution for $Y|X$ which confirms that the above conditions on the conditional mean and variance of $X|Y$ are sufficient to give this property.
Comment: @whuber: You're right that that would suffice to show normality, and perhaps that would be a better approach.  In this answer I've decided to go that step further to complete the square to get the relevant conditional moments of the distribution, so that I've got a full specification of the distribution of interest (not just knowing it's normal).  I agree that that goes beyond the question, but I just thought it might be usefu.
Comment: That result is appreciated.  I can't help thinking that stating this slightly more abstractly and generally would reveal the basic idea better and eliminate about two-thirds of the work. Specifically, for a sequence of triples $(\sigma_i,\mu_i,\alpha_i)$ (with $\sigma_i\gt 0$) it is elementary (and immediate by inspection) that $$\sum_i \frac{1}{2}\left(\frac{x-\mu_i}{\sigma_i}\right)^2+\alpha_i=\frac{1}{2}\left(\frac{x-M}{\Sigma}\right)^2+A$$ where $$\frac{1}{\Sigma^2}=\sum_i\frac{1}{\sigma_i^2}$$ and $$M=\sum_I\frac{\mu_i}{\sigma_i^2}.$$
Comment: BTW, didn't you already post exactly this result at https://stats.stackexchange.com/a/372075/919, using very nearly the same notation?  You could just quote it.
Comment: @whuber: I forgot about that one!  Déjà vu.
Comment: I know the problem ;-).
Comment: +1. But I can't help thinking there's a surfeit of algebra here.  Wouldn't it suffice to say that once $\sigma(y)$ is constant and because the argument of $\exp$ is (obviously) a quadratic function of $y,$ that your demonstration is complete?  In fact, if we were to impose "the following conditions" at the outset, the first two lines of this answer would suffice, exposing the conclusion as the result of the laws of exponentiation and polynomial addition.
Answer: Here is another counterexample which gives closed-form distributions of $X, Y, X|Y = y$ and $Y|X = x$.
Let $Y, Z \text{ i.i.d. } \sim N(0, 1)$, and define $X = \frac{Z}{Y}$.  Then for $y \neq 0$ (the probability of $Y = 0$ is zero),
\begin{align}
X | Y = y \sim N(0, y^{-2}).
\end{align}
On the other hand, it is well known that the marginal distribution of $X$ is Cauchy distribution, i.e.,
\begin{align}
f_X(x) = \frac{1}{\pi(1 + x^2)}, \quad x \in \mathbb{R}. \tag{1}
\end{align}
And the joint distribution of $X$ and $Y$ can be evaluated as (where $\Phi$ and $\varphi$ denote CDF and PDF of the standard normal distribution respectively):
\begin{align}
 & F(x, y) = P[X \leq x, Y \leq y] \\
=& P[Z \leq Yx, Y \leq y, Y > 0] + P[Z \geq Yx, Y \leq y, Y < 0] \\
=& \begin{cases}
\int_{-\infty}^y(1 - \Phi(tx))\varphi(t)dt & y < 0, \\[1em]
\int_0^y\Phi(tx)\varphi(t)dt & y > 0.
\end{cases}
\end{align}
Therefore, the joint density of $(X, Y)$ is given by
\begin{align}
 & f(x, y) = \frac{\partial^2F(x, y)}{\partial x\partial y} \\
=&\begin{cases}
-y\varphi(y)\varphi(yx) & y < 0, \\[1em]
y\varphi(y)\varphi(yx) & y > 0
\end{cases} \\
=& \frac{1}{2\pi}|y|e^{-(1 + x^2)y^2/2}.
\tag{2}
\end{align}
$(1)$ and $(2)$ together yield the conditional density of $Y$ given $X = x$:
\begin{align}
f_{Y|X}(y|X = x) = \frac{f(x, y)}{f_X(x)} =
\frac{1}{2}|y|(1 + x^2)e^{-(1 + x^2)y^2/2}. \tag{3}
\end{align}
Obviously, $(3)$ is not the density of any normal distribution (with $y$ as the variate).  Thus $Y | X = x$ is not normal.  For example, when $x = 0$, $(3)$ looks like as follows:

P.S., PDF $(3)$ may be termed as "double generalized gamma distribution", based on these two articles: Generalized gamma distribution and Double Gamma Distribution. The parameters linked to the generalized gamma distribution are $a = \sqrt{2(1 + x^2)^{-1}}$ (scale) and $d = 2, p = 2$.
Comment: (+1) The inverse of the r.v. with p.d.f. (3) is a particular case of the [inverse moment prior](https://web.stat.tamu.edu/~vjohnson/files/JRSSB.72.2.2010.143-170.pdf), p. 149


Bounded Distribution with specific limits regarding Variance

Question: Im currently looking for a probabilty density function that posesses the following properties
Should have range (0,1)
$$
\lim_{\sigma \rightarrow 0} f(x) = \delta(1)
$$
$$
\lim_{\sigma \rightarrow \infty} f(x) = \delta(0) + \delta(1)
$$
So far the closest i have gotten is the Beta Distribution which fullfils the second point when $\alpha = \beta$ and they both approach 0.
Answer: Try a $B(\frac{1}{r},\frac{1}{r+1})$. As $r\to 0$, the first parameter tends to infinity, the second one to $1$, which will approach a point mass at $1$. And as $r\to\infty$, both parameters tend to zero (almost equally quickly), giving you a mixture of two point masses.

R code:
<code>xx <- seq(0,1,by=0.0001)
rr <- c(0.01,0.1,1,10,100)
plot(c(0,1),c(0,10),type="n",las=1,xlab="",ylab="")
for ( ii in seq_along(rr) ) lines(xx,dbeta(xx,1/rr[ii],1/(rr[ii]+1)),col=ii,lwd=2)
legend("top",lwd=2,col=seq_along(rr),legend=paste("r =",rr))
</code>
(I prefer using a parameter $r$ rather than $\sigma$, which connotes a standard deviation.)
Comment: Thank you this looks really good. I will just leave the question open because I'm interested in what other ideas people come up with.


Why does bash shortcut not accept my ansible-playbook --limit wildcard *?

Question: I am trying to run an ansible-playbook via a bash shortcut, but getting an error.  When I run this command from the directory in long-hand (below), the playbook and my limits with wildcard * work fine; the playbook runs on all of the hosts starting with the BBCC names, i.e. BBCC01,BBCC02, etc.
[WORKS]
sparkles@Yo:~/myrepos/pre-post-role$ ansible-playbook run_pre_check.yml --limit 'BBCC*'

I have this bash script in my .bashrc file...
#precheck shortcut
    #to run, type "pre <whatever your limits will be>"
pre (){
    cd ~/myrepos/pre-post-role
    ansible-playbook run_pre_check.yml --limit $1
    cd
}

Normally if I do this command with specific limits, it works just fine.  Below are working examples:
[WORKS]
pre BBCC01
or
pre BBCC01:BBCC02:BBCC03
However, using the * wildcard as defined by ansible gives me an error.
[DOES NOT WORK]
pre BBCC*
Error from pre BBCC* is below:
ansible-playbook: error: unrecognized arguments: BBCC01.zip BBCC_.zip
Why doesn't this work?  Why is it looking for that zip file?  Is it something in my bash shortcut that I did wrong?  I read that it has to do with bash interpolation, but I'm not smart enough about bash to know what's happening.  Please educate me.  Thank you in advance.
Answer: You need to quote the argument to prevent the shell from expanding the wildcard, just like you do when you run the command by hand. And you have to quote $1 in the function to prevent it from being expanded there as well.
pre (){
    cd ~/myrepos/pre-post-role
    ansible-playbook run_pre_check.yml --limit "$1"
    cd
}

pre 'BBCC*'

Comment: That won't work if there are any `BBCC*` files in your current directory. The shell will expand the wildcard before calling the function.
Comment: It was the missing quotes in the bash!  Thank you for your help!  That fixed it!  Also, with the newly added quotes, I actually don't need the quotes in the command.  Just plain

pre BBCC*

works great.  Thank you again!!! =D


Carregar contatos do aparelho Android - ContactsContract.Contacts - ContentProvider

Question: Estou tentando carregar os seguintes dados da agenda do usuário: nome, telefone, e-mail e foto.
Contudo o processo e muito lento, está demorando em média 4 minutos para carregar informações de 800 contatos.
Procurei, mas sem sucesso, alguma biblioteca que faça isso.
Existe alguma outra forma para fazer este carregamento?
O código que estou usando:
<code>private void carregaContatos() {
        try {
            cr = activity.getContentResolver();
            contatos = new ArrayList<ContatosUsuario>();

            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);

            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        JSONObject contatosJson = new JSONObject();
                        ContatosUsuario contato = new ContatosUsuario();
                        // pega id do contato dentro do cursor
                        String idContato = cursor.getString(cursor
                                .getColumnIndex(ContactsContract.Contacts._ID));
                        String idLokkup = cursor
                                .getString(cursor
                                        .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                        contato.setId(idContato + "&" + idLokkup);
                        contato.setFavorito(null);
                        // pega nome
                        String nome = cursor.getString(cursor
                                .getColumnIndex(Contacts.DISPLAY_NAME));

                        contato.setNome(nome);
                        contatosJson.put("nome", contato.getNome());
                        // pega numeros de telefone
                        if (Integer
                                .parseInt(cursor.getString(cursor
                                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                            ArrayList<String> numeros = new ArrayList<String>();
                            numeros = getNumerosContato(idContato);
                            if ((numeros != null) && (numeros.size() > 0)) {
                                contatosJson.put("numeros", arrayNumero);
                                contato.setNumero(numeros.get(0));
                                contato.setTodosNumeros(numeros);
                            } else {
                                arrayNumero = new JSONArray();
                            }
                        }

                        // pega email
                        ArrayList<String> emails = new ArrayList<String>();
                        emails = getEmails(idContato);
                        if ((emails != null) && (emails.size() > 0)) {
                            contato.setEmail(emails);
                            contatosJson.put("emails", arrayEmail);
                        } else {
                            arrayEmail = new JSONArray();
                        }

                        // pega foto
                        Bitmap foto = getContactPhoto(idContato);
                        if (foto != null) {
                            contato.setImagem(Utils.encodeTobase64(foto));
                        } else {
                            contato.setImagem(null);
                        }
                        if ((contato.getTodosNumeros() != null)
                                && (contato.getTodosNumeros().size() > 0)) {
                            contatos.add(contato);
                        }
                        arrayContato.put(contatosJson);
                    } while (cursor.moveToNext());
                }
            }
        } catch (JSONException e) {
        }
    }

    private ArrayList<String> getNumerosContato(String idContato) {
        ArrayList<String> numeros = new ArrayList<String>();
        Cursor cursor = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[] { idContato }, null);

        if (cursor.moveToFirst()) {
            do {
                String numero = cursor.getString(cursor
                        .getColumnIndex(Phone.NUMBER));
                numeros.add(numero);
                arrayNumero.put(numero);
            } while (cursor.moveToNext());
        }
        if (cursor != null) {
            cursor.close();
        }
        return numeros;
    }

    private ArrayList<String> getEmails(String idContato) {
        ArrayList<String> emails = new ArrayList<String>();

        Cursor cursor = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[] { idContato }, null);
        if (cursor.moveToFirst()) {
            do {
                String email = cursor.getString(cursor
                        .getColumnIndex(Email.DATA));

                emails.add(email);
                arrayEmail.put(email);
            } while (cursor.moveToNext());
        }
        if (cursor != null) {
            cursor.close();
        }

        return emails;
    }

    private Bitmap getContactPhoto(String idContato) {

        Uri uri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI,
                Long.parseLong(idContato));

        InputStream input = ContactsContract.Contacts
                .openContactPhotoInputStream(cr, uri);

        if (input == null) {
            return null;
        }

        return BitmapFactory.decodeStream(input);
    }
</code>
Comment: Está demorando apenas na leitura ou você está enviando para algum webservice?
Comment: É em um webservice que faço a chamada da classe que possui o código que enviei
Comment: Provavelmente deve estar demorando é para comunicar com o webservice então, levando em conta que são 800 contatos.
Answer: Respondi uma questão parecida com essa aqui...
Este link vai te ajudar a clonar a lista de contatos de seu aparelho celular (Android)
Estes links -> (Parte 1 e Parte 2) achei completo, voce pode ler que te ajudará bastante..


Regex to retrieve any alphanumeric otp code

Question: I am trying to get a regex to retrieve alphanumeric OTP code (length of the code maybe dynamic i.e depending on user's choice) and must contain at least one digit.
I have tried the following regex :
 "[a-zA-z][0-9].[a-zA-z]"
But if a special character is there in the code it should result null instead it retrieves the characters before and after the special character which is not desired.
Some sample OTP-containing messages on which the regex is desired to work successfully:

OTP is **** for txn of INR 78.90.
**** is your one-time password.
Hi, Your OTP is ****.

Examples of Alphanumeric OTPs with at least one-digit:

78784
aZ837
987Ny
19hd35
fc82pl
Comment: You can check these links. https://stackoverflow.com/questions/336210/regular-expression-for-alphanumeric-and-underscores , https://stackoverflow.com/questions/5988228/how-to-create-a-regex-for-accepting-only-alphanumeric-characters/5988385
Answer: It would be a bit difficult, maybe this expression might work with an i flag:
[a-z0-9]*\d[a-z0-9]*

or with word boundaries:
(?<=\b)[a-z0-9]*\d[a-z0-9]*(?=\b)

Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "[a-z0-9]*\\d[a-z0-9]*";
final String string = "78784\n"
     + "aZ837\n"
     + "987Ny\n"
     + "19hd35\n"
     + "fc82pl";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.
Comment: I'd encapsulate it with lookbehind (?<=\b) and lookahead (?=\b) just to avoid some false positives, but otherwise it look good.
Answer: Try this one
[0-9a-zA-Z]*[0-9]+[0-9a-zA-Z]*

This evaluates your desired result.
I have tested this in this site
Answer: Your regex is almost correct.
It should be \b[a-zA-z]*[0-9]+[a-zA-z0-9]*\b.


Ajuda com Wifi Manager

Question: Eu preciso ler um valor de 2 edittext sendo eles SDDI e Senha e me conectar nesse access point com essas informações
exemplo:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect2);

        cone = (Button) findViewById(conec);
        sddi = (EditText) findViewById(R.id.editText);
        senha = (EditText) findViewById(R.id.editText2);

        cone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sddi.getText().toString().equalsIgnoreCase("") && senha.getText().toString().equals(" ")) {
                    Toast.makeText(getApplicationContext(), "Emtre com o Sddi e senha", Toast.LENGTH_LONG).show();
                } else {

                    mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

                    // Check for wifi is disabled
                    if (mainWifi.isWifiEnabled() == false) {
                        // If wifi disabled then enable it
                        Toast.makeText(getApplicationContext(), "Wifi desativo, ativando-o",
                                Toast.LENGTH_LONG).show();

                        mainWifi.setWifiEnabled(true);

                        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                        WifiConfiguration wc = new WifiConfiguration();
                        wc.SSID = String.valueOf(sddi);
                        wc.preSharedKey  = String.valueOf(senha);
                        wc.hiddenSSID = true;
                        wc.status = WifiConfiguration.Status.ENABLED;
                        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        int res = wifi.addNetwork(wc);
                        Log.d("WifiPreference", "add Network returned " + res );
                        boolean b = wifi.enableNetwork(res, true);
                        Log.d("WifiPreference", "enableNetwork returned " + b);

                    }

                }

            }
        });

    }
}

Comment: Não entendi a questão.... Você teve algum problema? Aconteceu alguma exceção? Em que você precisa ajuda?
Comment: já consegui obrigado.
Answer: Tente da seguinte forma:
wc.preSharedKey  =  senha.getText().toString();

A senhas é um EditText e o que você quer é o valor do texto ( getText ).
Desta forma : String.valueOf(senha) você está pegando a referencia do objeto.
Comment: desculpa a demora fiz como vc falo e mesmo assim não funcionou.
Comment: Ocorre algum erro? Pode dizer o porque não funcionou? Valeu!
Comment: Consegui, porem eu usei outro código se você quiser eu posso postar aqui

Ajuda com Wifi Manager


how to check ec2 instance-type availability in regions and availability zones

Question: How can we tell which regions and availability zones an instance type is available in? I am looking for to check for c5.xlarge instance type.
i already have one query but that is not giving me proper output.
<code>aws --profile qa ec2 describe-reserved-instances-offerings --filters Name=scope,Values=availability-zone --no-include-marketplace --instance-type 'c5.xlarge' | jq -r '.ReservedInstancesOfferings[].AvailabilityZone' | sort | uniq
</code>
is there also any other way?
Answer: I got the list by running below commands region wise.

C:\Users\sachinsin>aws --profile qa ec2 describe-reserved-instances-offerings --filters "Name=scope,Values=Availability Zone" --no-include-marketplace --instance-type c5.xlarge --region ap-southeast-2 >> ap-southeast-2.txt
C:\Users\sachinsin>aws --profile qa ec2 describe-reserved-instances-offerings --filters "Name=scope,Values=Availability Zone" --no-include-marketplace --instance-type c5.xlarge --region us-west-2 >> us-west-2.txt
C:\Users\sachinsin>aws --profile qa ec2 describe-reserved-instances-offerings --filters "Name=scope,Values=Availability Zone" --no-include-marketplace --instance-type c5.xlarge --region eu-west-1 >> eu-west-1.txt
Comment: When I try this I get the error "The config profile (qa) could not be found". I got around this by omitting the "--profile qa" portion of the command


Null check operator used on a null value. The relevant error-causing widget was StreamBuilder

Question: hi every one hope you doing well I have a code to develop an app by flutter language but this error appears to me in list.dart. this list.dart file should display the posts from firebase with the images if there are.
and I don't know the answer
this is the code
import 'package:flutter/material.dart';
import 'package:insight_software/models/postModel.dart';
import 'package:insight_software/other_screen/userFunction.dart';
import 'package:provider/provider.dart';

import '../models/user.dart';

class ListPost extends StatefulWidget {
  ListPost({Key? key}) : super(key: key);

  @override
  _ListPostState createState() => _ListPostState();
}

class _ListPostState extends State<ListPost> {
  UserService _userService = UserService();
  @override
  Widget build(BuildContext context) {
    List<postModel?> posts = Provider.of<List<postModel?>>(context);
    return ListView.builder(
      itemCount: posts.length,
      itemBuilder: (context, index) {
        final Post = posts[index];

        return StreamBuilder(
            stream: _userService.getUserInfo(Post!.creator),
            builder:
                (BuildContext context, AsyncSnapshot<UserModel?> snapshot) {
              // if (!snapshot.hasData) {
              //   return Center(child: CircularProgressIndicator());
              // }
              return ListTile(
                title: Padding(
                  padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
                  child: Row(
                    children: [
                      snapshot.data!.profileImageUrl != ''
                          ? CircleAvatar(
                              radius: 20,
                              backgroundImage:
                                  NetworkImage(snapshot.data!.profileImageUrl))
                          : Icon(
                              Icons.person,
                              size: 30,
                            ),
                      SizedBox(width: 10),
                      Text(snapshot.data!.name),
                    ],
                  ),
                ),
                subtitle: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(Post.post),
                          SizedBox(height: 20),
                          Text(Post.timestamp.toDate().toString())
                        ],
                      ),
                    ),
                    Divider(),
                  ],
                ),
              );
            });
      },
    );
  }
}

who is knowing how I can solve this problem please help me.
my postModel class:
import 'package:cloud_firestore/cloud_firestore.dart';

class postModel {
  final String id;
  final String creator;
  final String post;
  final Timestamp timestamp;

  postModel(
      {required this.id,
      required this.creator,
      required this.post,
      required this.timestamp});
}

my userModel class:
class UserModel {
  final String id;
  final String bannerImageUrl;
  final String profileImageUrl;
  final String name;
  final String email;

  UserModel({
    required this.id,
    required this.bannerImageUrl,
    required this.profileImageUrl,
    required this.name,
    required this.email,
  });
}

and this is the getUserInfo
UserModel? _userFromFirebaseSnapshot(DocumentSnapshot snapshot) {
    return snapshot != null
        ? UserModel(
            id: snapshot.id,
            name: snapshot.get('First Name') ?? '',
            profileImageUrl: snapshot.get('profileImageUrl') ?? '',
            bannerImageUrl: snapshot.get('bannerImageUrl') ?? '',
            email: snapshot.get('email') ?? '',
          )
        : null;
  }

  Stream<UserModel?> getUserInfo(uid) {
    return FirebaseFirestore.instance
        .collection("Users")
        .doc(uid)
        .snapshots()
        .map(_userFromFirebaseSnapshot);
  }

Comment: Please write your 'postModel' & 'UserModel' Class.
Comment: I added them to the question
Comment: I've  updated my below answer according your Class.
Answer: This error occurs when you use a bang operator (!) on a nullable instance which wasn't initialized.
String? foo; // Nullable String

void main() {
  var len = foo!.length; // Runtime error: Null check operator used on a null value
}

For more details, you can see this answer.
So you can update your code:
stream: _userService.getUserInfo(Post!.creator),

with stream: _userService.getUserInfo(Post?.creator ?? ''),
and snapshot.data!.profileImageUrl != ''
with (snapshot.data?.profileImageUrl ?? '') != ''
and NetworkImage(snapshot.data!.profileImageUrl))
with NetworkImage(snapshot.data?.profileImageUrl) ?? '')
and Text(snapshot.data!.name),
with Text(snapshot.data?.name ?? ''),
Comment: thank you so much is working know❤
Comment: So please accept my answer by clicking tick mark next to the my answer if it solved your problem.
Comment: there is a small error that appears to me the name in this line    Text(snapshot.data?.name ?? ''), should be displayed on the simulator but it its not
Comment: It's because that 'snapshot.data.name' is null so snapshot.data?.name ?? '' returns an empty String.
Comment: how I can fix it ?
Comment: It happens because '_userService.getUserInfo' returns data that contains name = null. You should find that reason.
Comment: Yes, and you should fix that in there.
Comment: I update the question and add the getUserInfo could you take a look and help me please
Comment: Are you sure that 'First Name' in 'name: snapshot.get('First Name') ?? '', ' is correct?! Maybe its spelling is wrong.
Answer:
Error: Null check operator used on a null value

Two way you solve this.

in your model class set variable nullable
final String? profileImageUrl; Use ?.

Or

Set default value if null.
profileImageUrl ?? '' if value is null => ?? ''.


Mysql Join verificar SE dado não existe em outra tabela

Question: TABELA "perfil"
id  nome
1   UserA
2   UserB
3   Usuario1
4   Usuario2
5   Usuario3
6   Usuario4

TABELA "amigos"
id  amigo1  amigo2  status
 2      3       2       1
 4      4       1       1
 5      2       5       1
 6      1       5       1
 7      6       1       0

Caso eu use o join, para retornar os amigos, funciona OK (uso como base o id=1 / UserA)
SELECT perfil.id, perfil.nome,amigos.amigo1, amigos.amigo2, amigos.status FROM perfil
INNER JOIN amigos ON ((amigos.amigo1 = perfil.id) OR (amigos.amigo2 = perfil.id))
WHERE perfil.id != 1 AND (amigos.amigo1 = 1 or amigos.amigo2 = 1) AND amigos.status = '1' GROUP BY perfil.id ORDER BY rand() LIMIT 12

Me retorna
id  nome     amigo1 amigo2  status
 4  Usuario     4        1       1
 5  Usuario3    1        5       1

Minha duvida é, não consigo retornar os valores de quem não é amigo do ID 1 / UserA
O Join dessa maneira (aplicando no WHERE !=1 (ID 1):
SELECT perfil.id, perfil.nome, amigos.amigo1, amigos.amigo2, amigos.status FROM perfil
INNER JOIN amigos ON ((amigos.amigo1 = perfil.id) OR (amigos.amigo2 = perfil.id))
WHERE perfil.id != 1 AND (amigos.amigo1 != 1 or amigos.amigo2 != 1) GROUP BY perfil.id ORDER BY rand() LIMIT 12

Resulta:
id  nome    amigo1  amigo2  status
6   Usuario4    6       1       0
5   Usuario3    2       5       1
4   Usuario2    4       1       1
2   UserB       3       2       1
3   Usuario1    3       2       1

Meu objetivo é que o join faça não aparecer o Usuario3 (ID 5) na lista, pois ele consta como amigo do ID 1.
Comment: SELECT perfil.id, perfil.nome, amigos.amigo1, amigos.amigo2, amigos.status FROM perfil
LEFT JOIN amigos ON ((amigos.amigo1 = perfil.id) OR (amigos.amigo2 = perfil.id))
WHERE perfil.id != 1 AND (amigos.amigo1 IS NULL ) GROUP BY perfil.id ORDER BY rand() LIMIT 12

Tenta desta forma
Comment: Retornou em branco, nenhum dado recuperado
Comment: Guilherme, acho que eu havia entendido sua dúvida errada.

Tente assim:

SELECT perfil.id, perfil.nome,amigos.amigo1, amigos.amigo2, amigos.status FROM perfil
INNER JOIN amigos ON ((amigos.amigo1 = perfil.id) OR (amigos.amigo2 = perfil.id))
WHERE perfil.id != 1 AND (amigos.amigo1 != 1 and amigos.amigo2 != 1) AND amigos.status = '1'
Comment: @LucasBrogni então meu querido amigo, infelizmente o resultado ainda não foi esperado (mesmo eu fazendo alguns ajustes removendo o status,etc), ainda aparece outros usuários (ID 5 / Usuario3 e duplicidade do userB) na busca, a remoção do agrupamento tornou menos eficaz, e informação complementar: o status tem que ser diferente de 1(status:1 significa amigo), de preferencia NULL.
Comment: Se serve de apoio para a vossa colaboração, esse script (pastebin) é o que já uso atualmente, ele "serve" mas é extremamente ineficaz https://pastebin.com/Jy0XsSaC
(desconsidere os campos desnecessários a questão)
Answer: /**
selecionar todos que nao sao amigos do id=1
**/
SELECT *
FROM perfil p
WHERE
 id!=1
AND
 id NOT IN (
 SELECT pa.id
 FROM perfil p
 JOIN amigos a ON (a.amigo1=p.id OR a.amigo2=p.id)
 JOIN perfil pa ON pa.id=IF(a.amigo1=p.id, a.amigo2, a.amigo1)
 WHERE p.id=1
 AND a.status=1
);


Symmetric matrix factorization of a $2 \times 2$ symmetric matrices

Question: Let S be a symmetric matrix such that
$$S= \begin{bmatrix}
A & B\\
B & C
\end{bmatrix}, \text{where A, B, C $\in$ $\mathbb{Z_P},$}$$
 where p is prime.
I am tasked to get the solution(s) of the symmetric matrix factorization of $2 \times 2$ symmetric matrices of the forms,
$$S = X^2(\mod p)$$
and
$$S = XY(\mod p),$$
where $X$ $\neq$ $Y$ but are both symmetric.
For example, under $\mathbb{Z_3}$,
$$S = \begin{bmatrix}
2 & 2\\
2 & 1
\end{bmatrix} = \begin{bmatrix}
1 & 2\\
2 & 0
\end{bmatrix} \begin{bmatrix}
1 & 2\\
2 & 0
\end{bmatrix}(\mod 3) = X^2$$
and
$$S = \begin{bmatrix}
1 & 2\\
2 & 2
\end{bmatrix} = \begin{bmatrix}
2 & 0\\
0 & 1
\end{bmatrix} \begin{bmatrix}
2 & 1\\
2 & 2
\end{bmatrix}(\mod 3) = XY$$
I know that these pairs are not the only possible factors of the given matrix S. I've also done programming the matrix multiplication which is the inverse of matrix factorization because I have no idea how to start with matrix factorization.
Any suggestions will be much appreciated. Thanks in advance!
Comment: Is $S$ assumed to be invertible? If not, there are a whole lot of degenerate cases to consider.
Comment: Hi. Based on the examples that we have, both invertible and not invertible cases are to be observed. For the non-invertible matrix cases, what would be the best option that we could do? Thank you very much!
Comment: My answer already covers non-invertible matrices; they fall under points 1 and 3.
Answer: $S$ is symmetric and hence disagonalisable. First, we find the eigenvalues and eigenvectors of $S$. The characteristic polynomial is $\chi_S(t)=|S-tI|=(A-t)(C-t)-B^2$, which has roots $\lambda_1=\frac{1}{2}\left(\sqrt{A^2-2AC+4B^2+C^2}+A+C\right), \lambda_2=\frac{1}{2}\left(-\sqrt{A^2-2AC+4B^2+C^2}+A+C\right)$. Let $v=(v_1, v_2)^t, u=(u_1, u_2)^t$ be their corresponding eigenvalues. Then we have $Sv=\lambda_1 v, Su=\lambda_2 u$. A tedious calculation gives a possible solution $v=\left(-\frac{-A+C+\sqrt{A^2-2AC+4B^2+C^2}}{2B}, 1\right)$ and $u=\left(\frac{A-C+\sqrt{A^2-2AC+4B^2+C^2}}{2B}, 1\right)$. Let $P$ be the matrix with columns $v, u$ and $D=\operatorname{diag}(\lambda_1, \lambda_2)$. Then $S=PDP^{-1}$. Hence, letting $D'=\operatorname{diag}\left(\sqrt{\lambda_1}, \sqrt{\lambda_2}\right)$ we get that $D'^2=D$ and hence $S=PD'^2P^{-1}=(PD'P^{-1})(PD'P^{-1})$. Now setting $X=PD'P^{-1}$ we get $S=X^2$. Of course, we can set $X=PD'$ and $Y=P^{-1}$ to get $S=XY$. Note I assumed $A^2-2AC+4B^2+C^2, \lambda_1, \lambda_2$ turn out to be squares in $\mathbb{Z}_p$, and I'm uncertain on how to prove this; I'm not even sure if it is true. Suggestions in the comments would be appreciated.
Comment: It need not be the case that the discriminant is a square; counterexamples are plentiful, a simple example for $p=3$ is $A=B=1$, $C=0$.
Comment: @Servaes Yeah, I assumed so. How could one bypass this issue, then?
Comment: Well the discriminant is certainly a square in $\Bbb{F}_{p^2}$, and so the matrix is diagonalizable over this field. The remainder of your argument also has quite a few problems though; have you found all solutions?
Answer: First, if $S$ is diagonal then this is fairly straightforward. We distinguish three cases:

If $S=0$ then there are $(p-1)^2(p+1)$ pairs $(X,Y)$ of nonzero matrices such that $S\equiv XY\pmod{p}$, and $2p^4-1$ such pairs $(X,Y)$ with either $X=0$ or $Y=0$.
If $B=0$ and $A=C\neq0$ then $S=AI$, and hence the solutions to
$$S\equiv XY\pmod{p},$$
are precisely the pairs $(X,Y)$ where $X$ is invertible and $Y=AX^{-1}$. In particular there are $(p-1)^2p(p+1)$ solutions $(X,Y)$.
If $B=0$ and $A\neq C$ then both $X$ and $Y$ must be diagonal and so
$$A\equiv x_{11}y_{11}\pmod{p}\qquad\text{ and }\qquad B\equiv x_{22}y_{22}\pmod{p}.$$
There are $(p-1)^2$ solutions if $A\neq0$ and $C\neq0$, and $(p-1)(2p-1)$ solutions if $AC=0$.

From here one we assume that $B\neq0$. If $A=C=0$ then for the matrix $R:=\binom{0\ 1}{1\ 0}$ we have
$$SR=\begin{pmatrix}
0&B\\B&0
\end{pmatrix}
\begin{pmatrix}
0&1\\1&0
\end{pmatrix}
=\begin{pmatrix}
B&0\\0&B
\end{pmatrix},$$
so $SR$ is diagonal. The above tells us the solutions $(X,Y)$ for $SR$, and hence because $R^2=I$ we have
$$S\equiv XYR\pmod{p},$$
so the solutions for $S$ are the pairs $(X,YR)$ where $SR\equiv XY\pmod{p}$.
If $A$ and $C$ are not both zero then wlog $A\neq0$. For the matrix $T:=\binom{1\ \hphantom{-}B}{0\ -A}$ we have
$$T_A^{\intercal}ST_A=\begin{pmatrix}
1&0\\B&-A
\end{pmatrix}\begin{pmatrix}
A&B\\B&C
\end{pmatrix}
\begin{pmatrix}
1&B\\0&-A
\end{pmatrix}
=\begin{pmatrix}
A&0\\0&A|S|
\end{pmatrix},$$
where $|S|=\det S$, so $T_A^{\intercal}ST_A$ is diagonal. Again the argument above tells us the solutions for $T_A^{\intercal}ST_A$. Note that $T_A$ is invertible because $A\neq0$, and so
$$S\equiv T_A^{-\intercal}XYT_A^{-1}\pmod{p},$$
and hence the solutions for $S$ are the pairs $(T_A^{-\intercal}X,YT_A^{-1})$ where $T_A^{\intercal}ST_A\equiv XY\pmod{p}$.
Comment: I am currently checking your suggestions and I am amazed by how i can verify the results of my work from your masterpiece. Thank you very much!
Comment: Hello again, for now, I'm a bit confused on how you came out of the idea of the matrix T and what follows. Correct me if I am mistaken but should it be the negative of the determinant of S (multiplied by -A) instead? Also, Am I the one to put an invertible matrix X or does it have a way on how to get it? Thankfully I get most of the parts now but still need some clarifications. :)
Comment: @RowellSanJose You are right, I have corrected this coefficient. I am not sure what you are asking, but this method works for *all* matrices, not just invertible matrices.


Confidence Intervals for proportions in a small sample of unknown distribution

Question: I'd like to calculate proportion confidence intervals for a labor economics survey of 2000 people, but the subpopulations I'm looking at are often 10 or fewer people. I'm not sure if it's appropriate to assume normality and was looking for some non-parametric way to calculate proportions.
Within the survey, I'm looking at workers who are urbanized, skilled, or self-employed. When broken down by year, it narrows to a small N by sub-population. The histograms are often heavily skewed, depending on the variables used.
So, for instance, let's say I have data (in R)
year <- c(2000, 2001, 2002)
total <- c(1000, 1000, 1000)
selfemployed <- c(4, 5, 6)
test <- data.frame(year, total, selfemployed)

And I want to estimate some form of upper and lower-bound estimates of the number of self-employed in the population, what is a good method? This is not for regression or modeling, just descriptive statistics. I saw a discussion of bootstrapping methods here but am unsure the best method. Thanks, and please let me know if I can make this question more clear.
Comment: small samples and bootstrap are not good friends
Comment: I don't see what about this is unclear.
Answer: Since the proportions are close to the boundary of the parameter space (0 in this case), I would not trust the normal approximation. Instead, I would advocate exact confidence intervals based on the binomial distribution. Note that these intervals may be skewed and include 0.
Answer: There is actually a substantial literature on estimating binomial proportions and their CI. A good entry point is Agresti and Coull. It turns out that the exact estimates are not optimal.  R offers several CI for binomial proportions in the binom package. For your data:
install.packages("binom")
library(binom)

binom.confint(4, 1000, alpha=0.05,
        method= "all",
        include.x=FALSE, include.n=FALSE, return.df=FALSE)

shows a wide variety of confidence intervals. (And similar for 5 and 6).


Is there a standard tool to trim cache directories?

Question: I want to run a cron job that removes old entries in a cache directory, so that its total size is reduced to a certain amount of bytes. I've concocted this one line shell command, but I was wondering: is there a standard Linux tool (like logrotate for rotating logs) that does the same?
The oneliner is as follows:
<code>find /some/cache/dir -type f -printf "%T@ %s %p\n" | sort -rn | awk '{ s += $2 ; if (s > 100000) print $3 }' | xargs rm
</code>
I use find to gather the timestamp, filesize and filepath of each file. I then sort by timestamp, newest files first. I then calculate the cumulative filesize, outputting files only once a certain limit has been reached. The outputted files are then deleted.
Answer: I've never seen a "standard" way to do this - because there is no "standard" when it comes to how applications cache content: duration, total space used, etc is all dependent upon the specific caching methodologies of the given tool.
What happens when you "trim" something that is currently in use? Just because something was cached a while ago doesn't mean it's not still being handed-out ... and it easily could be being handed-out while you are trying to delete it - and that's bad juju all around.
In the overwhelming majority of cases, you should use the app's configuration manager (be it config file, db, etc) to manage its own caching strategy.
I can name one product at least where the file names cached have absolutely no correlation to the files themselves: and just because it was cached a month ago does not mean it isn't still in active use, because the app doesn't update timestamps on cached files - ever. It tracks least-recently-accessed inside itself, and the way to manage cache size is to limit the partition/mount point where the cache is housed (as it will use up to and including the entire space available in its cache directory).
Don't edit application's caches unless you really know what will happen when you do.
Comment: My cache trimming job is intended for an application that I'm maintaining myself, so there is no configuration to be managed :)

My take-away is that cache trimming should be part of the code base of the application, not a oneliner in the crontab.
Comment: @ScatoEggen :: don't put it external to the app, no :)


How to add variable error to regex fuzzy search. Python

Question: import regex,re

sequence = 'aaaaaaaaaaaabbbbbbbbbbbbcccccccccccc' #being searched
query = 'aaabbbbbbbbbbbbccc' #100% coverage
query_1 = 'aaaabbbbbbbbcbbbcccc' #95% coverage
query_2 = 'aaabbbbcbbbbbcbccc' #90% coverage

threshold = .95
error = len(query_1) - (len(query_1)*threshold) #for query_1 errors must be <= 1

print regex.search(query_1 + '{e<={}}'.format(error),sequence).group(0)

Im trying to add additional parameters to a regex search so it only works if a certain percentage of the query is in sequence being searched.
For example, if I wanted it to be at least 95% coverage it would work for query_1 but it would not work for query_2
Comment: The fuzzy matching capabilities of the [regex module](https://pypi.python.org/pypi/regex) might be what you are looking for.
Answer: Using the regex module:
import regex
sequence = 'aaaaaaaaaaaabbbbbbbbbbbbcccccccccccc' #being searched
query = 'aaabbbbbbbbbbbbccc' #100% coverage
query_1 = 'aaaabbbbbbbbcbbbcccc' #95% coverage
query_2 = 'aaabbbbcbbbbbcbccc' #90% coverage
threshold = 0.97
queries = (query, query_1, query_2)
for q in queries:
    error = int(len(q) - (len(q)*threshold))
    m = regex.search(r'(%s){e<=%d}'%(q,error), sequence)
    print 'match' if m else 'nomatch'

Comment: what is it called when you add (%s)(%d)%(variable1,variable2) ? i want to look at the documentation because i've seen that before @perreal
Comment: it's called old style string format: http://docs.python.org/2/library/stdtypes.html#string-formatting


How do i compare NDCG values?

Question: I want to compare different ranking models using the NDCG metric. I average the NDCG of the models, then use the difference between the avg. NDCGs to compare the models.
My questions:

What does it mean for a ranker to have 0.1 (avg.) NDCG more than another ranker? Can i say more than: "It ranks a little better"? Especially, how big a difference in NDCG would be "significant" in absolute terms?
Can i say something about statistical significance of the difference of the average NDCG  w.r.t the 0-Hypothesis? I'm quite unsure about whether normality assumption holds here... Especially since i'm not looking at the average of the differences, but the difference of the averages.
Answer: After you get an insight from the 2 means, then compare the 2 NCDG arrays (do not use the means), using for example wilcoxon signed-rank test to check whether the difference previously seen is relevant or not.
In other words, you use the test paired by queries.
The same holds if you were comparing P@K, MAP etc

How do i compare NDCG values?


hadoop namenode port in use

Question: This is actually a standby HA namenode.  It was configured with the same settings as the primary and hdfs namenode -bootstrapStandby was successfully run.  It begins coming up on the standard HTTP port 50070 as defined in the config file:
<property>
  <name>dfs.namenode.http-address.ha-hadoop.namenode2</name>
  <value>namenode2:50070</value>
</property>

The start up begins OK then hits:
15/02/02 08:06:17 INFO hdfs.DFSUtil: Starting Web-server for hdfs at: http://hadoop1:50070
15/02/02 08:06:17 INFO mortbay.log: Logging to org.slf4j.impl.Log4jLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
15/02/02 08:06:17 INFO http.HttpRequestLog: Http request log for http.requests.namenode is not defined
15/02/02 08:06:17 INFO http.HttpServer2: Added global filter 'safety' (class=org.apache.hadoop.http.HttpServer2$QuotingInputFilter)
15/02/02 08:06:17 INFO http.HttpServer2: Added filter static_user_filter (class=org.apache.hadoop.http.lib.StaticUserWebFilter$StaticUserFilter) to context hdfs
15/02/02 08:06:17 INFO http.HttpServer2: Added filter static_user_filter (class=org.apache.hadoop.http.lib.StaticUserWebFilter$StaticUserFilter) to context logs
15/02/02 08:06:17 INFO http.HttpServer2: Added filter static_user_filter (class=org.apache.hadoop.http.lib.StaticUserWebFilter$StaticUserFilter) to context static
15/02/02 08:06:17 INFO http.HttpServer2: Added filter 'org.apache.hadoop.hdfs.web.AuthFilter' (class=org.apache.hadoop.hdfs.web.AuthFilter)
15/02/02 08:06:17 INFO http.HttpServer2: addJerseyResourcePackage: packageName=org.apache.hadoop.hdfs.server.namenode.web.resources;org.apache.hadoop.hdfs.web.resources, pathSpec=/webhdfs/v1/*
15/02/02 08:06:17 INFO http.HttpServer2: HttpServer.start() threw a non Bind IOException
java.net.BindException: Port in use: hadoop1:50070
        at org.apache.hadoop.http.HttpServer2.openListeners(HttpServer2.java:890)
        at org.apache.hadoop.http.HttpServer2.start(HttpServer2.java:826)
        at org.apache.hadoop.hdfs.server.namenode.NameNodeHttpServer.start(NameNodeHttpServer.java:142)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.startHttpServer(NameNode.java:695)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.initialize(NameNode.java:585)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.<init>(NameNode.java:754)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.<init>(NameNode.java:738)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.createNameNode(NameNode.java:1427)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.main(NameNode.java:1493)
Caused by: java.net.BindException: Cannot assign requested address
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:444)
        at sun.nio.ch.Net.bind(Net.java:436)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
        at org.mortbay.jetty.nio.SelectChannelConnector.open(SelectChannelConnector.java:216)
        at org.apache.hadoop.http.HttpServer2.openListeners(HttpServer2.java:885)
        ... 8 more
15/02/02 08:06:17 INFO impl.MetricsSystemImpl: Stopping NameNode metrics system...
15/02/02 08:06:17 INFO impl.MetricsSystemImpl: NameNode metrics system stopped.
15/02/02 08:06:17 INFO impl.MetricsSystemImpl: NameNode metrics system shutdown complete.
15/02/02 08:06:17 FATAL namenode.NameNode: Failed to start namenode.
java.net.BindException: Port in use: hadoop1:50070
        at org.apache.hadoop.http.HttpServer2.openListeners(HttpServer2.java:890)
        at org.apache.hadoop.http.HttpServer2.start(HttpServer2.java:826)
        at org.apache.hadoop.hdfs.server.namenode.NameNodeHttpServer.start(NameNodeHttpServer.java:142)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.startHttpServer(NameNode.java:695)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.initialize(NameNode.java:585)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.<init>(NameNode.java:754)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.<init>(NameNode.java:738)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.createNameNode(NameNode.java:1427)
        at org.apache.hadoop.hdfs.server.namenode.NameNode.main(NameNode.java:1493)
Caused by: java.net.BindException: Cannot assign requested address
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:444)
        at sun.nio.ch.Net.bind(Net.java:436)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
        at org.mortbay.jetty.nio.SelectChannelConnector.open(SelectChannelConnector.java:216)
        at org.apache.hadoop.http.HttpServer2.openListeners(HttpServer2.java:885)
        ... 8 more
15/02/02 08:06:17 INFO util.ExitUtil: Exiting with status 1
15/02/02 08:06:17 INFO namenode.NameNode: SHUTDOWN_MSG:
/************************************************************
SHUTDOWN_MSG: Shutting down NameNode at hadoop1.marketstudies.com/192.168.1.125
************************************************************/

I have tried changing the http-address port by setting:
<property>
  <name>dfs.namenode.http-address.local1-hadoop.hadoop1</name>
  <value>hadoop1:10070</value>
</property>

But then I get the same as above only with the new port:
15/02/02 08:16:51 INFO hdfs.DFSUtil: Starting Web-server for hdfs at: http://hadoop1:10070
...
java.net.BindException: Port in use: hadoop1:10070
...
java.net.BindException: Port in use: hadoop1:10070

This is working with the same config on the primary namenode.
This Question seems to be similar to my issue but the Answer didn't help.  I tried setting dfs.http.address to other things and it didn't change anything.  I belive this is a non-HA config option replaced in HA with dfs.namenode.http-address.ha-name.namenodename
There is nothing actually listening to the http port as can be seen from here:
# netstat -anp |grep LIST
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      946/sshd
tcp        0      0 0.0.0.0:46712           0.0.0.0:*               LISTEN      2066/java
tcp        0      0 0.0.0.0:50010           0.0.0.0:*               LISTEN      28892/java
tcp        0      0 0.0.0.0:50075           0.0.0.0:*               LISTEN      28892/java
tcp        0      0 0.0.0.0:8480            0.0.0.0:*               LISTEN      1471/java
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      2358/zabbix_agentd
tcp        0      0 0.0.0.0:50020           0.0.0.0:*               LISTEN      28892/java
tcp        0      0 0.0.0.0:8485            0.0.0.0:*               LISTEN      1471/java
tcp        0      0 0.0.0.0:8040            0.0.0.0:*               LISTEN      2066/java
tcp        0      0 0.0.0.0:8042            0.0.0.0:*               LISTEN      2066/java
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1020/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      946/sshd

Tried starting as root user to see if it's some kind of perms problem for listening to the port but that gives the same error.
Answer: Found the issue.  This came from a short history of this server where the IP address changed, but the /etc/hosts file just had the new one appended to it rather than replaced.  I think this was confusing the Hadoop start up as it was trying to open 50070 on a non-existent interface.  The error being "port in use" made this a little confusing.
Answer: download osquery https://code.facebook.com/projects/658950180885092
and install
issue this command osqueryi
when prompt appears use this sql command to see all running java processes and find the pids
SELECT name, path, pid FROM processes where name= "java";
you will get something that looks like this on a mac
  +------+--------------------------------------------------------------------------+-------+
  | name | path                                                                     | pid   |
  +------+--------------------------------------------------------------------------+-------+
  | java | /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java | 59446 |
  | java | /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java | 59584 |
  | java | /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java | 59676 |
  | java | /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java | 59790 |
  +------+--------------------------------------------------------------------------+-------+

issue the sudo kill - PID command on all the processes to make sure that it kills the port in use 0.0.0.0:50070
then after all this retry the sbin/start-dfs.sh and the namenode should now show
Comment: This doesn't show which process has the port.  ```netstat -anp |grep LISTEN``` will give me all the processes that are listening, this didn't help due to the fact it wasn't a process already running problem, but a messed up /etc/hosts file.
Answer: Use the following command for checking the processes which are running and using java:

ps aux | grep java

After that, kill all the processes which are related to hadoop using the PID from above command as follows:

sudo kill -9 PID

hadoop namenode port in use


Editing user permissions or restoring SQLBuddy to default, for Wampserver?

Question: I accidentally locked myself out by creating a new user account (with what I thought had full privileges) and deleting root altogether instead of editing it.  I'm not the brightest bulb in the box sometimes.
Basically, how can I restore my full user privileges -- either by restoring everything to default or modifying my current configuration for SQLBuddy?
I've looked online and I'm having trouble finding an appropriate solution.
Answer: Woops, you are not having a good time recently are you.

Stop the mysql service
wampmanager -> MySQL -> Service -> Stop Service
Edit the my.ini file
wampmanager -> MySQL -> my.ini
Find the [wampmysqld] section in the ini file
Add this line directly after the section [wampmysqld]
skip-grant-tables
Restart the mysql service.
wampmanager -> MySQL -> Service -> Start/Resume Service
Open the MySQL console
wampmanager -> MySQL -> MySQL Console
just hit enter when asked for the password.

Now we are going to give all privilages to your new account.
enter the following 2 commands at the mysql> command prompt, each with a semi colon at the end of a line, and press ENTER after each line to issue the command to mysql.
GRANT ALL PRIVILEGES ON *.* TO 'Your_Userid'@'localhost' WITH GRANT OPTION;

FLUSH PRIVILEGES;

Note If you created this new user with more than one domain i.e. xxx@localhost and email@example.com you should do this for each domain

Now enter 'quit' at the mysql command promt to exist mysql.
Stop the mysql service
wampmanager -> MySQL -> Service -> Stop Service
Edit the my.ini file
wampmanager -> MySQL -> my.ini
Find the [wampmysqld] section in the ini file
Remove the 'skip-grant-tables' parameter we added earlier.
DO NOT Leave this parameter in the ini file its a HUGH security hole.
Restart the mysql service.
wampmanager -> MySQL -> Service -> Start/Resume Service

You should now be able to login with phpmyadmin or SQLBuddy using your new useridand the new password you have just set for that user.


AWS SDK installation problem

Question: I used Drush to install Amazon S3 module. AWS SDK was installed as part of the process. No error was thrown
[root@ip-10-128-146-179 zencoder]# drush dl amazons3
Project amazons3 (7.x-1.0-beta7) downloaded to                         [success]
/var/www/html/drupal/sites/all/modules/amazons3.
[root@ip-10-128-146-179 zencoder]# drush en amazons3
The following projects have unmet dependencies:
amazons3 requires awssdk
Would you like to download them? (y/n): y
Project awssdk (7.x-5.4) downloaded to                                 [success]
/var/www/html/drupal/sites/all/modules/awssdk.
Project awssdk contains 2 modules: awssdk_ui, awssdk.
The following extensions will be enabled: amazons3, awssdk
Do you really want to continue? (y/n): y
amazons3 was enabled successfully.                                     [ok]
awssdk was enabled successfully.                                       [ok]

Now I see an error in my Drupal admin dashboard:
Failed to load the AWSSDK
Please make sure the AWSSDK library is installed in the libraries directory. Use the drush make file for easy installation.

What shoud I do?
This is my drush status:
Drupal version                  :  7.22
 Site URI                        :  http://default
 Database driver                 :  mysql
 Database hostname               :  localhost
 Database username               :  drupal
 Database name                   :  drupal
 Database                        :  Connected
 Drupal bootstrap                :  Successful
 Drupal user                     :  Anonymous
 Default theme                   :  bartik
 Administration theme            :  seven
 PHP configuration               :  /etc/php.ini
 Drush version                   :  5.9
 Drush configuration             :
 Drupal root                     :  /var/www/html/drupal
 Site path                       :  sites/default
 File directory path             :  sites/default/files
 Temporary file directory path   :  /tmp

Answer: Using the AWS PHP SDK version 1.6.2 instead of the latest solved the problem. sites/all/libraries/awssdk/sdk.class.php does not apply for at least version 2.1.1.
Answer: Simply download the AWSSDK library and extract it to your sites/all/libraries folder following the format sites/all/libraries/awssdk/sdk.class.php.
Comment: sites/all/libraries/awssdk/sdk.class.php does not exist for the latest version of AWS SDK for PHP. So I had to use version 1.6.2
Comment: version 2.0 wasn't out when release 5.4 was created. So yes you were supposed to use version 1.6. My guess is it now works for you.
Answer: I ran this command from the Drupal root after installing the awssdk module and it worked:
drush make --no-core sites/all/modules/awssdk/awssdk.make
By worked, I mean it set up the library and fixed the "Failed to load the AWSSDK" error.
The command is referenced here: https://www.drupal.org/node/1353114#comment-5300564

AWS SDK installation problem


How to set image as background

Question: I am using bartik theme. I have an image that I like to use as background of the home page only. Admin > appearance > Bartik > settings > I see different colors to set as background. Can I have an image as background instead of the color for the home page. And for other pages I can use a color.
Thanks.
D
Answer: unless you want to add it to a theme setting so that it can be adjusted by UI, the simplest way is probably to override page--front.tpl.php in your theme template directory, and manually add the html to create the background.
For more info on page template naming, see here.
Answer: Consider using the ThemeKey module. From its project page (bold markup added here):

... allows you to define simple or sophisticated theme-switching
rules which allow automatic selection of a theme depending on current
path, taxonomy terms, language, node-type, and many, many other
properties.
In combination with Drupal's theme inheritance you can easily achieve features like:

a front-page / "splash" screen
...

So by creating a subtheme of the Bartik theme to be used on the home page, and another subtheme for all other pages, it should achieve your goal, no?
Comment: Any feedback about this answer?

How to set image as background


Giving a user the choice to appear offline in a view showing online users

Question: I created a views block which displays users which are online. I would like to give a user a choice to be able to appear offline (i.e not be listed in the block) when he/she is online.
Thank you in anticipation.
Answer: Assuming Drupal 7, you can go to <code>/admin/config/people/accounts/fields</code>, attach a Boolean field to user accounts to allow the user to select their preference, then filter on that user field in your view to exclude those who opt out.
Comment: Please, do you know if there is a way using views, to make the Boolean field appear as an attachment to the original block? In this way users can choose to appear offline without going to there user account page. Thanks.
Comment: This is another good question. Please post it as a separate question.
Comment: Okay, will do so.


Uncaught TypeError: $(...).Attr is not a function

Question: porque não funciona?
<code>$('#chatEnvia').click(function() {
  if ($("#chatEscrita").val() !== "") {
    $.post("inc_chatEnvia.php", {
      acesso: "ok",
      msg: $("#chatEscrita").val(),
      de: $("#de").val(),
      para: $("#para").val()
    });
    $("#chatEscrita").val("");

    $("#chatEnvia").Attr('data-toggle', 'modal');
    $("#chatEnvia").Attr('data-target', '#chatVazio');

  }
});
</code>

Uncaught TypeError: $(...).Attr is not a function

<code>$("#chatEnvia").Attr('data-toggle', 'modal');
$("#chatEnvia").Attr('data-target', '#chatVazio');
</code>
Comment: Tente `attr`, com letra minúscula.
Comment: @VictorStafusa kkkk Deu certo!...rsrs Obrigado.
Answer: Chamadas de funções são case sensitive, no seu exemplo, você está utilizando ".Attr()", com letra maíscula:
<code> $("#chatEnvia").Attr('data-toggle', 'modal');
</code>
Tente utilizar com letra minúscula:
<code>$("#chatEnvia").attr('data-toggle', 'modal');
</code>
Documentação: http://api.jquery.com/attr/
OBS: Todas as funções jQuery seguem um padrão lowerCamelCase, ou seja, a letra inicial sempre será minúscula. Mais informações: https://pt.wikipedia.org/wiki/CamelCase


Subsampling with rare independent variables

Question: Say I have the model,
$Y_i = B_0 + B_1X + e_i$
and I have $N$ samples of size $k$. Let $X$ be a dummy variable.
In some samples, I have variation on $X$, and in some I don't (all $0$ or all $1$). For each sample, I attempt to estimate $B_0$ and $B_1$ using OLS. In samples where there is no variation, then $B_1$ is ill-defined.
Assuming that the samples of size three are drawn from the same underlying distribution, what would be the consequence for the distribution of $B_0$, $B_1$ if I discarded samples without variation on $X$. It seems like it should be benign.
Comment: I think that to get some more usefull response, you should tell us about your real aplication!
Comment: Fair point. http://stats.stackexchange.com/questions/135073/confidence-credible-intervals-for-parameter-estimates-from-structured-support-ve
Answer: It will not be benign. It will have the effect of biasing your estimates to think that the frequency of $X$ is more balanced than it really is. This effectively changes the distribution of your data.
For instance, suppose that you have only one instance where $X=1$ and $X=0$ everywhere else. Then, applying the procedure you described will cause you to discard $N-1$ of your $N$ samples, which is basically equivalent to down-weighting the instances where $X=0$ by a factor of about $N$ (since only about $1/N$ of them will appear in your single sample). This is extreme, but the effect will persist with less extreme numbers.
Assuming you have more than $N$ instances in each class of $X$, a better solution is to use stratified random sampling to ensure that each subsample has (roughly) the same class frequencies.
Comment: I see what you mean. Do you know of any references which discuss stratification for subsampling?
Comment: The R [sampling](http://cran.r-project.org/web/packages/sampling/sampling.pdf) package may be what you're looking for. Weirdly, I can't find a great exposition of it, but [wikipedia](https://en.wikipedia.org/wiki/Cross-validation_(statistics)) and the [Food and Agriculture Organization](http://www.fao.org/docrep/x5684e/x5684e04.htm) have decent ones (if you can get past the extremely fish-themed examples in the latter...)
Comment: See Cochran's Sampling Techniques, 1977 or Leslie Kish's "Survey Sampling."  Any basic sampling book should discuss stratified random sampling.
Comment: I understand stratified sampling, but I'm looking for references on stratification for [subsampling](http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/ebooks/html/csa/node132.html)


Which is faster, XML + XSL or CLR + DataBinding

Question: Which is faster, XML with XSLT or  CLR with DataBinding?
I'm assuming that it's CLR + Databinding but I could be wrong.
Comment: It varies based on individual cases...
Comment: Thanks for asking the question man... Only because I love comparing the two... Most others would probably say it varies based on individual cases...
Answer: This is actually a question very close to my own heart, as almost all the work I do revolves around XSLT design layers with a custom .NET based backend that generates data (I love the system to death).
To the best of my knowledge, there are a few things that should be kept in mind:

Make absolutely sure you use cached instances of System.Xml.Xsl.XslCompiledTransform. This class uses System.Reflection.Emit to create on demand classes that will absolutely PUMP OUT your xslt transformation incredibly fast

Use the right data structure as the input into your xslt transformations. If you have an XmlDocument (or better yet an XPathDocument) available, use that. Otherwise, for very large input documents and transformations pass in an xml reader (if available), since XslCompiledTransform will load the document into a XPathDocument (which are optimized for XPath access). Just to add, there exists an extension method in System.Xml.XPath for the System.Xml.Linq.XElement type that will create an XPathNavigator from a XElement, which will come in handy if your source data structure is an XElement
Do not use msxsl:script tags in your xsl transformations. msxsl:script tags are compiled differently than the rest of the xslt and can cause serious memory leaks in high-demand applications (they load custom assemblies EVERY TIME the xslt is run)
Avoid the use of extension methods as much as possible. I disassembled (reflector FTW) right down to the .NET source for executing extension methods in XSLT transformations and at the very heart it really is nothing more than a call to MethodInfo.Invoke(). A few calls won't ruin your application, but don't think you can make up for all of XSLT's shortcomings using extension methods (might change in a future version of the framework, since they are caching extension methods in a custom hashing system, it is very possible that they could translate this to use compiled linq expressions, in which case it would be lightning fast)
To the best of my knowledge, System.Web.UI.DataBinder still boils down to a call in System.ComponentModel.ReflectPropertyDescriptor that uses System.Reflection.MethodInfo.Invoke() in order to evaluate Eval("MyProperty") statements. This is going to be one of the biggest comparisons in terms of performance between the two models. By minimizing the number of reflection calls, XSLTs get an upper hand here.
It is very very easy to write un-tuned xslt files. Proper use of xsl variables can really eliminate many of the iterations required to produce the xml output. If you have a commonly referenced input element, store it in an xsl variable an access it from there.
Depending on whether or not you plan on writing your xsl output directly to the response stream. Remember to size your buffers properly. By settings a default buffer size on your transformation MemoryStream's you save yourself a massive amount of memory allocations (which are generally rather large in the context of an xsl transformation).
Although this really comes down to an application level concern. While using XSLT transformations you avoid the whole overhead of control creation, view state serialization, event persistence etc etc etc... Creates a much simpler page life cycle (get data, transform, with a few niceties thrown in between) with should give another edge to an XSLT based system.

Overall, my money is DEFINITELY on a well structures properly implemented XSLT transformation. Especially on a system that has plenty of RAM available for the in memory transformations. I've seen XSLT transformations scale to a fairly incredible level, and once a few very key points are learned, it really isn't that hard to maintain.
Will see if I can remember any others and will edit this post if I do...
Comment: The question's vague enough that it's hard to know how much of this answer is really relevant to it, but even so it's great information.
Answer: You should benchmark your particular use case instead of trying to get a (probably wrong) answer based on false generalizations.


How to whitelist a large list of IP addresses on AWS EC2 instances?

Question: I have a large list of IPs (>100) that need to be whitelisted for both inbound and outbound communication on EC2 instances. Is there a way to whitelist them collectively? (The IPs also don't belong to a range and are discontinuous). We are currenlty using security groups for whitelisting IPs, but I couldn't find an easy way to whitelist a large collection of IPs.
PS- I tried exploring IP sets in AWS WAF, but it requires setting up an application load balancer, additionally since we are already using security groups, blocking IPs at the application layer (via WAF) will also block IPs that are already whitelisted at the EC2 level.
Thanks in Advance!
Answer: You can use managed prefix lists which allows you to make it easier to configure and maintain your security groups.
Keep in mind that every entry in the prefix list count as a rule in your SG, so probably you will need to request an increase of the default quota (60 inbound and 60 outbound rules per security group).
Answer: For an EC2 port access the best option you got is security groups. But you better use IaC to manage this such as Terraform or CloudFormation - it will help you to better manage the IP list and save them all in one file.
Here is a reference for Terraform: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
Answer: You can use IP prefix list to simplify your design and use it where required.
You can use below link for more info.
https://aws.amazon.com/about-aws/whats-new/2020/06/amazon-virtual-private-cloud-customers-use-prefix-lists-simplify-configuration-security-groups-route-tables/
https://aws.amazon.com/blogs/networking-and-content-delivery/simplify-network-routing-and-security-administration-with-vpc-prefix-lists/
Answer: Another easy option is you maintain a csv file to store IP addresses and use a python script to update your security group. Since working with Excel/csv files are more popular you can easily find python scripts to read csv files and the use boto3 to update your security group.
Answer: Maintaining the whitelisted IPs in a Security group, which is the best option I believe, you don't need for both inbound and outbound.
Since Security group is stateful, you need to mention whitelisted IPs in inbound rule only.
(You may have different use case for outbound, which I am not sure)
However, I think the best way to handle it via an automation. You can create a Dynamodb table with different rule entries, enable dynamodb stream, any change triggers a Lambda which inturn creates/amends security group associated with the EC2.
Answer: I know it's pretty late but you can allow all IPs on your security group and inside your ec2 machine if it's Linux based then set iptables to explicitly allow those ip addresses and block anything else.
<code>iptables -A INPUT -s ip1,ip2,ip3 -j ACCEPT
iptables -A INPUT -s 0.0.0.0/0 -d ip2 -j DROP
</code>


How to use variables derived from factor analysis as predictors in logistic regression?

Question: Context
I have a survey that asks 11 questions about self-efficacy.
Each question has 3 response options (disagree, agree, strongly agree).
Nine questions ask about self-esteem.
I have used a factor analysis of the 11 self-efficacy items and extracted two factors.
$x_1$ to $x_{11}$ denote the 11 self-efficacy questions in the survey, and $f_1$ ($x_1$ to $x_6$) , $f_2$ ($x_7$ to $x_{11}$) denote the two factors I got from the factor analysis.
$y$ is a Dependent variable.
Then I created two new variables:
<code>   f1=mean(x1 to x6);
   f2=mean(x7-x11).
</code>
So the logistic regression would looks like this:
<code>   y=a+bf1+cf2+....
</code>
My question:

Can i use these two factors as predictor variables in my multivariate logistic regression model?
Should I calculate the mean of each items in each factor and use this mean as a continuous variable in my logistic regression model?
Is this an appropriate use of factor analysis?
Comment: A few questions: Is $y$ univariate? Then you have a multiple logistic regression, not a multivariate logistic regression. How are you doing the factor analysis? While there are methods applicable to ordinal variables that are analogous to factor analysis for continuous manifest variables, it isn't clear to me what you're using. Finally, what would you consider to be an inappropriate use of factor analysis? If you look on it as a dimension reduction technique (turning 11 variables into 2) then there's nothing inherently wrong with it. Whether it's a good idea or not is another question.
Answer: If I understand you correctly, you are using FA to extract two subscales from your 11-item questionnaire. They are supposed to reflect some specific dimensions of self-efficacy (for example, self-regulatory vs. self-assertive efficacy).
Then, you are free to use individual mean (or sum) scores computed on the two subscales as predictors in a regression model. In others words, instead of considering 11 item scores, you are now working with 2 subscores, computed as described above for each individual. The only assumption that is made is that those scores reflect one's location on an "hypothetical construct" or latent variable, defined as a continuous scale.
As @JMS said, there are other issues that you might further clarify, especially which kind of FA was done. A subtle issue is that measurement error will not be accounted for by a standard regression approach. An alternative is to use Structural Equation Models or any latent variables model (e.g. those coming from the IRT literature), but here the regression approach should provide a good approximation. The analysis of ordinal variables (Likert-type item) has been discussed elsewhere on this site.
However, in current practice, your approach is what is commonly found when validating a questionnaire or constructing scoring rules: We use weighted or unweighted combination of item scores (hence, they are treated as numeric variables) to report individual location on the latent trait(s) under consideration.
Answer: Using factor scores as predictors
Yes, you can use variables derived from a factor analysis as predictors in subsequent analyses.
Other options include running some form of structural equation model where you posit a latent variable with the items or bundles of items as observed variables.
Mean as scale score
Yes, in your case, the mean would be a typical option for computing a scale score.
If you have any reversed items, you have to deal with this.
You could also use factor saved scores instead of taking the mean. Although when all items load reasonably well on each factor and all items are on the same scale and all items are positively worded, there is rarely much difference between the mean and factor saved scores.
You could also look at methods that acknowledge the ordinal nature of the scale and therefore do not treat the scale options as equally distant.
Comment: (+1) Good that you mentioned the use of Factor scores directly (and their correspondence with raw scores under certain conditions).
Answer: Everything have be said by chl and Jeromy for the theorical part... If you don't have use sum/mean of variables you identify with FA you can use scores of FA.
Regarding the syntax you use you're probably using SAS. So to do a correct use of factor analysis you must use the score of observations and not the mean of variables.
You find below the code to obtain score for 2 factors with an FA. Scores you'll have to use will be call Factor1, Factor2, ... by SAS.
This is a 2 steps... 1) First FA then 2) call the proc score to compute Scores.
<code>proc factor
    data = Data
    method = ml
    rotate = promax
    outstat = FAstats
    n=3
    heywood residuals msa score
    ;
    var x:;
run;

proc score data=Data score=FAstats out=MyScores;
    var x:;
run;
</code>
The variables to use are Factor1, Factor2, ... in MyScores datasets.
Answer: Continuous latent variables with discrete (polytomous in your case) manifest variables is part of item response analysis. Package 'ltm' in R covers a variety of such models. I refer you to this paper, which deals with exactly same problem.
Comment: (+1) I have (re)read your paper, which looks pretty interesting, although I found amazing the use of a Rasch model in clusters of genes. Did you compare your results with a sparse PLS-DA approach?
Comment: @chl Not yet; working on that.


How to redirect from node view to node edit page in a view?

Question: I need on of the views in my Drupal design, which is using node title linked to the node content view, just redirect form node content view page to that node edit page.
How would it be possible? If it needs coding please write the code. Thanks
(My Drupal version is 7.x)
Answer: Steps to achieve:
Add new field: 'nid'

Add nid field to view above the title field.
Exclude nid from display.
Goto arrangement and put it above the title field, so that it can fetch it.

Change to existing field: 'title'

Open title field settings.
Uncheck Link this field to the original piece of content.
Expand REWRITE RESULTS section.
Check Output this field as a link checkbox.
Put node/[nid]/edit to the Link path input.


Tenho definição de classe mas o objeto vindo do JSON não mantém os métodos

Question: Tenho um problema no momento que recebo o JSON do servidor e quero usar ele como se fosse minha classe local.
<code>//classes

export class Pessoa {
 id: number;
 nome: string;
 raca: Raca;

 constructor(){}

 getTeste1(): string {
   return this.id + ' - ' + this.nome;
 }

}

export class Raca {
 id: number;
 nome: string;

 constructor(){}

 getTeste2(): string {
   return this.id + ' - ' + this.nome;
 }
}

</code>
<code>//JSON recebido
{
   "id": 20354,
   "nome": "CRISOSTOMO KOLLING",
   "raca":    {
      "nome": "BRANCA",
      "id": 2
   }
}
</code>
<code>//recebimento do servidor
let pessoa: Pessoa;

buscar(nome: string){
 this.rest.get(nome).subscribe((v: Pessoa) => {Object.assign(this.pessoa, v)})
}
</code>
Eu esperava poder usar this.pessoa.raca.getTeste2() assim como uso no java.
Mas não consigo.
O this.pessoa é do tipo PESSOA, mas o this.pessoa.raca NÃO é do tipo RACA, mas sim Object.
Como faço para que o retorno do JSON preencha a tipagem que eu determinei com as classes?
Ou eu não posso criar métodos dentro dos arquivos onde crio a tipagem da classe?
Comment: Tenta fazer com o new e passando o parametros para o construtor. tipo this.pessoa= new Pessoa(v)
Answer: Tente desta forma:
<code>export class Pessoa {
  id: number;
  nome: string;
  raca: Raca;

  constructor(data?){
    this.id = data.id ? data.id : null;
    this.nome = data.nome ? data.nome : null;
    this.raca = data.raca ? new Raca(data.raca) : null;
  }

  getTeste1(): string {
    return this.id + ' - ' + this.nome;
  }

}

export class Raca {
  id: number;
  nome: string;

  constructor(data?){
    this.id = data.id ? data.id : null;
    this.nome = data.nome ? data.nome : null;
  }

  getTeste2(): string {
    return this.id + ' - ' + this.nome;
  }
}

buscar(nome: string){
  this.rest.get(nome).subscribe( result => {
    if(result){
      this.pessoa = new Pessoa(result)
    }
  }
}
</code>
Comment: Funcionou !! Apenas tive que adaptar. No construtor eu tive que colocar um ```If ``` antes das atribuições dentro do construtor, pois o angular não acha os atributos. E tive que inicializar os atributos com null em sua própria definição: ```export class Pessoa {
  id: number = null;
  nome: string = null;
  raca: Raca = null;

  constructor(data?){
      if (data){
        this.id = data.id ? data.id : null;
        this.nome = data.nome ? data.nome : null;
        this.raca = data.raca ? new Raca(data.raca) : null;
      }
  }
...```


Using Loop multiple times

Question: 1. Briefly
I don't understand, how I can use cycles from Loop multiple times without ending script.

2. Settings
I have a file SashaAutoScrolling.ahk, it content:
; First loop, Speed 1
#+1::
Loop
{
    Send {WheelDown}
    Sleep 3000
}

; Second loop, Speed 2
#+2::
Loop
{
    Send {WheelDown}
    Sleep 600
}

; Third loop, Speed 3
#+3::
Loop
{
    Send {WheelDown}
    Sleep 100
}

; Fourth loop, Speed Up
#+0::
Loop
{
    Send {WheelUp}
    Sleep 600
}

; Loop pause
; http://autohotkey.com/board/topic/95308-endless-loop-with-hotkey-pause/?p=600526
#p::Pause

; Exit script
#esc::ExitApp

3. Steps to reproduce
I open any pdf file in any PDF-viewer. I switch between «speeds»:

Shift+Super+3,
Shift+Super+0,
Shift+Super+2,
Shift+Super+3,
Shift+Super+0.

4. Actual behavior

If I run Shift+Super+3 and Shift+Super+0 first time
    I can successful switch between «speeds».
If I run Shift+Super+3 and Shift+Super+0 second and next times,
    I can't switch between «speeds».

5. Expected behavior
Successful switch between «speeds» unlimited number of times.

6. Not helped

Googling, searching in Stack Overflow and AutoHotkey forum.

7. Do not offer

Please, do not offer use third-party programs. Adobe Reader free version works as expected, but I can't read, use this program , for example, djvu or doc books.
Please, do not offer use built-in mouse auto scrolling. It uncomfortable, because is problematic quick choose exact comfortable «speed» for reading.
Comment: The reason your script only works once is probably because when you press ESC, `ExitApp` exits AutoHotKey
Answer: Scrolls current window by simulating mouse wheel scroll when Shift+Win+1, Shift+Win+2, etc. pressed.  esc exits loop
#+1:: scrollit(3000)
#+2:: scrollit(600)
#+3:: scrollit(300)
Esc:: abort()

scrollit(delay)
{
  global abort := false
  while (!abort)
  {
    Send {WheelDown}
    Sleep delay
  }
}

abort()
{
  global abort := true
}

Comment: Thanks for the answer! Is it possible switch between «speeds» without pressing `Esc` each time, [**as in Adobe Reader**](https://www.youtube.com/watch?v=kpfp4-kinn4)?
Comment: I added another answer that makes scrollit reentrant
Answer: This code scrolls current window with simulated mouse wheel scrolls
Shift+Win+1, Shift+Win+2, etc... starts scrolling. If already scrolling, just updates sleep interval. esc exits
; globals:
;   g_running : true while loop is active
;   g_sleep   : milliseconds to sleep between sending input event
;   g_key     : key to simulate

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; HOTKEY DEFINITIONS

#MaxThreadsPerHotkey 2

#+1:: scrollit(3000)
#+2:: scrollit(600)
#+3:: scrollit(300)
#+0:: scrollit(600,"WheelUp")

#MaxThreadsPerHotkey 1

Esc:: abort()

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; HELPER FUNCTIONS

; send wheeldown every _delay_ microseconds
; if called while loop is already active, just update delay and exit
;
scrollit(sleep, key="WheelDown")
{
  global g_running
  global g_sleep := sleep
  global g_key   := key

  if g_running
    return

  g_running := true

  while (g_running)
  {
    Send {%g_key%}
    Sleep g_sleep
  }
}

abort()
{
  global g_running := false
}

Comment: But if user wants `{WheelUp}`, he still need press to `Esc`. Thanks.
Comment: okay, I parameterized the input event instead of assuming WheelDown all the time (which is now just a default)
Comment: Can't decide if I should upvote your answer for the creative and working solution or downvote because it will block the UI thread(or event loop) in any other language(maybe not all but definitely most). It's probably fine for autohotkey but people who will see it probably won't understand why it works and might get some wrong ideas about how a problem like this one should be solved(with a timer). If you see things differently please tell me.
Comment: I don't see my answer as creative or unique.  It's structured similarly to the [message loop](https://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows) used by every windows program with a graphical interface.  The `sleep` statement yields control just as `GetMessage()` does.  Feel free to post your "right" way as an alternate solution.
Comment: It's not, your code is structured similarly to calling `sleep` after `GetMessage()` and freezing. `sleep` doesn't yield control of the current thread, that statement doesn't even make any sense. Your code works only because of the unique threading model of autohotkey. I posted an alternative solution.
Comment: I meant `sleep` yields control of the cpu allowing for any other related processing to complete. I did not mean to imply anything about how threads are processed. Script authors shouldn't assume that autohotkey and the scripts it launches are all a single thread. When I program in autohotkey, I think of launching a hotkey as similar to launching a shell script; the script can use any reasonable program structure without worrying about blocking operating system threads.
Answer: Your code doesn't work because by default there is only one thread per hotkey. When you do:
Loop
{
    Send {WheelDown}
    Sleep 100
}

You never return from this one thread so when you call it second time the hotkey will not fire and your program will continue looping in the current thread.
Jim U solved it by calling #MaxThreadsPerHotkey 2, this wouldn't have made your code work though because you don't return from your hotkey threads at all, Jim U doesn't return from the first thread only so 2 threads is enough for his solutoin to work.
An alternative solution is to use timers, the following will change the scrolling speed immediately, doesn't need any special directives and doesn't rely on Autohotkeys unique threading model.
#+1::callSetTimer(3000)
#+2::callSetTimer(600)
#+3::callSetTimer(100)
#+0::callSetTimer(600, "WheelUp")
Esc::SetTimer, scrollTimer, Off

callSetTimer(interval, key := "WheelDown") {
    global currKey := key
    SetTimer, scrollTimer, %interval%

    scrollTimer:
        send, {%currKey%}
    return
}

Using Loop multiple times


The line number on the SQL Editor in DBeaver

Question: How to toggle the line number on the SQL Editor in DBeaver?
I can't find this option in Window / Preferences / General / Editors / SQL Editor.
Is it possible to do this?
Answer: It is under Window / Preferences / Editors / Text Editors:

In older versions it was under Window / Preferences / General / Editors / Text Editors.
But as @simhumileco pointed out, there is a shortcut in the context menu when you right-click the left gutter of the editor.
Answer: I found it:
Right click on left column of SQL Editor and click Show / Hide Line Numbers from context menu.
Answer: Window > Preferences > General > Editors > Text Editors
Then, select the checkbox "Show Line Numbers"
Answer: In version 220.127.116.11105021514 it looks like this:
Windows > Preferences > Editors > Text Editors > Show line numbers

Comment: please improve your answer with an explanation
Answer: You can follow the below steps:
Windows > Preferences > General > Editors > Text Editors > Show line numbers

Comment: Downvote, this repeats the accepted answer.
Answer: In my case, none of this answers work for me, so I decide to expose my answer.
Environment
DBeaver: Version 184.108.40.206309041200
Mac M1 Pro

Steps
Select the Script \
Right click on the UI very close to the beginning of the line \
Left clik on "Show Line Numbers" button

Before solution

After solution


Express a number in a special format in Android

Question: I have a question, I am doing an App that is a kind of bill however I don't know how to express in a textview for example:
1120 as $1.120 or something like this.
Thanks in advance
Answer: numberformat is your solution :
    Test code :
int = 1236;
NumberFormat nf = NumberFormat.getInstance(Locale.US);
Log.i("test", nf.format(nb));

show output :
1,236

Answer: There are a number of solutions to format numbers in Android which of course depend on the nature of your app and your requirements.
Because you are using a money value in your example, I would look into Big Decimal at has numerous precision and rounding methods.
Here's a little example for you, note that it is not localised!
BigDecimal theAmount = new BigDecimal(int);

DecimalFormat decimalFormat = new DecimalFormat("$#,##0.00;-$#,##0.00");
// ^ This is set manually, you could use a localised format to have Android set the
// values based on locale like this: DecimalFormat.getInstance();

decimalFormat.setDecimalSeparatorAlwaysShown(true);
decimalFormat.setMinimumFractionDigits(3);
decimalFormat.setMaximumFractionDigits(3);
// ^ There are a lot of methods to check out here!

TextView yourTextView.setText(decimalFormat.format(theAmount.divide(new BigDecimal(1000))));

Good luck!


Constructing a cyclic and tangential quadrilateral given the radii of the two circles and a side

Question: I want to construct a quadrilateral inscribed in a circle of known radius $R$ (cyclic) and at the same time I want this quadrilateral to be tangential to another circle of a known radius $r$.  In addition to $R$ and $r$, I know one of the side lengths.
I have attempted solving this problem numerically by creating the circumcircle of the quadrilateral and placing the given side as a chord.  Then I placed the inner circle (to which the quadrilateral is tangential) tangent to the chord, and using the relative position of the inner circle along the given side, and Newton's iteration, I was able to find the quadrilateral.
Question: Is there a method that does not require trial-and-error to construct such a quadrilateral?
The following image shows an example

where $R = 12 $ , $ r= 7 $ and $ s = 12 \sqrt{2} $
Comment: What about the position of circle with radius $r$. Do we know where it lies wrt the bigger circle ?
Comment: No.  That's what we want to find out.
Answer: Let $c_R,c_r$ be the circles with radii $R,r$ respectively, $O_R,O_r$ the respective centers, and $d$ be the distance between $C_R$ and $C_r$.
Then, by a formula dating back to Fuss(1798),
$$
\dfrac{1}{(R-d)^2}+\dfrac{1}{(R+d)^2}=\dfrac{1}{r^2}.
$$
Given $R,r$ we can solve for $d$:
$$
d=\sqrt{r^2 + R^2- \sqrt{r^2(r^2 + 4R^2)}}.
$$
So your construction can start, as you've done, with circumcircle $c_R$ and chord $s$ of the given length.  Construct line $\ell$ parallel to $s$ at a distance $r$ from $s$.  Construct circle $\delta$ with radius $d$ and center $C_R$.  Let $p$ be one of the points of $\ell\cap\delta$.  Then $c_r$ is the circle with center $O_r=p$ and radius $r$.  You can now construct the other three sides of the quadrilateral.