Usando a função explode() no Infomoney

Question: Estou tentando colocar alguns indicadores econômicos no site e arranjei um script que traz a cotação do dolar à partir do site do Infomoney.
Pore´m estou querendo outros índices, mas o resultado do campo é composto e separado por espaços. O código ficou assim:
if(!$fp=fopen("https://www.infomoney.com.br/mercados/acoes-e-indices" , "r" ))
{
    echo "Erro ao abrir a página de indices" ;
    exit;
}
$conteudo = '';
while(!feof($fp))
{
    $conteudo .= fgets($fp,1024);
}
fclose($fp);

$valorCompraHTML    = explode('class="numbers">', $conteudo);
// Esta é a variável que eu preciso explodir
$campo5             = trim(strip_tags($valorCompraHTML[5]));

//Estes são os valores HTML para exibir no site.
$ibovespa = trim(strip_tags($valorCompraHTML[5]));
$ibovespa = explode(' ', $ibovespa);
$bovespa  = trim($ibovespa[0]) ;

O campo sem explodir está assim: 74.294 +0,99 e no resultado desta variável está vindo apenas 74.294.
Eu precisava saber como faz para retornar o segundo valor, o +0,99
Comment: Ué, ao invés de 0 coloca 1.
Comment: Tentei fazer isso mas retorna campo vazio...
Comment: Se alguma resposta lhe for útil não deixe de marcá-la como aceita, veja aqui o porque https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079
Answer: Utilizar explode para tratar e encontrar HTML, sendo que o PHP tem recursos nativos para isso... Por mim é uma enorme gambiarra.
Você pode utilizar o XPath (ou outro recurso), para localizar tal elemento diretamente. O número está dentro de um td com a classe positivo ou negativo, além disso esta tabela está dentro de uma div com id de pnlContentA.
Portanto bastaria fazer:
//div[contains(@id, "pnlContentA")]//td[contains(@class,"numbers") and (contains(@class, "positivo") or contains(@class, "negativo"))]

No caso do PHP:
$Numbers = $XPath->query('
//div[contains(@id, "pnlContentA")]
//td[contains(@class,"numbers") and (contains(@class, "positivo") or contains(@class, "negativo"))]
');

Você pode testar o XPath acima no próprio Chrome, no console em "Elementos" e depois um "Ctrl + F" e colando o XPath acima.

No final teria algo deste tipo:
libxml_use_internal_errors(true);

$ch = curl_init('http://www.infomoney.com.br/mercados/acoes-e-indices');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FAILONERROR => 1,
    CURLOPT_PROTOCOLS => CURLPROTO_HTTP,
]);

$conteudo = curl_exec($ch);

if($conteudo !== false) {
    $DOM = new DOMDocument();
    $DOM->loadHTML($conteudo);

    $XPath = new DOMXPath($DOM);

    $Numbers = $XPath->query('
    //div[contains(@id, "pnlContentA")]
    //td[contains(@class,"numbers") and (contains(@class, "positivo") or contains(@class, "negativo"))]
    ');

    foreach ($Numbers as $number){
        echo htmlentities($number->nodeValue, ENT_QUOTES, 'utf-8');
        echo '
';
    }

}

O cURL fará a requisição e retornará o corpo da página, ou falhará imediatamente. Então, criamos o DOM e importamos o HTML, usando o $DOM->loadHTML($conteudo).
Agora a parte principal, localizamos a div que contenha um id de pnlContentA, depois localizamos um td dentro dele que tenha a classe de Numbers e também que tenha a classe de positivo ou negativo.
Isso dá como resultado:
+0,99
+1,05
+1,00
+1,67
+1,20
+1,47
+1,34
+2,12
+0,80
+1,01
+1,43
+0,97
+1,93

Este site é inseguro, não possui HTTPS, a conexão é redirecionada sempre para HTTP. Devido a isto nem fiz questão de utilizar qualquer recurso do cURL.
Comment: +1 ... PS: não é questão só de gambiarra, é questão de manutenção ou melhor dizendo, pra que usar explode se existem funções com este propósito. :D
Comment: é verdade, mas daí a pergunta deveria ser outra "Qual a melhor forma ...."
Answer: Se o campo está vindo 74.294 +0,99 e você criou um Array com explode():
$ibovespa = explode(' ', $ibovespa);

Os elementos dessa Array serão:
ibovespa[0] = "74.294"
ibovespa[1] = "+0,99"

Logo, para pegar o segundo valor (segundo elemento da Array), basta usar o código:
$bovespa2  = trim($ibovespa[1]);

Comment: Foi o que eu pensei. Estou usando assim:                          $valorVendaHTML  = explode('+', strip_tags($valorCompraHTML[5]));
//Estes são os valores HTML para exibir no site.
$ibovespa = trim(strip_tags($valorCompraHTML[5]));
$ibovespa = explode(' ', $ibovespa);
$bovespa  = trim($ibovespa[1]) ;       Mas está retornando campo vazio
Comment: Aqui tá vindo o valor correto. Dá uma olhada neste Ideone: https://ideone.com/S56u4K
Answer: Existem várias formas de se obter o desejado, mas vou me ater a respostas que vão ao encontro da dificuldade postulada na pergunta.
O valor é pego pelo elemento de class="numbers positivo"
PHP
if(!$fp=fopen("https://www.infomoney.com.br/mercados/acoes-e-indices" , "r" ))
{
    echo "Erro ao abrir a página de indices" ;
    exit;
}
$conteudo = '';
while(!feof($fp))
{
    $conteudo .= fgets($fp,1024);
}
fclose($fp);

$valorCompraHTML    = explode('class="numbers">', $conteudo);
$ibovespa = trim(strip_tags($valorCompraHTML[5]));
$ibovespa = explode(' ', $ibovespa);
$bovespa  = trim($ibovespa[0]);

$valorMais    = explode('class="numbers positivo">', $conteudo);
$mais = trim(strip_tags($valorMais[1]));
$mais = explode(' ', $mais);
$maisResult  = trim($mais[0]);

HTML
R$  <?php echo $bovespa  ?>  <?php echo $maisResult ?>

Outra forma de obter o mesmo resultado
Conforme dito na pergunta do autor O campo sem explodir está assim: 74.294 +0,99 não corresponde a realidade, na verdade o campo retorna assim,

portanto devemos retirar todos os espaços extras antes do explode para ficar assim 74.294 +0,99
if(!$fp=fopen("https://www.infomoney.com.br/mercados/acoes-e-indices" , "r" ))
{
    echo "Erro ao abrir a página de indices" ;
    exit;
}
$conteudo = '';
while(!feof($fp))
{
    $conteudo .= fgets($fp,1024);
}
fclose($fp);

$valorCompraHTML    = explode('class="numbers">', $conteudo);
// Esta é a variável que eu preciso explodir
$campo5             = trim(strip_tags($valorCompraHTML[5]));

//Estes são os valores HTML para exibir no site.
$ibovespa = trim(strip_tags($valorCompraHTML[5]));

//retira todos os espaços em branco e retorna 74.294 +0,99
$ibovespa = preg_replace(array("/\t/", "/\s{2,}/", "/\n/", "/\r/"), array("", " ", " ", " "), $ibovespa);

$ibovespa = explode(' ', $ibovespa);

$bovespa  = trim($ibovespa[0]);

$mais  = trim($ibovespa[1]);

script para Dolar, euro etc...

O explode() foi criado com a intenção de separar uma string em um array de várias strings menores. Para isto ele utiliza determinados caracteres, passados por parâmetros, para fazer a separação.
Na programação, a gambiarra é uma maneira paliativa (e criativa) de resolver um problema ou corrigir um sistema de forma ineficiente, deselegante ou incompreensível, mas que mesmo assim funciona.
Este é um código que imprime na tela a repetição da frase "Hello world!" 5 vezes utilizando-se para isto de um laço para controlar
#include <stdio.h>
int main (void) {
    int i;
    for (i=0; i<5; i++) {
        printf("Hello world!");
    }
    return 0;
}

Este é um código que imprime na tela a repetição da frase "Hello world" 5 vezes, porém utilizando-se de gambiarra para obter o resultado final:
#include <stdio.h>
int main (void) {
    printf("Hello world!");
    printf("Hello world!");
    printf("Hello world!");
    printf("Hello world!");
    printf("Hello world!");

    return 0;
}

Comment: Sensacional, agora deu certo. Eu nunca iria descobrir isso sozinho. Obrigado irmão!

Usando a função explode() no Infomoney


Como publicar no JitPack artefatos Maven com "flavour" definidas por perfil Maven?

Question: Eu tenho uma série de dependências que ora preciso do seu "sabor" normal, hora preciso de seu "sabor" sem lambdas.
Eu resolvo isso tendo o perfil retrolambda habilitado ou não. Um dos meus projetos que preciso disso é totalcross-functional-toolbox. Atualmente, o que eu tenho é um build no gitlab-CI para publicar num repositório de artefatos privado, porém estou pensando em mudar para o JitPack. O que faço atualmente está no .gitlab-ci.yml do projeto:
archive-functional-toolbox:
  script:
    - ./mvnw $MAVEN_CLI_OPTS clean deploy -pl :totalcross-functional-toolbox,:totalcross-functional-toolbox-bom,:gwt-functional-toolbox-is-serializable -am
    - ./mvnw $MAVEN_CLI_OPTS clean deploy -pl :totalcross-functional-toolbox,:totalcross-functional-toolbox-bom -P retrolambda

Note como, para publicar os dois sabores, eu mando executar clean deploy habilitando ou não o perfil retrolambda. Essa diferença de sabores eu coloca na versão. A priori, a versão é 2.0.1${revision}, se eu não colocar o perfil retrolambda acontece que ${revision} expande para string vazia, já com o perfil expande para +retrolambda.
No caso, como o JitPack controla o ciclo de vida, tem como eu detectar qual o "flavor" que está sendo requisitado para fazer o build adequado? Eu vou requerer ora o artefato com.gilab.geosales-open-source:totalcross-functional-toolbox:2.0.1, ora o artefato com.gilab.geosales-open-source:totalcross-functional-toolbox:2.0.1+retrolambda, como saber qual artefato estou requerendo para gerar corretamente a versão desejada?

Por hora, ignorar o fato de que o projeto de exemplo é um projeto Maven multi-módulo, a resposta pode considerar apenas como se fosse um projeto Maven simples
Answer: O JitPack permite que você coloque um script de build personalizado. Nesse script, ele garante algumas variáveis. No caso, a variável garantida VERSION traz a versão desejada a ser gerada. Por exemplo, se em algum lugar tiver a dependência com.gitlab.geosales-open-source:totalcross-resultset-extractor:1.0.1-retrolambda, JitPack irá passar a variável VERSION=1.0.1-retrolambda para o script.
Então, para o caso de a versão passar algum esquema de "sabor" para o artefato gerado, é preciso fazer algum processamento na variável VERSION passada. Por exemplo:
./mvnw `[[ "$VERSION" == *-retrolambda ]] && echo "-P retrolambda"` clean install

Aqui se verifica se a versão satisfaz o glob *-retrolambda; se satisfizer, irá realizar a substituição textual para -P retrolambda, caso contrário será substituído pela string vazia. Desse modo, é possível fazer a detecção de "sabor" desejado para o build e usar um perfil Maven associado a ele.
Vale ressaltar que, nos testes, usar + como parte da string de versão não deu muito certo, tanto não reconhecendo corretamente a string de versão como também dizendo que é "versão inválida".
O arquivo de build do JitPack é identificado por jitpack.yml na raiz do diretório. Mais informações na documentação oficial.


conditional removal of a field in a view

Question: I'm displaying the latest 5 nodes (teaser view mode) in a sidebar region using views. The nodes have an image field but I only need to render that on the first node on the list.
So, since the teaser view mode does include the image, I'm trying to remove the image from all nodes apart the first upon rendering.
I've tried the following in my theme's template.php:
function foo_preprocess_views_view(&$vars) {
  if (isset($vars['view']) &&
      isset($vars['view']->style_plugin) &&
      isset($vars['view']->style_plugin->row_plugin->nodes)) {
    $first = true;
    foreach($vars['view']->style_plugin->row_plugin->nodes as $node) {
      if (!$first) {
        unset($node->field_image);
      }
      $first = false;
    }
  }
}

But the image is still being rendered. I think that this has to do with the fact that I'm trying to temporarily remove the image from the wrong datastructure which represents the node state.
I then tried using the hook_preprocess_node with something like:
unset($vars['field_image']);

But it still has no effect. The image was still there.
How is this solved? And at which layer is it solved best?
I know I could simply hide via CSS by using the .views-row-first class but I don't think this is optimal.
Answer: Take a look at Views Nodes Split. Module Monday: Views Nodes Split gives more information about how to use the module.

Setting up the module is simple: just turn it on, and build your View as you normally would. Then, click on the View's Format option and select the newly available "Splited Content" row style. While the grammar is awkward, the results are great: you can specify separate build modes and display styles for the first node and all subsequent nodes from a simple config screen. In fact, you can even adjust where the cutoff happens: you might want the first three nodes to display in full, for example. No other changes are needed for your View; this module's new row style handles the rest.
Comment: @kiamlaluno I'm not a member of Lullabot. Its rude to just nab images from a site without some form of attribution. That's why I merely linked the blog post to begin with -- let a needy programmer feed their thirst of knowledge from the source I found the answer at. The whole blogpost is more meaningful than your hack-up copypaste of their content. I did link the project page url so drupal.org gets some direct traffic ......
Comment: try views_pre_render() and unset the field there if you dont want to show it. THen in the view configuration set the field to "hide if empty". That should do it ....

@kiamlaluno -- ok thank you for the clarification. In the future I'll try to make my replies longer than a single link (eg include a summary).
Comment: Answers that are just a link are not considered answers, on Stack Exchange. If you link an external page, you need to provide a summary of what said in the linked page, or an excerpt.
Comment: Thanks. I'd like to find a programmatic solution which will only remove a single field.
Comment: @tenken: Thanks, using hook_views_pre_render worked. Feel free to post it as a new answer so I can accept
Answer: try hook_views_pre_render() and unset the field there if you dont want to show it. THen in the view configuration set the field to "hide if empty".


How do to get the value of a query with input hidden?

Question: I have a form which I'm submitting,
<cfset cnt=0>
<form method="post" action="add_approver.cfm" name="select_employee">
    <select name="selectDept">
        <option selected>Select  Department</option>
        <cfloop query="department">
           <cfset cnt= cnt+1>
           <cfoutput>
               <option value="#dept_name#">#csedept_name#</option>
           </cfoutput>
           <input type="hidden"  name="id#cnt#" value="#dept_id#">
        </cfloop>
    </select>

<input type="submit" name="Submit" value="Submit">

</form>

When using dump=form. I'm not getting the value of the id. I get:
id=#dept_id#,#dept_id#...

The other problem is that even thought I'm using type="hidden" the dropdown doesn't show
all the options. It is actually showing in text. It is only showing the first item as a list option.
Answer: You need to put a cfoutput tag around your hidden input tag. Also the hidden input tags are inside of your select tag which is most likely breaking the select tag.
You should loop though the inputs separate from the drop down
Comment: if you would just like to submit the id of the department put that as the value of the select option. it will come through as #form.selectDept# when you submit the form
Comment: Can you elaborate on why you need the "name", instead of just the id? BTW, there is no need to create a counter variable. Since you are using a query loop, just use `currentRow`. Also, instead of using cfloop plus cfoutput, just switch to a cfoutput loop.
Comment: that would give me the id:1,2,3,4,5,6..., not for the one i selected
Comment: so is your intent to get the name and the id of the selected drop down item?
Comment: i didnt need the name , so i change it to getting id , and got it to work


How many possible combinations in 9 character?

Question: I have a question because i cant solve it myself i dont realy know how to calculate it exactly, i know it's gonna be easy for you in this section..
So i have 2 questions,
I want my computer to generate all possibility of 9 character, with: only uppercase alphabetic, and number
so exemple:
A5S8E5D8E5
How many possibility is possible ? , no lower case, only uppercase and number.
And if its possible to know, how many time it would take to guess all possibility ?
I know it depend of the configuration of my computer but well let's say a 1080 GTX.
Thank for your help guys !
Comment: Why the tags "complex-analysis" and "statistics" ?
Comment: @TheSilverDoe sorry it's clearly the wrong tags, but i wasn't able to put any tags due to my reputation
Comment: Are you familiar with Pascal's Triangle.  See https://en.wikipedia.org/wiki/Pascal%27s_triangle.
Answer: So you have 26 possibilities for upper-case letters and 10 possibilities for digits meaning you have 36 possibilities for each character. In total you obtain that number:
$$
36^9 \simeq 10^{14}
$$
To see how long it would take for a GPU to crack it, we would need to test the hash of a password and compare it to the hash stored in the database. So it depends on the algorithm you use to produce the hash and check in the database. To get an idea, from this website (for MD5 encryption), we get that a GTX 1080 processes $4 \times 10^{10}$ hashes per second. So you get a rough estimate of $2500s$ which is less than an hour.
Note that it blows up if you use all possibilities from your keyboard.You get 10 digits + 26 lowercase + 26 uppercase + 31 special characters which is 93. Then the numbers of possibilities gets to $10^{17}$ and it takes roughly 1 month non-stop to exhaust all possibilities.
Comment: Right. But then if you don't have access to it there is not much you can do.
Comment: Oh great.. so what i understand is, with 26 uppercase+10 number possibility, it would take 41 minutes for a 1080 GTX to get all possibility of 9 characters ? thank it helped me a lot !
Comment: I think so yes unless I made a mistake. It seemed surprising to me at 1st too but I think this is right. Note that depends a lot on the encryption scheme used (though MD5 is quite standard).
Comment: mmh interesting, I thought much more as you know! But it depends if you have access to 'raw' database at the end , right?
Answer: For each character, you have $26$ letters + $10$ digits = $36$ possible choices.
Therefore, the total number of combinations is $36^9$, which is a really big number.
I'm not sure about computation time though, since I'm not a hardware expert.
Comment: Alright so it would be 101 559 956 668 416 attempt right ? well yes that's a big number, thank
Comment: Are you trying to bash a password or something?
Comment: @GeneralPoxter It's not for a password, but something for a algorithm
Comment: @Tortar no I think it takes less an hour for the GPU he mentioned. For a CPU it would take more time yes. Cf my answer
Answer: This problem is called a permutation with repetition. The sequence here involves 26 letters of the English alphabet and 10 numbers from 0 to 9.
This turns out to be a simple calculation. So we can form a sequence of 9 characters by selecting one letter/number out of 36 for each character, which gives us:
$$36 \times 36 \times 36\times 36\times 36\times 36\times 36\times 36\times 36 = 36^9 = 101559956668416 \simeq 10^{14} $$
To estimate the total time required you need to know the execution time of your algorithm needed for testing a sequence of 9 characters, then you multiply by $36^9$ times.
Comment: Are you sure that upper and lower doesn't matter ?
Because if its only uppercase, there will be less possibility than upper and lower right ?
What i mean is like, lets do it for 1 character :
AaBbCc ect..
then if its only upper its:
ABC ect..

So less guessing no ?
Comment: I meant we have 26 letters in total, and the number of letters doesn't change if the letters are uppercase or lowercase. However, if you decide to combine them, you have double this number in terms of possibilities (26 + 26 for each character). I removed the confusing part in my answer.
Comment: Oh yes alright i get it now, thank man !


$G/N$ is nilpotent $\Leftrightarrow$ $N$ contains the intersection of all subgroups in the lower central series.

Question: I want to solve the following exercise:

Let $G$ be a group. $G^{0}\geq G^{1} \geq G^{2}\dots$ is a lower central series, s.t. $G^{0} = G$ and $G^{i+1} = [G,G^{i}]$. Let $N$ be a normal subgroup of $G$.
a) Then $G/N$ nilpotent $\Rightarrow$ $\bigcap\limits_{i\geq 0}G^{i} \subset N$.
b) With $G$ finite, $\bigcap\limits_{i\geq 0}G^{i} \subset N$ $\Rightarrow$ $G/N$ nilpotent.

So far I just have ideas to a):
I found a Corrolary that says: $N$ normal, $G/N$ nilpotent and $N\leq Z_{i}(G)$ for some $i$ (where it notates the upper central series), then $G$ is nilpotent.
I thought, if I could show that $N\leq Z_{i}(G)$, then with $G$ nilpotent, the lower central series terminates and the intersection would be trivial (? is that right?) and so contained in $N$.
Is that the right way? Or should I better approach that exercise differently?
Thanks and best,
Sara
Comment: A lower central series of $G$ can be transferred to a lower central series for $G/N$, by considering $G^{i}N/N$. Try to relate this lower central series with nilpotence of $G/N$.
Comment: There is a little typo : The serie starts at $G^0=G$ so it can't be increasing.
Answer: No need for any of the book's lemmas!  Instead, understand what's going on. Intuitively, what you're being asked to prove is that whenever $\bigcap_{i\geq 0} G^i$ is nontrivial (and thus $G$ is not nilpotent), you can make $G$ into a nilpotent group by quotienting out by $\bigcap_{i\geq 0} G^i$.  In fact, $\bigcap_{i\geq 0} G^i$ is the smallest such subgroup so that this quotient is nilpotent.
What do you need to do to prove this?

What is a nilpotent group?

 This one depends on what definition you're using, as there are a bunch of equivalent definitions, but I assume you're using this one: a group $K$ is nilpotent iff $K^n=1$ for some $n$, where $K^n$ (as in your notation) is the $n$th term in the lower central series.

What is $\bigcap_{i\geq 0} G^i$?

 Surely $G^{i+1}\subseteq G^i$.  (Prove this real quick.)  Thus, if $\bigcap_{i\geq 0} G^i$ is not trivial, there must be some $m$ for which $G^m= G^k$ for every $k\geq m$, and so $G^m=\bigcap_{i\geq 0} G^i$.

Determine the relationship between $G^i$ and $(G/N)^i$.

 What does it say about $G^i$ when $(G/N)^i$ is trivial?  What does it say about $(G/N)^i$ when $G^i\leqslant N$?   (If you're still stuck: can you rewrite $(G/N)^i$ in terms of $G^i$ and $N$)?

Can you connect the dots for me?
Comment: I seem to not understand *your* definition of nilpotency: what is for you $\,K^n\,$ when $\,K\,$ is a group? For me it is the subgroup generated by the $\,n-$th powers of all elements of $\,K\,$, but this would make your definition false, since for example $\,S_3^6=1\,$ but, of course, $\,S_3\,$ isn't nilpotent...
Comment: @DonAntonio I am using the notation for the lower central series as given by OP.
Comment: Oh, I see @Alexander. Thanks.
Answer:
Sub-exercise 1. Show for any subset $A$ and subgroup $B$ of $G$, $AB\le B\implies A\le B$.
Sub-exercise 2. Show that for any $L,\,M\le G$ and $N\trianglelefteq G$, we have $$\left[\frac{L}{N},\frac{M}{N}\right]=\frac{[L,M]N}{N}.$$
Sub-exercise 3. By induction and the above, show that $(G/N)^{(i)}=G^{(i)}N/N$.

As corollary, deduce part (a) by relating $(G/N)^{(i)}$, $G^{(i)}$ and $N$ together using $(G/N)^{(i)}=N/N$, which comes from the hypothesis that $G/N$ is nilpotent.

Sub-exercise 4. Argue that if $G$ is finite, then $G^{(j)}=G^{(j+1)}=\cdots$ for some $j$. Subsequently, prove that this final term $G^{(j)}$ is equal to the intersection $\bigcap_{\ell\ge0}G^{(\ell)}$.

Thus deduce part (b) as corollary by considering $(G/N)^{(j)}$ in view of $G^{(j)}=\bigcap_{\ell\ge0} G^{(\ell)}\le N$.
Comment: Thank you all a lot for the help!!!
I'll now try to "connect the dots" :)
Comment: I must admit I feel still a little confused with that, but here is my try:

First: $(G/N)^{i} = G^{i}N/N$. I just do the induction step:
$i \mapsto (i+1):$ $(G/N)^{(i+1)} = ((G/N)^{i})'$ (with $G' = [G,G]$.). $((G/N)^{i})'$ is generated by $[xN,yN] = [x,y]N$ with $x,y\in G^{i}$, $N$ normal in $G$. Since $\{[x,y]| x,y\in G^{i}\}$ is the system of generators of $G^{(i+1)}$ follows the assumption.
Comment: Now I assume $(G/N)$ is nilpotent. Then $(G/N)^{i}=e$ for some $m$, and for all $k\geq m:$ $(G/N)^{k}=(G/N)^{m}=e$.
Then with the above property: $(G/N)^{m} = G^{m}N/N = e$. This is the case, if $G^{m}\subset N$. And with $G^{k}N/N=(G/N)^{k}=(G/N)^{m}=G^{m}N/N$ follows $G^{m}=G^{k}$ for some $m$ and all $k\geq m$. Hence $\bigcap\limits_{i\geq 0}G^{i} = G^{m}\subset N$.
Is that ok like this?

Thanks a lot!


How to get value from PROMISE in angular js $http service

Question: How can get value from the Promise return by $http service
>d {$$state: Object}
 $$state: Object
 status: 1
 value: Array[1]
 __proto__: Object
 __proto__: Object

How to get the value in the d object
s.states = '';
$http.get('/api/random').then(function(response) {
  s.states = response;
 }, function(error) {

}).finally(function() {

})
console.log(s.states);

which shows the above result. $$state so how can get the value from it?
Comment: have you tried d.value ?
Comment: By calling `promise.then(function(value) { acessValueHere(value); })`, as for all promises. Read http://blog.ninja-squad.com/2015/05/28/angularjs-promises/
Answer: As suggested above:
promise.then(function(value) {
      // Value contains the data received in the response
   }, function(error) {
      // This is called when error occurs.
   }
)

The promise itself does not contain a value. It returns the value in the future ("promises" to return a value) when it's available. And when it becomes available (or not), one of two callbacks passed to then() methods gets triggered.
So basically, you should make calls like this:
$http.get('/api/random').then(function(response) {
    // Do some stuff with response.
});

You can pass a callback to finally() method callback, which will be run regardless of success or error in promise.
$http.get('/api/random').then(function(response) {
    // This one is triggered in case of successful response.
}, function(error) {
    // This code runs in case of error
}).finally(function() {
    // And this block will be triggered anyway after the promise returns.
});

Comment: You probably need to check the Network tab of your developer tools to see what you request and what you get in response.
Comment: i print the response it shows the above result $$state object
Comment: Based on the code snipped you added: "console.log(s.states);" executes BEFORE  "s.states = response"; So basically it should print empty string assigned above. The output above shows that you are trying to print the promise itself (like console.log($http.get(...)), and $$state.status equal to 1 means that the promise is already resolved. Try console.log(response) in the first callback and inspect what it logs.
Comment: i want the result outside the $http
Comment: That's how promises work. You cannot stop the code execution and wait until promise returns. However, your scope is still available in the callbacks, so you can use anything you need in combination with response.


ReactJS - Listing all keys at once

Question: I'm a beginner of ReactJS and I'm stuck trying to figure out why map only returns a single prop at a time.
In file1.jsx, I have an array that contains 3 objects:
var MainPanelOneData = [{"id":1,"shotviews":15080},{"id":2,"likes":12000},{"id":3,"comments":5100}];

File1.jsx also has a render function to extrapolate the data inside the array:
render: function() {
    var ListMainPanelOne = MainPanelOneData.map(function(data) {
        return <MainPanelOne key={data.key} shotviews={data.shotviews} likes={data.likes} comments={data.comments} />
});

In file2.jsx, I have this code to render the data object from file1.jsx:
render: function() {

    return (

      {this.props.shotviews} shot views
      {this.props.likes} likes
      {this.props.comments} comments

    )
  }

The result shows this:
15080 shot views likes comments
shot views12000 likes comments
shot views likes5100 comments

I'm guessing it repeats like this because it searches through one key at a time?  If that's the case, how do I display all keys at the same time?  Use indexing?
Comment: What are you trying to achieve? Do you want to show all data keys in a single component? i.e. `shotViews`, `likes`, `comments`?
Answer: well your array of data doesnt have all the keys. each one of your objects in PanelOneData has ONE key and is missing the other two; additionally none of them have key called key. so youre making three MainPanelOne components, each with a single prop. the result of that map is this
[
  <MainPanelOne key={null} shotviews={15080} likes={null} comments={null} />,
  <MainPanelOne key={null} shotviews={null} likes={12000} comments={null} />,
  <MainPanelOne key={null} shotviews={null} likes={null} comments={5100} />
]

which is an accurate display of what youre seeing
Comment: Hmm, I must be misunderstanding what a key is then.  Do you mean that `MainPanelOneData = [{"id":1,"shotviews":15080},{"id":2,"likes":12000},{"id":3,"comments":5100}];` the key for it is MainPanelOneData[0]?  So if I wanted to access shotviews, it would be something like this? MainPanelOneData[0][0].shotviews?  Sorry for my bad syntax, I'm also learning JS after coming from Ruby.
Answer: To get one line you might do something like this.

render: function() {
    var ListMainPanelOne = MainPanelOneData.map(function(data) {
        return  {data.shotviews} {data.likes} {data.comments}
});

ReactJS - Listing all keys at once


Gulp not working after downgrade

Question: After installing gulp#4 (the next version of gulp npm install 'gulpjs/gulp.git#4.0' --save-dev ) I was doing a couple of tests and ended up going to the last version but the console kept printing me the following output AssertionError [ERR_ASSERTION]: Task function must be specified I've tried several things like changing branch, removing the node_modules and installing a different gulp version like gulp@3 but none work
$ npm run watch

> firstname.lastname@example.com watch /Users/alevale/work/CLIENT
> gulp watch

assert.js:60
  throw new errors.AssertionError({
  ^

AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (/Users/alevale/work/CLIENT/node_modules/undertaker/lib/set-task.js:10:3)
    at Gulp.task (/Users/alevale/work/CLIENT/node_modules/undertaker/lib/task.js:13:8)
    at /Users/alevale/work/CLIENT/gulpfile.js:43:10
    at Object.<anonymous> (/Users/alevale/work/CLIENT/gulpfile.js:168:3)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! email@example.com watch: `gulp watch`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the firstname.lastname@example.com watch script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alevale/.npm/_logs/2017-07-20T12_28_19_935Z-debug.log

Answer: Wanna share my silly mistake ^^
After a couple of tries it seemed like the changes I did were saved in the package-lock.json and I forgot to delete that file as well when getting the environment in a "clean state"

Gulp not working after downgrade


How can i make this menu on iphone?

Question: I want to know How does this menu?
menu like this:
Comment: It does fine, I think ;)
Answer: UIButtons on a UIScrollView.

How can i make this menu on iphone?


context.SaveChangesAsync() doesn't work as expected

Question: I have a problem to update my database with EF.
In my application i have Orders and OrderPositions. An OrderPosition can be a FoodPosition or a ReservationPosition. So i have two lists in my Order class one for the FoodPositions and one for the ReservationPositions.
My problem is by updating an Order with its FoodPositions and ReservationPositions.
When i want to save the changes on my Database in this line:
await context.SaveChangesAsync();

This line is part of UpdateAsync method:
private async Task UpdateAsync(Order order)
{
    Order contextOrder = context.Set<Order>()
        .Include(o => o.FoodPositions)
        .Include(o => o.ReservationPositions)
        .Single(e => e.Id == order.Id);

    contextOrder.FoodPositions = order.FoodPositions;
    contextOrder.ReservationPositions = order.ReservationPositions;
    contextOrder.Status = order.Status;
    contextOrder.Cost = order.Cost;
    contextOrder.CostCenter = order.CostCenter;

    context.Update(contextOrder);
    await context.SaveChangesAsync();
}

It doesn't finish the line and goes back to where the method was called.
this is the code in my database context to bind the foreign keys:
modelBuilder.Entity<FoodPosition>()
    .HasOne<Order>()
    .WithMany(e => e.FoodPositions);

modelBuilder.Entity<ReservationPosition>()
    .HasOne<Order>()
    .WithMany(e => e.ReservationPositions);

When i try to update an Order without these two lines:
contextOrder.FoodPositions = order.FoodPositions;
contextOrder.ReservationPositions = order.ReservationPositions;

it works just fine and it goes through the line with saveChangesAsync.
I don't get any Errors, it just doesn't work the way i want it. Because it doesn't save the updated order as expected with all the updated property values and the new OrderPositions.
Maybe there are some conflicts by replacing the OrderPositions with new ones and it doesn't know how to deal with it in the database.
Comment: What exception do you have if you put your code in a try/catch bloc ?
Comment: What do you mean by "doesn't finish the line"? Whenever using async methods you should make sure each task is awaited, that a task is returned from all async methods. If you cannot return the task, due to being in a button event handler for example, you need to make sure to catch and handle any exceptions from the `await`.
Comment: This is the exception i get:

System.InvalidOperationException: 'The instance of entity type 'FoodPosition' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.' @MarcelCallo
Comment: @Flu the bug is caused by the misuse of EF Core. SaveChanges works fine. `UpdateAsync` is fundamentally broken though and suggests you're using a CRUD class over DbContext. That's a very bad anti-pattern, DbContext is a high-level Unit of Work, not a low level CRUD class. `contextOrder` is *already* tracked in the `Modified` state. When you call `context.Update` on it, you try to *attach* it and all related objects in the `Added` or `Modified` state.
Comment: Where does the `order` parameter value come from? If it was loaded from the database, you don't need `UpdateAsync` at all. If it came from an ASP.NET Core requesty, `contect.Update(order)` is enough to track it as modified. `Update` is essentially an UPSERT. It attaches an object and its relations in the `Added` state if their PKs have default values or the `Modified` state if they have actual values. Given that the query returned `contextOrder`, `ID` clearly has a non-default value
Comment: Please [edit] your question and add the exception message. And when you're there anyway, please also fix capitalization.
Answer: You have to care about navigation collections by yourself, they are not updated automatically when they are detached.
I have created universal method MergeCollections which may help you to simplify this case. Using this method you can update collection properties.
private async Task UpdateAsync(Order order)
{
    Order contextOrder = context.Set<Order>()
        .Include(o => o.FoodPositions)
        .Include(o => o.ReservationPositions)
        .Single(e => e.Id == order.Id);

    CollectionHelpers.MergeCollections(context, contextOrder.FoodPositions, order.FoodPositions, e => e.Id);
    CollectionHelpers.MergeCollections(context, contextOrder.ReservationPositions, order.ReservationPositions, e => e.Id);

    contextOrder.Status = order.Status;
    contextOrder.Cost = order.Cost;
    contextOrder.CostCenter = order.CostCenter;

    await context.SaveChangesAsync();
}

And implementation:
public class CollectionHelpers
{
    public static void MergeCollections<TItem, TKey>(
        DbContext context,
        ICollection<TItem> destination,
        ICollection<TItem> source,
        Func<TItem, TKey> keyFunc)
        where TItem: notnull
        where TKey : notnull
    {
        MergeCollections(destination, source, keyFunc, keyFunc, e => e,
            (d, s) => context.Entry(d).CurrentValues.SetValues(s),
            d => context.Remove(d)
        );
    }

    public static void MergeCollections<TItem, TKey>(
        ICollection<TItem> destination,
        ICollection<TItem> source,
        Func<TItem, TKey> keyFunc,
        Action<TItem, TItem> onUpdate,
        Action<TItem> onDelete)
        where TKey : notnull
    {
        MergeCollections(destination, source, keyFunc, keyFunc, e => e, onUpdate, onDelete);
    }

    public static void MergeCollections<TDestination, TSource, TDestinationKey>(
        ICollection<TDestination> destination,
        ICollection<TSource> source,
        Func<TDestination, TDestinationKey> destinationKeyFunc,
        Func<TSource, TDestinationKey> sourceKeyFunc,
        Func<TSource, TDestination> insertFactory,
        Action<TDestination, TSource> onUpdate,
        Action<TDestination> onDelete)
        where TDestinationKey : notnull
    {
        // shortcuts
        if (source.Count == 0)
        {
            while (destination.Count > 0)
            {
                var first = destination.First();
                destination.Remove(first);
                onDelete(first);
            }
            return;
        }

        if (destination.Count == 0)
        {
            foreach(var s in source)
                destination.Add(insertFactory(s));
            return;
        }

        var sourceDic = source.ToDictionary(sourceKeyFunc);

        foreach (var d in destination.ToList())
        {
            var key = destinationKeyFunc(d);
            if (sourceDic.TryGetValue(key, out var foundSource))
            {
                onUpdate(d, foundSource);
                sourceDic.Remove(key);
            }
            else
            {
                destination.Remove(d);
                onDelete(d);
            }
        }

        if (sourceDic.Count > 0)
        {
            foreach(var s in sourceDic.Values)
                destination.Add(insertFactory(s));
        }
    }
}

Answer: From the documentation about Change Tracking

Entity instances become tracked when they are:

Returned from a query executed against the database
Explicitly attached to the DbContext by Add, Attach, Update, or similar methods
Detected as new entities connected to existing tracked entities

So, when your initial query returns the contextOrder, contextOrder.FoodPositions is tracked, but order.FoodPositions is not tracked. Both of these objects presumably share the same Id, So when you run .Update it has two different tracked objects with the same Id, i.e. what the exception says. This has nothing to do with being asynchronous or not.
You could try adding .AsNoTracking() to ensure entities are not tracked until you do that explicitly, but note that when you do .Update(..), a search will be done on the object graph, and all found entities with existing keys will be tracked as "modified", while entities without keys (i.e. 0) will be tracked as "added".
You can also do contextOrder.Entry(myEntity).State = EntityState.Modified to set the tracking state of an single entity explicitly.
You might also need to consider the lifetime of your context object. The approach I'm most familiar with ensures that the context is short lived. I.e. you create your context, uses it for whatever you need to do, and dispose it. If your context is long lived you need pay closer attention to what entities are tracked or not, and in what state.
Answer: After you've get the entity by it's Id from the database and made changes to the entity, you can simply call context.SaveChangesAsync() method of the DbContext to propagate the changes to the database.
private async Task UpdateAsync(Order order)
{
    // ...
    contextOrder.Cost = order.Cost;
    contextOrder.CostCenter = order.CostCenter;

    await context.SaveChangesAsync();
}

Answer: Updating detached aggregate entities requires special care. In EF, references are everything, and if the DbContext is not tracking the reference you are giving it, you are going to encounter exceptions or duplicate data situations. If it's already tracking another reference for the same row, again, an exception.
Firstly, when loading a tracked reference (existingOrder) you do not use Update. Update is used to attach a detached entity and tell EF to treat the entire thing as modified. This doesn't apply to other entities referenced, so those would need to be done individually. I don't recommend using Update as it is inefficient, marking everything as changed even if nothing actually changed, and you're "trusting" the data coming in, so there are many situations (like web applications) where that is exposing your system to tampering. Instead, load the tracked entity, copy allowed values across, then call SaveChanges().
The next issue is dealing with navigation properties, your FoodPositions and ReservationPositions. With navigation properties you need to consider whether these represent associations or a more "owned" type relationship. For instance are these a many-to-many relationship where an Order associates to one or more existing Food or FoodPositions, or are FoodPositions created and treated as a one-to-many children of the order? The same basic rules apply in both cases, but they will be handled slightly differently as anything associated needs to be tracked by the DbContext before we save the Order.
The typical code structure I recommend for updating associations looks something like this: (Applying code for FoodPositions, you can apply it to ReservationPositions. This assumes a more owned one-to-many relationship)
private async Task UpdateAsync(Order order)
{
    Order contextOrder = await context.Set<Order>()
        .Include(o => o.FoodPositions)
        .Include(o => o.ReservationPositions)
        .SingleAsync(e => e.Id == order.Id);

    var existingFoodPositionIds = contextOrder.FoodPositions
        .Select(x => x.Id)
        .ToList();
    var updatedFoodPositionIds = order.FoodPositions
        .Select(x => x.Id)
        .ToList();
    var foodPositionIdsToAdd = updatedFoodPositionIds
        .Except(existingFoodPositionIds);
    var foodPositionIdsToRemove = existingFoodPositionIds
        .Except(updatedFoodPositionIds);

    if (foodPositionIdsToRemove.Any())
    {
        var foodPositionsToRemove = contextOrder.FoodPositions
            .Where(x => foodPositionIdsToRemove.Contains(x.Id))
            .ToList();
        foreach(var foodPosition in foodPositionsToRemove)
            contextOrder.FoodPositions.Remove(foodPosition);
    }
    if (foodPositionIdsToAdd.Any())
    {
        var foodPositionsToAdd = order.FoodPositions
            .Where(x => foodPositionIdsToAdd.Contains(x.Id))
            .ToList();
        foreach(var foodPosition in foodPositionsToAdd)
            contextOrder.FoodPositions.Add(foodPosition);
    }

    contextOrder.Status = order.Status;
    contextOrder.Cost = order.Cost;
    contextOrder.CostCenter = order.CostCenter;

    await context.SaveChangesAsync();
}

Essentially we compare the lists to find what items need to be added or removed, then proceed to add or remove them to the tracked order. This can involve a fair bit of code, so it's something I would likely break off into a simple method to call (i.e. updateOrderFoodPositions(order, contextOrder)) rather than growing the UpdateOrder to cover everything.


What is $\frac{\partial z}{\partial y}$ for $z = F(u, v, y), u = f(v, y)$ and $v = g(x, y)$?

Question: I'm reviewing the chain rule for partial derivatives, and since I've never actually learned it other than picking it up while learning other stuff in courses, I'm not 100% sure I'm doing it correctly when it comes to more complicated interdependencies.
So my question is what $\frac{\partial z}{\partial y}$, where $z = F(u, v, y), u = f(v, y)$ and $v = g(x, y)$ would be? My understanding is that it's:
$\frac{\partial z}{\partial y} = \frac{\partial z}{\partial u}\frac{\partial u}{\partial v}\frac{\partial v}{\partial y} + \frac{\partial z}{\partial u}\frac{\partial u}{\partial y} + \frac{\partial z}{\partial v}\frac{\partial v}{\partial y} + \frac{\partial z}{\partial y}$
Is this correct? I'm unsure about the first two terms and whether the third one would somehow be subsumed in the first one.
Comment: You might want to be more verbose with your notation. Partial derivatives act on *functions*. So instead of writing $(\partial/\partial y) z$ on both sides of the equation, try writing $(\partial/\partial y) F(u, v, y)$ versus $(\partial/\partial y) F(x, y)$. That should clear things up.
Comment: @ChristopherCulter, you're right, of course, but I did this quickly and figured it was clear enough what I was after, so I didn't want to spend too much time on writing it up nicely in TeX.
Answer: It is always been helpful for me, to think of this as a tree. Then you walk to the leaves that contain to variable of your interest.

Derivative with respect to $x$ would be:
$\frac{\partial F}{\partial x} = \frac{\partial z}{\partial u} \frac{\partial u}{\partial v} \frac{\partial v}{\partial x} + \frac{\partial z}{\partial v} \frac{\partial v}{\partial x}$
Derivative with respect to $y$ would be:
$\frac{\partial F}{\partial y} = \frac{\partial z}{\partial u} \frac{\partial u}{\partial v} \frac{\partial v}{\partial y} + \frac{\partial z}{\partial u} \frac{\partial u}{\partial y} + \frac{\partial z}{\partial v} \frac{\partial v}{\partial y} + \frac{\partial z}{\partial y}$
Read about Leibniz notation, for further information on this notation.
The trick is to take ONLY the paths to the leaves with the variables you are interested in.
Comment: @Ryker Yeap, your solution is correct. Also, you should take into consideration what C. Culter commented. I answered you with a focus on insight, but in rigour, what he says is the right thing to do.
Comment: Damn, I noticed I put the right thing in the thread title and my solution, but not the question itself (I edited it now). Basically, I was asking for $\frac{\partial z}{\partial y}$, not $\frac{\partial z}{\partial z}$. Could you perhaps verify if my solution is correct? Sorry about the confusion.
Comment: For now I've only up-voted it, because it is helpful, but it doesn't actually answer the question. I feel that's how it's done on this website, so if you change your answer to the solution for $\frac{\partial z}{\partial y}$, I'll be glad to accept it!
Comment: Oh! Of course, I'm sorry. I'll edit my answer.


Mathematical (matrix) notation for a regression model with several dummy variables

Question: I have spent a significant amount of time on this, but my knowledge in Matrix Algebra is still limited. So, I am not sure ...
I have a linear regression model based on $n$ observations. The dependent variable is $y$.
As predictors, there are 10 independent continuous variables + 9 dummy variables (based on a 10-category scale) + 5 dummy variables (based on a 6-category response). The model looks like:
$$
y = \beta_0+\beta_1 \text{C}_1+\beta_2 \text{C}_2+ ...+\beta_{11}\text{dummy}_1+\beta_{12}\text{dummy}_2+ ...+\beta_{19}\text{dummy}_9+\beta_{20}\text{dummy}_{10}+...\beta_{24}\text{dummy}_{14}+\epsilon
$$
Would this be an accurate matrix notation of the above model?
$$
\textbf{y} = \textbf{C}{\beta}+\textbf{S} \gamma + \mathbf{R}\lambda + \textbf{e}
$$
where $\textbf{C}$ is n $\times$  10+1 design matrix with  1s in the first column and 10 other columns containing the continuous predictors , $\textbf{S}$ is the n $\times$  9 matrix containing dummy variables for the 10-category scale,  $\textbf{R}$ is the n $\times$ 5 matrix containing dummy variables for the 6-category response,  ${\beta}$, ${\gamma}$ and ${\lambda}$ are the vectors of parameters, and  $\textbf{e}$ is the error vector.
Is this notation sufficiently accurate? Any references on matrix notation for dummy variables are also very much appreciated.
Sincerely,
Jacob
Comment: Welcome to CV.SE. Yes, you are fine. Good job (+1) Please see my answer below I expand on this a bit more.
Answer: Yes, you are fine. Please note that all your categorical vectors will have their first level absorbed within the intercept $\beta_0$, therefore take note of the way the encoding of dummy variables is done. I would say that if anything your notation lends itself nicely to group lasso regularisation; standard lasso solution would only select individual dummy variables instead of whole factors. (check Meier et al.(2007) The group lasso for logistic regression for more information on that aspect.)
If you are starting and have some queries about notations, standard textbooks on the use of matrices in Statistics are: Matrix Algebra Useful for Statistics by Searle and Matrix Algebra From a Statistician's Perspective by Harvill. Both are consider classics on the matter and you can use them as well-accepted references.
(I also like: Matrix Algebra: Theory, Computations, and Applications in Statistics by Gentle, I have used it a lot).
Comment: Remarkably useful. Thank you so much!
Comment: I am glad I could help. If this answer is helpful please consider upvoting it and if it resolves your question marking it as the accepted answer.


FTP IIS Log Files not working?

Question: I have FTP site. When I enter wrong UserName/Password, then I don't find any log in C:\inetpub\logs\LogFiles. I have checked, FTP logging, It's enabled and path is correct.
Comment: Your question is ambiguous: Is IIS logging not logging the UserName of FTP users or not logging at all?
Answer: How long did you wait? IIS logs are not flushed to the disk instantly.
See: http://weblogs.asp.net/owscott/archive/2012/02/03/flush-http-and-ftp-logs-in-iis.aspx - unfortunatly flushing the FTP logs is not as straight forward as flushing HTTP logs:
Powershell:
<code>Param($siteName = "Default Web Site")

#Get MWA ServerManager
[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" ) | Out-Null
$serverManager = new-object Microsoft.Web.Administration.ServerManager

$config = $serverManager.GetApplicationHostConfiguration()

#Get Sites Collection
$sitesSection = $config.GetSection("system.applicationHost/sites")
$sitesCollection = $sitesSection.GetCollection()

#Find Site
foreach ($item in $sitesCollection){

    if ($item.Attributes.Item("Name").Value -eq $siteName){
        $site = $item
    }
}
#Validation
if ($site -eq $null) {
    Write-Host "Site '$siteName' not found"
    return
}

#Flush the Logs
$ftpServer = $site.ChildElements.Item("ftpServer")

if (!($ftpServer.ChildElements.Count)){
    Write-Host "Site '$siteName' does not have FTP bindings set"
    return
}

$ftpServer.Methods.Item("FlushLog").CreateInstance().Execute()
</code>

FTP IIS Log Files not working?


Do histograms need to be sorted to determine if the data follow a power Law distribution?

Question: I am analyzing Twitter response data. I plotted a histogram shown below:

As you can see, most tweets get a response fairly quickly. I am confused whether this follows a power law distribution. I notice the J-shape of this distribution. It makes me think that this is indeed power law distributed.
However, power laws, like Zipfs law seem to "sort" the histogram in order to determine whether it follows a power law. Do we need to sort a histogram, before claiming it is a power law?
My reasoning that this follows a power law is that, it looks J-shaped and decreases rapidly. But I would like confirmation in my understanding.

I fit a one-parameter power law distribution and created a QQ plot below. The correlation coefficent is fairly low, so I'd presume that this isn't power law distributed then.
Comment: @looperr: can you please quantify what the $x,y$ axis represent? What is "Twitter response data?" It looks like you're looking at response time to a post?

The reason Zipf's law sorts words by frequency is that words inherently don't have a numeric order. Response time is already ordered so there's no real sense in reordering it by frequency.
Comment: Power laws are not that easily identified. See this paper by Shalizi, et al, which pretty much debunks the too frequent claims to power law distributions... http://www.santafe.edu/media/workingpapers/07-12-049.pdf In addition, there's this PDF of a presentation Shalizi gave which is more accessible ... http://www.stat.cmu.edu/~cshalizi/2010-10-18-Meetup.pdf
Comment: Could you please explain what you mean by "sort" a histogram? Note that this bar chart is not a histogram (because it uses a log scale for the counts). Note, too, that few statistical claims can be justified solely from a set of qualitative observations like "decreases rapidly:" you need to *quantify* the data behavior to draw clear and valid conclusions.
Comment: Thanks for the comment whuber. By sort, I mean: In Zipf's law: they sort the word counts by highest to lowest. Then it is claimed that this is power law distributed. My distribution follows a similar shape to a power law, but, I'm unsure of whether it is truly a Power Law (as I didn't sort the bars from highest to lowest).
Comment: Hey Guys: Do you have any ideas? This has been bothering me for sometime. Are both power law distributions?
Comment: @Alex R, the x axis represents the time taken to respond to a Tweet. The Y axis corresponds to frequency. Does Power Law not apply to ordinal x axis?
Comment: So first, normalize the data to get an actual density for your data. It looks like you'll have to play around with bin size to get something sensible for larger response times. After that, you can try fitting a power law, exponential or whatever distributions you desire.
Comment: @DJohnson, agreed. And in many cases, it's not totally clear that it matters. The OP needs to decide if there's some particular power-law-generating model at play in their data, or if they just need something with heavy tails.
Answer: A power law is just a name for a particular form of distribution, specifically:
$$ \begin{align}
p(x)  &\propto x^{-\alpha} \textrm{ } \textrm{ or}\\
p(x=X) &\propto x^{-\alpha}
\end{align}
$$
depending on whether the data are continuous or discrete. Despite a lot of hype, there's nothing particularly mystical or magical about them. Power law-distributed data has a long tail (highly right skewed) and falls along a (negatively-sloped) line on a log-log plot.  Cosma Shalizi has a great blog post on determining whether your data is actually power law distributed. Importantly, he points out that many other distributions also share those properties (e.g., log-normal) and the usual trick of fitting a straight line to a log-log plot isn't particularly well-founded. In the post, he also links to some papers showing that many putative power law relationships aren't actually great fits to the data.
In brief, he recommends the following steps:

Maximum likelihood estimation to find the scaling exponent ($\alpha)$
The goodness of fit procedure described here to where the power law-y region begins
A K-S test to check goodness of fit (using a bootstrap and not the regular tables because you're testing against an estimated fit).
Vuong's test for model selection against other heavy-tailed distributions (e.g., log-normal)

For the specific case of Zipf's law, it shows that $\log(\textrm{rank}) \propto \log(\textrm{frequency})$ for many natural languages. If you want to compute this specifically, you will need to turn your data into ranks and frequencies, which seems like it will probably entail sorting it.
Most other power law relationships seem to describe a survival function (e.g., sales of books) and these also necessarily involve sorting. They don't, however, necessarily involve binning into a histogram: you can--and shoud--generate these directly from the data.
Comment: Hey Matt, I just updated the question with a QQ Plot for Power Laws. It seems to give me a low coefficent of determination! So - I suppose that this isn't a power law. Just to confirm, was using the QQ plot a good idea here? (i'm a novice stats guy)
Comment: That...looks like a pretty bad fit. You may want to work through the steps in that post anyway though, because many of the power-law things only "work" over a certain range.


MySQL: Truncate Table vs Delete From Table

Question: When do we use the DELETE command versus the TRUNCATE command? I am trying to find on the Internet but both commands delete the data; I can't tell the difference.
Comment: you can refere to following:

http://stackoverflow.com/questions/20559893/comparison-of-truncate-vs-delete-in-mysql-sqlserver
Comment: Truncate command is faster than delete command. If you want to delete whole table then better use truncate command
Comment: http://stackoverflow.com/questions/2763295/what-is-the-diffrence-between-truncate-and-delete-in-sql-server/2763332#2763332
Comment: Did you really ask what's the difference when you completely empty the table and when you delete certain records from it? It's **common sense** what to use and when.
Answer: DELETE FROM TABLE
1. DELETE is a DML Command.
2. DELETE statement is executed using a row lock, each row in the table is locked for deletion.
3. We can specify filters in where clause
4. It deletes specified data if where condition exists.
5. Delete activates a trigger because the operation are logged individually.
6. Slower than truncate because, it keeps logs.
7. Rollback is possible.
8. LIMIT clause can also be used to set a limit on the number of rows to be deleted.
9. ORDER BY clause can be used in DELETE statement. In this case, the rows are deleted in the specified order.

TRUNCATE TABLE
1. TRUNCATE is a DDL command.
2. TRUNCATE TABLE always locks the table and page but not each row.
3. Cannot use Where Condition.
4. It Removes all the data reset the auto increment number.
5. TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.
6. Faster in performance wise, because it doesn't keep any logs.
7. Rollback is possible.
8. Cannot use LIMIT and ORDER BY.

DELETE and TRUNCATE both can be rolled back when used with TRANSACTION.
If there is a primary key with auto increment, truncate will reset the counter.


how to prove $f(x)=x^2$ is uniformly continuous on an infinite union of closed sets

Question: Need to prove that $f(x)=x^2$ is uniformly continuous on $\bigcup_{n=1}^\infty \left[n, n+\frac1{n^2}\right]$
I know I can prove that f is uniformly continuous on $\left[n,n+\frac{1}{n^2}\right]$ but I'm not sure if it'll apply to this infinite union. Also this union is open, not closed so it's not compact, right?
Comment: The set is not compact (otherwise this would be easy), because it is unbounded. To prove that the function is uniformly continuous, let $S_n = [n, n+1/n^2]$ and $S = \cup_n S_n$. Then use the fact that if $|x-y| < \delta$ and $x,y \in S_n$, then also $|x-y| \le n^{-2}$ and therefore $|x^2-y^2| = |x+y||x-y| \le 3nn^{-2} = 3/n$, for any $n$.
Comment: For the second question: that union not only it is not open, but it's also closed. However, it is not compact, because it is not bounded.
Comment: Sorry I don't really get the $|x−y|≤n^−2$. Can you give me more detail?
Comment: If $x,y \in S_n$, then $n \le x,y \le n + n^{-2}$, hence $|x-y| < n^{-2}$. And then $|x+y| \le |x| + |y| \le n+1 + n+1 \le 3n$, at least if $n > 1$.
Comment: Hi Hans, so for this I will set $delta=1/n^2$, so that $|x^2-y^2|$ is smaller than all epsilon?
Answer: Lets define $M_l:=\bigcup_{n=1}^l \left[n, n+\frac1{n^2}\right]$ and let $\varepsilon>0$ be arbitrary.
Since $$\lim_{n\to\infty}f(n+\frac1{n^2})-f(n) = \lim_{n\to\infty}\frac2n+\frac1{n^4}=0,$$ we can choose $k\in\Bbb N$ with $\varepsilon>f(n+\frac1{n^2})-f(n)>0$ for all $n>k$. It is easy to see now that one can find $\delta>0$, such that $|f(x)-f(y)|<\varepsilon$ for all $|x-y|<\delta$ with $x,y\in M_\infty\setminus M_k$. (This follows from the monotonicity of $f$ and the fact that the intervals $\left[n, n+\frac1{n^2}\right]$ is not getting arbitrarily close to each other.)
Since $M_k$ is compact and $f$ is continuous, the restriction of $f$ to $M_k$ is uniformly continuous. Thus, by (possibly) sufficiently shrinking $\delta$, we can achieve $|f(x)-f(y)|<\varepsilon$ with $|x-y|<\delta$ and $x,y\in M_k$.
Now we have $f$ uniformly down pat on the whole set $M_\infty$.


Unknown Ornament Notation - Three horizontal short lines

Question:
This nonation appears on Partite diverse sopra l'Aria 'Schweiget mir von Weiber nehmen' by Reincken, Johann Adam, IMSLP #04945.
Comment: Very, very tenuto? :)
Comment: "don't even think about ornamenting this note"?
Comment: @JohnBelzaguy Tenutississimo
Comment: To see how this is commonly performed I downloaded the pdf then found performances on youtube and spotify but they all sounded like they were playing from a different edition. So I'm none the wiser.
Comment: Just chiming in to say I'm stumped. I've never seen it before, it doesn't appear in [this list](https://www.dolmetsch.com/musicalsymbols.htm), and strangely enough it only happens the two pictured times out of a 13-page publication. I can't see a hypothesis based on the musical context either. [An original manuscript](http://conquest.imslp.info/files/imglnks/usimg/1/13/IMSLP616990-PMLP991212-ouch_p61-p65.pdf) of another piece by Reincken shows nothing of the sort (no notated ornaments at all, in fact).
Comment: my vote goes for the typesetter accidentally tossing ledger lines in and not bothering to fix it.
Comment: Tags harpsichord and organ may help.
Comment: I love this kind of question. Looking forward to seeing who works it out…!
Comment: I'm gonna recruit some organ buddies on facebook... (And, for my joke answer: every web dev on the SE knows it's the "hamburger icon")
Comment: @CarlWitthoft - 1st example, feasible, 2nd not so.
Comment: I wouldn't assume, just because it's above the note, that it *is* an ornament. I've toyed with the idea of it having some structural or theoretical meaning, the way one might mark a *hauptstimme* or *nebenstimme*. A Schenkerian reductionary view of the phrase would point out a scalar descension from the D to the C, and on to the cadence through B, A, and G. (But why mark just the first two...)
Comment: @BrianTHOMAS I tried a few recordings too and notice that the first three—organ, harpsichord, and modern piano—all play that D as a B. And otherwise ornament other notes than those indicated in this edition.
Answer: The Dolmetsch Musical Symbols guide lists the = symbol, but not ≡, but for = it says this:

a symbol found above note heads in The Bird Fancyer's Delight which is explained in the original publication thus: "The marks & rules for graceing are these Viz. a close shake thus ="

I expect that you'd perform = similarly to ≡
Comment: Searching [this treatise](https://www.dolmetsch.com/Vibrato_pictures.pdf) for "close shake" shows that it meant many different things to different people. Before I got too concerned about it, I'd try to find an edition that was closer to Reinken than this 1886 one and verify that it was even in the original. Manuscripts of Reincken's other works seem to be free from notated ornaments altogether, and maybe this is just an artifact of an over-zealous editor wanting to notate an un-notated practice.
Comment: in my opinion, we can listen the sound record of this sheet to reongize this ornament, or find book that talking about musical ornament?


Minimize sum of inner products of a set of vectors

Question: Please forgive me if this question has already been asked. I am not a mathematician and I expect I am missing some of the relevant vocabulary.
Consider a set of six vectors $\{v_1,v_2,v_3,v_4,v_5,v_6\}\in\mathbb{R}^3$. Additionally $|v_i|=1$ for $i=1,2,3,4,5,6$. How can I choose all $v_i$ such that the sum
$$\sum_{i,j>i}v_i\cdot v_j=v_1\cdot v_2+v_1\cdot v_3+v_1\cdot v_4+v_1\cdot v_5+v_1\cdot v_6+v_2\cdot v_3+v_2\cdot v_4+v_2\cdot v_5+v_2\cdot v_6+v_3\cdot v_4+v_3\cdot v_5+v_3\cdot v_6+v_4\cdot v_5+v_4\cdot v_6+v_5\cdot v_6$$
is minimized.
The best approach I can think of at the moment is to define some function
$$f(x_1,y_1,z_1,x_2,\cdots,x_6,y_6,z_6)=\sum_{i,j>i}v_i\cdot v_j$$
and then find the solution to $0=\nabla f$. However, I don't know how to constrain this solution so that $|v_i|=1$. I am interested in learning how to constrain the solution to this case, and if alternative methods exist for minimizing $f$.
Also, if there is a specific term used to denote the sum $\sum_{i,j>i}v_i\cdot v_j$, please let me know!
Answer: This is not a full answer but something to consider in terms of symmetry. Since the dot product is symmetric, we expect the resulting configuration of vectors to possess some kind of symmetry, and in some sense to be maximally symmetric. The "maximally symmetric" configuration of $6$ vectors in $\mathbb{R}^3$ is something like $\{e_1,e_2,e_3,-e_1,-e_2,-e_3\}$. You can check that $f$ here is equal to $-3$. I do not claim that this is the minimum yet, but we can see what happens when we perturb a chosen vector out of this symmetric state.
For example, let me perturb $e_3$. Rotating it about the $e_1$ axis, let us consider what happens to $f$. The dot products of $e_3$ with $e_1$ and $-e_1$ remain unchanged. Curiously, the dot products with $e_2$ and $-e_2$, while now non-zero, cancel each other out exactly. Thus the only term affected is the dot product with $-e_3$ whose value becomes less negative. A similar argument works for rotations about the $e_2$ axis. This hints toward the fact that perturbing in any direction will only make $f$ less negative. Here's why: suppose you perturb $e_3$ slightly so that the projection onto the $xy$-plane falls in the 1st quadrant, then you can decompose the perturbed $e_3$ into a the $z$ component and the $xy$. By symmetry in the $xy$ plane, the dot product of the $xy$ portion with $e_1,e_2,-e_1,-e_2$ cancel out, so all that's left is the $z$ component which contributes to a less negative value for (perturbed $e_3$) $\cdot -e_3$. Okay so what does this establish? It establishes that this configuration is a local minimum for $f$ with $f = -3$.
This isn't the only such maximally symmetric state for which $f = -3$ either. Suppose you continuously move $e_3$ to $\frac{1}{\sqrt{2}}(e_1+e_2)$ and similarly $-e_3$ to $-\frac{1}{\sqrt{2}}(e_1+e_2)$, such that at each step, the $e_3$ and $-e_3$ are always opposite. Throughout this entire transformation, $f$ is unchanged at $-3$. So now you end up with $6$ vectors in the $xy$ plane. You can go even further and deform so that all $6$ lie on a line, $3$ in one direction and $3$ in the other. $f$ is still $-3$. So not only are these states all local minima, there is a continuous path in the configuration space that connects a lot of these states.
The one thing that remains the same in all these configurations is that every vector is accompanied by its negative. You can easily show that given these constrains, $3$ pairs of vectors and their negatives will always give you an $f = -3$. Since your configuration space is compact (product of $6$ spheres), any global extrema will occur at local extrema (there is no boundary). We are just shy of completing this proof. We need to establish that there are no other local minima. I have not yet figured out how to show this. Hopefully this helped with the intuition however! It's a highly geometric problem.


What variation of the minor scales should I use for tonal species counterpoint?

Question: I just started learning about species counterpoint, and I thought one should only use (for both cantus firmus and counterpoint) the natural keys of the scale (diatonic) until I saw this G minor example:

So my question is, when to change to harmonic or melodic?, can you do this in both CF and counterpoint?
Comment: Small point, but both F and F# occur in G melodic minor.
Comment: So let's say I had to write an excercise in "G minor", do I have to use F or F#? that's the point I dont get, is it up to me or what does it depend on?
Comment: Generally speaking, the melodic minor is formed with raised leading note and raised 6th when a melody is rising, but when it is going down, both the 6th and 7th notes revert to those of the relative major. In the Gm example, those notes are E and F# going up, and Eb and F going down. it was considered at the time that the 3 semitone gap between natural 6th and raised 7th as in harmonic minor was too great. I've tried to put this into laymans' terms. These days, it's more of a question of what actually *sounds* better.
Answer: In some sense, writing strict species counterpoint in a modern "minor key" is an anachronism.
Really, you ought to use one of the 8 Church Tones which were based on the medieval plainsong modes - some of them transposed, and written with modern-looking key signatures of one or two flats, but not conceptually the same as the modern ideas of "D minor" or "G minor". This system was more or less standardized across Europe in the Renaissance and Baroque periods, and is what Couperin meant when the title of a piece mentions the first to eighth "Ton," the Spanish composers by "Tono," etc.
The tonality of this music was not so sharply defined as in the classical era - it would be more accurate to say the music used any of the "well tuned" chords in meantone temperament, and "modulation between different keys" was not recognized as a concept - successive cadences were often in what appear to be different "keys" but the "modulations" don't have much structural function in defining the form of the complete piece.
This little history lesson doesn't entirely answer your questions, because the "leading note" of the "Ton" was often (but not always!) sharpened to be a semitone below the tonic, especially at cadences. On the other hand, for music that was not strictly contrapuntal or liturgical, Renaissance keyboard composers were quite happy to write F# and G# in one hand simultaneously with F and G natural in the other, in the key of "A minor," especially if the notes were separated by 2 octaves - though writing like that probably won't get you good marks in a 21st-century examination! (Meantone temperament softens the effect compared with modern equal temperament, and the resulting clashes don't sound like "discords" at all once you get used to them.)
Another way to approach this question is to stick to the notes that were "in tune" in Meantone temperament - i.e. C C# D Eb F F# G G# A Bb B in the normal tuning with key signatures of 1 of 2 flats - though sometimes G# was retuned to Ab if the tonality ventured somewhere close to the modern C minor.
Here's a brief history lesson: http://www.kholopov.ru/arc/atcherson.pdf
Comment: So, if I wanted to write a "Renaissance-like" counterpoint excercise (with a tonal approach) in minor mode, I have to stick to the notes of a natural minor scale right?


Proving that ${\textrm{gcd}} (n, p-1) = 1$ if $p$ is the prime is the smallest prime divisor of $ n $

Question: Show that if $n>1$ is a positive integer and $p$ is the prime is the smallest prime divisor of $ n $ then $ {\textrm{mcd}} (n, p-1) = 1$.
Comment: Is $\operatorname{mcd}$ the Spanish name of the greatest common divisor ($\gcd$)?
Comment: yes, it means máximo común divisor
Answer: Suppose to the contrary that $\gcd(n,p-1)=d\gt 1$.
Then some prime $q$ divides $d$. It follows that $q$ divides $n$, and $q\lt p$, contradicting the fact that $p$ is the smallest prime divisor of $n$.
Answer: Assume that $\operatorname{mcd}(n,p-1)=d>1$. Then $d\mid n$, but since $d\leq p-1<p$, we have a contradiction, since $p$ is the smallest prime divisor of $n$. So $\operatorname{mcd}(n,p-1)=1$.


Error while inserting data to an enum generated field mysql

Question: I am exporting data from one database on one server to another database on a different server. I am exporting to a csv file and using the import function on MYSQL workbench. Why am i using this way? That is because of access issues.
There is a field defined as,
<code>`isTest` enum('TRUE','FALSE') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci GENERATED ALWAYS AS ((case when ((`Fixed` = _utf8mb4'FIXED') and ((`Type` is null) or (`Type` = _utf8mb4''))) then _utf8mb4'TRUE' else _utf8mb4'FALSE' end)) STORED,
</code>
The data in the exported file for this field looks like,
<code>"FALSE"
</code>
The error is,
<code>grt.DBError: ("The value specified for generated column 'isTest' in table 'Testing' is not allowed.", 3105)
</code>
Is there another way of inserting the data using workbench/editing the field column definition?
Answer: So i excluded that field from the list of exports. The generated field will fill itself based on the data from the fields


Malware on D7 website - Blacklisted by Google

Question: I want to tell you about the malware attack to my Drupal website. Not just for your suggestions but also to create something helpful to anybody tha could suffer for the same problems. Well...

INITIAL SETUP

Drupal 7.9
Activated modules:

CORE: Block, Contextual links, Database logging, Field, Field SQL storage, Field UI, File, Filter, Image, List, Locale, Menu, Node, Number, Options, Overlay, Path, PHP Filter, RDF, System, Taxonomy, Text, Toolbar, User
CCK: Multiselectd
CHAOS TOOL SUITE: Chaos tools
DATA/ORA: Calendar, Date, Date API, Date Popup, Date views
FIELDS: Email, Field permission, Link
OTHER: Google Plus One +1, Pathauto, Token, Weight
SHARING: Share this, Share this block
TAXONOMY MENU: Taxonomy menu
VIEWS: Views, Views PDF Display, Views PHP, Views UI
OTHER MODULES THAT I REMOVED: CKEDITOR, VIEWS_SLIDESHOW, IMCE, DOMPDF, PRINT, WYSIWIG

MY SETUP ERRORS

In order to satisfy the custome, I modified some of the modules and I've never update them (AUCH!)
The customer was in posses of the login data, and maybe his computer wasn't safe (MMM...)
I didn't have a copy of the webiste, because I trusted on the provider weekly backup (DOH!)

ATTACK EXTERNAL SYMPTOMS

All the link of the homepage redirected to a malware website
Google blacklisted the website
Critical alert on the Google Webmaster Tools panel

FTP SYMPTOMS

Lots of "strange" files: mainma3.php (I found this one in every folder!), functoins.php, sum75.html, wlc.html, aol.zip, chase.zip, chaseverification.zip, 501830549263.php, wp-conf.php and a dozen of wtmXXXXn.php (dove X = numero) in the root folder. All these files was plenty of malicious functions (unescape, base64_decode, eval, etc.)
Install.php was modified with a long line of malicious code
To EVERY javascript files was appended this line of code:
;document.write('');
The weekly backup was also infeceted
Dozen of repeated "strange" request, found on the Drupal log panel (my domain is obscured with the string "-----"):

index.php?q=ckeditor/xss > Notice: Undefined offset: 5 in eval() (linea 29 di /web/htdocs/-----/home/modules/php/php.module(74) : eval()'d code(1) : eval()'d code).
-----/user?destination=node/add > Failed login by shadowke
calendar/week/2012-W19?year=2011&mini=2012-12 > page not found
misc/]};P.optgroup=P.option;P.tbody=P.tfoot=P.colgroup=P.caption=P.thead;P.th=P.td;if(!c.support.htmlSerialize)P._default=[1, > page not found
misc/)h.html(f?c( > page not found
mail.htm > page not found

RECOVER [Thank to this article: http://25yearsofprogramming.com/blog/20070705.htm]

I've put the website on Maintanance mode (error503.php + .htaccess). Traffic open just for my IP Address
[see this useful guide: http://25yearsofprogramming.com/blog/20070704.htm]
I've downloaded the whole website in local
I've searched and removed the strange files > I found forty of them
I've searched the files for these worlds [with the freeware AGENT RANSACK]: eval(base64_decode($POST["php"])), eval(, eval (, base64, document.write, iframe, unescape, var div_colors, var _0x, CoreLibrariesHandler, pingnow, serchbot, km0ae9gr6m, c3284d, upd.php, timthumb. > I've acted in one of the follow ways: a) I've replaced eval with php_eval() (the eval safe version of drupal); b) I've wrote down the suspected modules; c) I've compared the code with the fresh downloaded module; d) I've removed all the malicious code (see the javascript mentioned above)
I've searched for mohanges in the file system [with the freeware WINMERGE]
I've identifyed some suspected modules, thank to the list written at the point 4 above, and thank to some researches on Google (name_of_the_module security issue, name_of_the_module hacked, etc...) and on Secunia [http://secunia.com/community/advisories/search]
I've scan my computer (Avast, Search&Destroy, Malwarebytes Antimalware) > I didn't found any virus or spyware
I've changed all the logins (ftp, cpanel, drupal admin panel)
I've reloaded the whole website
I've removed all the suspected modules: CKEDITOR, VIEWS_SLIDEWHOW, PRINT, DOMPDF, IMCE, CAPTCHA, WYSIWIG, WEBFORM.
I've tell the whole story to the provider assistance
I request Google for a revision (they did it in 12 hours)

DRUPAL LOG NOW
dozen of these messages
- wtm4698n.php?showimg=1&cookies=1 > page not found
- fhd42i3d.html > page not found
- wp-conf.php?t2471n=1 > page not found
- -----/user?destination=node/add > Failed login by Elovogue

LESSONS LEARNED

Never touch the modules, so you can update them
Keep all the login in a safe computer / Use a safe computer to work on the FTP
Search for any security issue before installing a module
Keep a clean copy of the website somewhere

MY QUESTIONS:

What kind of attack I've received?
There are other unsure module in my installation?
What can I do yet?

Thanks to everybody for your patience!
Answer:
What kind of attack I've received?

It's really hard to say. It could be a desktop virus, or a php execution via one of the modules (or insecure configuration). It's good that you seem to have blocked it.

There are other unsure module in my installation?

You should download a "good" copy of each module from drupal.org and compare them. If you haven't yet, put the whole site in revision control (git) and make the live site a checkout from git so you can quickly see "git status" if anything is changed and then "git reset --hard origin/master" to undo the changes.

What can I do yet?

In addition to the git idea, I suggest installing and running http://drupal.org/project/security_review which may have some suggetsions. Another potential helper is: http://drupal.org/project/paranoia
Comment: Good stuff, @greggles. I had no idea these modules existed.
Answer:
"Never touch the modules, so you can update them"
  There are often situation where you have to patch modules to fix bugs or add features that aren't in the latest stable release.
  This practice is generally ok as long as you are careful.

Always use version control as it helps you keep track of changes you have made, as well as changes someone else might have sneakily made.
I also recommend including a patch file of the changes in the module directory, which allows you to see at a glance if a module has been modified and what the changes were.
Also commit the patch file to version control.
The patch file also makes things easier when updating the module as it can often be reapplied after the update (sometimes if it doesn't apply to the new version you have to manually apply and re-create the patch).
Make sure the patch file name includes the drupal.org issue node id and comment id, so that you can easily check the progress of the issue that the patch addresses, so you can see if the new version of the module already includes the patch or not and you can get a newer version of the patch if there is one.

Keep all the login in a safe computer / Use a safe computer to work on the FTP

Yes. Also, use SFTP or SSH to connect to the server.

Search for any security issue before installing a module

This is generally not something you can do.
If you have the latest stable release of a module you should have the most secure.
There is no way to see security issues for a module that have not been fixed yet (or it would be easier for people to exploit them).
The exception to this are modules without a full release, like dev versions and betas, etc.
These modules don't go through the same security processes so they can have security issues posted in the public issue queue.
However, even in this case if you have the latest version you should have the most secure version.

Keep a clean copy of the website somewhere

Yes. This comes down to version control again.
If you have the code in a version control repository you have a clean version of the code.
You should also keep offsite backups of the database and non-version-controlled files.
The idea is you can fully restore a site in the case of emergency and hopefully never lose more than 24 hours of data (or less if you run backups more often).
Also, don't rely on general webhost backups. Do your own backups to a different server where possible.
I recently had an incident where the hosting provider had a server issue that took down MySQL, corrupting databases on a bunch of servers.
The nightly backups then happened after that, so the nightly backups were also corrupted.
So the data restored was in some cases a week old.
This kind of thing can happen with any hosting, however in theory should happen less with more high end hosting.
In this case if you take your own offsite backups you can avoid troubles.
It is possible for your backups to have problems too but if you have 2 lots of backups in different places you are much safer.

What kind of attack I've received?

As greggles says, there are so many possibilities that we can't know the answer to this.
Your hosting provider may be able to find out though.

There are other unsure module in my installation?

As mentioned, version control can easily allow you to see what has been hacked.
Aside from that, you could download all the same versions of the modules and drupal core and compare them to yours using diff or a similar command.
Then you can review all the differences and see if they are your customisations or something a hacker has done.
It could be a long process but it's good to be absolutely sure.
As for potentially dangerous modules, all you can do is use the latest version of all modules, as they will generally be the safest.
There are still always more security issues found though so you must keep them up to date.
The security team sends out notices about security updates.
You can also check out https://drupal.org/security, which has 3 tabs of security related information for contrib and drupal core.

What can I do yet?

Here are some things (there are plenty more):

Make sure you always keep your drupal modules up to date.
If you are doing custom development make sure to get up to speed with secure
coding practices for drupal & PHP and other related web technologies. A good place to start is Writing secure code.
Keep your server software up to date (if you have control over that).
Always use secure connections like SFTP to access the server.
Always use strong passwords and don't reuse passwords.
Give user 1 a unique username, not "admin" or similar.
Don't give the user 1 drupal account out to others, if admins need a higher level of access set up a role for them and give them only what they need.
If there are other user admins use the userprotect module to keep user 1 safe.
There are a bunch of other modules for better passwords and roles/access control management that might be useful depending on the website.
Answer: I'd add:
1) don't use FTP.
2) use version control on the server so you know what code you put there.
3) don't hack core or contributed modules and make sure to subscribe to security updates.
4) ensure you have set your file permissions on your server correctly so that Apache is limited to what files it can write to.
Answer:
What kind of attack I've received?

If your server was not exposed, most likely it was some remote injection exploit which worked over a network which exploits the security vulnerability without any prior access to the vulnerable system (e.g. SQL injection, XSS, etc.).
Example scenario:

Your hosting provider sends you the message that your site has been blocked, because it found a lot of IP addresses are visiting some particular file (e.g. /en/printmail/123).
You identified that this may be caused by Print module, so you've to check which version currently you've.
This helped you to find the right security advisory, e.g. SA-CONTRIB-2012-057 which says:

Security risk: Moderately critical
Exploitable from: Remote
Vulnerability: Cross Site Scripting

Then you know what kind of attack you had and you can start from there knowing the risk by knowing what happened and address the problem.

See also: How to remove malicious scripts from admin pages after being hacked?

There are other unsure module in my installation?

Try to not installing modules which are less popular therefore not tested properly. Especially when they're executing some dynamic code or they've their own custom code instead of using Drupal API properly or using custom SQL queries instead of using proper API and PDO.

What can I do yet?

Once you've removed malware, you should:

keep your Drupal core and all modules up-to-date,
keep up-to-date with Drupal Security Advisories & related news (Twitter),
keep up-to-date with your system packages (e.g. apt-get update),
make sure your PHP and web server is up-to-date and configured properly,
make sure your file permissions are correct,
See: What are the recommended directory permissions?,
monitor Drupal watchdog and system logs for any suspicious behaviour,
install Hacked module to monitor integrity of modules,
use some security monitoring tool (like Nagios) to monitor your website,
avoid using Drupal PHP filters/modules and similar (which allows injection of dynamic code),
never keep PHP code in your database (like in blocks or views),
disable development modules (like Devel),
keep learning about recent vulnerabilities to be up-to-date with technology,
and so on.


Tomcat memory usage

Question: I'm running tomcat on a ubuntu 10.4 VPS with 512MB of RAM (1024 burstable). I'm using it for development, so performance isn't an issue, but memory is.
Tomcat is currently using about 250MB without any apps installed (I compared memory usage with tomcat stopped and running), and I also need to run lighttpd and mysql.
Is there any way to get that number down? I don't need it to be able to handle a large number of requests at once.
Comment: Try looking in your config files, there might be a setting buried in there somewhere.
Answer: You can try tuning the following parameters on your tomcat startup
-Xms128m -Xmx128m
Also it would be worth you checking on what Garbage collection options you have enabled on tomcat, having GC tuned correctly will help keep your memory usage down.

Tomcat memory usage


python invalid literal error

Question: so I have this code
data = input()
m = data.split(',')

for i in range(0, len(m)):

    print(int(m[i]) )

but then when i run it and type in "1,2,3", i get this error:
    print(int(m[i]))
ValueError: invalid literal for int() with base 10: '"1'

what did i do wrong?
using python 3
Answer: As you have your code, the input you type in should be numbers separated by commas.  You do not have to include the quotes, just type 1,2,3 and nothing else.  You only need the quotes only if you are writing actual code writing string literals.  In this case, you are just taking input and it will already be strings.
The problem is that you are typing literally "1,2,3" and it tries to parse the first int the string, "1 which is invalid.
Answer: It seems you typed in literal quotation marks (") as part of your input; int() doesn't know how to parse those. You'll need to strip them out first:
data = input().strip('"')

Answer: Looks like there's an extra " character in your input. int() will only convert strings consisting solely of digits in the selected base (default base 10, so it accepts [0-9]). If any other characters appear in the string input, it raises this error.
Answer: Your input to input:
"1,2,3"

is appropriate for the Python 2 input. Python 3 input is like Python 2 raw_input and all its input is already considered a string.

python invalid literal error


Diophantine equation $n^2+n+1=m^3$

Question: Is there an elementary method for solving Diophantine equation $n^2+n+1=m^3$ for integers $m$ and $n$? There is a similar one, which I could solve:$$p^2-p+1=q^3,$$where $p$ and $q$ are prime numbers. But the technique that I used for solving this doesn't work for the original problem!
Comment: If you put $n=-p$, then you get a family of solutions (but not all)
Answer: $$n^2+n+1=m^3$$
Here can be multiplied by $64$ and obtain the equation:
 $$(8n-4)^2+48=(4m)^3.$$
All solutions $y^2+48=x^3$ in integers known: $(4;\pm4)$ and $(28,\pm148)$.
Then  $m^2-m+1=n^2$ has solutions in positive integers: $(1,1)$ and $(19,7)$.
Comment: Here is a reference: $y^2+48=x^3$ is Mordell's equation $y^2=x^3-48$. Search for `E_-00048` in http://tnt.math.se.tmu.ac.jp/simath/MORDELL/MORDELL-.

Diophantine equation $n^2+n+1=m^3$


Cfimage inside a CFC

Question: I seem to be having an issue manipulating photos from within my CFC. I get an error that says that it encountered an exception while trying to read the image. So the question is pretty straightforward, are there any issues with manipulating files from within the CFC, rather than the CFM?
<code>    <cffunction name="imageResize" access="public" returntype="boolean">

    <cfargument name="filename">
    <cfset result = "true">

    <cfimage
    action = "resize"
    source = "#root#/documents/uploads/PHOTOS/#filename#"
    width = "400px"
    height = ""
    destination = "#root#/documents/uploads/PHOTOS/thumbs/#filename#"
    overwrite = "yes"></cfimage>

    <cfreturn result>
    </cffunction>
</code>
Thanks
Comment: Have you tried running the same code in a CFM, rather than the CFC above, before blaming CFC?
Answer: There are no issues with having cfimage inside a cfc. It's something I do all the time.
I suspect that the issue you have is that the variable "root" is unknown inside your function. You should probably also var scope your result variable and specify the scope on your use of the filename variable.
Comment: Thanks! In that case, is there any problem with the way I am using "destination" if I want the resized image to be put in the thumbs folder? I couldn't find much that explained that attribute to me.
Comment: @AustinSlominski make sure `root` is what you expect it to be since it is not in arguments scope, and it could be now shared by multiple requests and this might not be what u want.
Answer: I have found quite a few issues with your code and have rectified them and it is working fine.
Here are the issues.

The function name 'imageResize' needs to be changed to some other name, because the function name provided by you is an existing coldfusion function.
The width attribute in the cfimage tage need to have an integer like 420 not 420px.
The source & destination attributes in the cfimage tag need to have absolute path like 'D:\projects\test' not like '/test/images'.

FYI: The 'root' variable you have mentioned in your code should be accessable in the function.
Here are the rectified code.
<code><cffunction name="imageResize2" access="public" returntype="boolean">
<cfargument name="filename">
<cfset result = "true">

<cfimage
action = "resize"
source = "D:\Projects\Test\images\#filename#"
width = "400"
height = ""
destination = "D:\Projects\Test\temp\#filename#"
overwrite = "yes" />

<cfreturn result>
</cffunction>
</code>
Yes, there are no issue in manupulating the files/images in cfc. The above code works both in cfs and cfm.

Cfimage inside a CFC


clear cookies stored by single website Via bookmarklet

Question: Hi is there anyway to remove all cookies saved by single website ?
For example i can view the cookie stored by a particle websites by typing following code in bookmarklet.
javascript:alert(document.cookie);

I know below code but , is there small code that fits in bookmarklet ?
var cookie = document.cookie.split(';');

for (var i = 0; i < cookie.length; i++) {

    var chip = cookie[i],
        entry = chip.split("="),
        name = entry[0];

    document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

Answer: You can use a minifier tool like the Online YUI Compressor which will minify and put all your code in a single line. As an example, your code above turns into the following after minification:
var cookie=document.cookie.split(";");for(var i=0;i<cookie.length;i++){var chip=cookie[i],entry=chip.split("="),name=entry[0];document.cookie=name+"=; expires=Thu, 01 Jan 1970 00:00:01 GMT;"};


HTML viewport meta starts from specific width

Question:
I'd like to ask you if there is any possibility to run  from a specific width... for example I want to do something like this:
<meta name="viewport" start_from="width=500px" content="width-device-width">

If there is a possibility I would appreciate if you post it here. Thank you for help.
Comment: What's the purpose of this? Maybe there's a better method.
Comment: Well I want to have responsive site from specific width..  for example when somebody is on my webpage from desktop PC I don't want the site to be responsive, but when somebody is on my webpage from phone or netbook I'd like to made viewport for width-devide-width, so that means my page would be responsive. I don't mean it like the final decision, I'm just experimenting and learning new things and it's interesting for me, maybe it would look nice :)
Answer: use a @media tag and css.
You put meta viewport according divice width:
<meta name="viewport" content="initial-scale=1.0, width=device-width, user-scalable=yes, minimum-scale=1.0, maximum-scale=2.0">

and, then adjust the size for the panel attached to the viewPort:
For example:
you can sent the styles for content on screens that have a width of 480px and lower.
 @media all and (max-width: 480px) {
      // styles assigned when width is smaller than 480px;
 }

Media Queries for Standard Devices
Hope that will helps you to achieve your task.
Comment: I'll try it, thank you. But isn't the whole page responsive then ? Or it is resposive only from the width , as in example, 480px ?
Comment: Here you need to define different - 2 media rules for different - 2 widths. Note:CSS media-query works like a if-else statements. Please read this one: http://line25.com/tutorials/create-a-responsive-web-design-with-media-queries
Comment: Your welcome. You can up-vote answer, if it's helpful to you..:)
Comment: I will.. I just need to get 15 rep first ;)


How can You refresh a vaadin grid after data change

Question: How can You refresh a grid, when some new row was added to its datasoruce container or one of its rows has been edited?
I have tried the hacks written about here (http://www.helpsforcoder.com/code/31861375-vaadin-refresh-grid-after-row-modification.html ) and here (deleted link to malicious advertisement) but with no positive result. I add / edit the row in a separate subwindow that 'pop-ups' over the view, where the grid, that should update its contents, is placed.
Comment: Your Question is not clear. Please describe exactly what you are doing. Show code for simplest possible example. Have you worked through examples in The Book Of Vaadin?
Comment: Please cite from the real StackOverflow.com. Both of your citations appear to be from sites that clone questions and answers from StackOverflow.com. The second one is a spammer that displays an repeating loops of lies saying iOS has crashed.
Comment: possible duplicate of [Update Grid with a fresh set of data, in Vaadin 7.4 app](http://stackoverflow.com/questions/29204674/update-grid-with-a-fresh-set-of-data-in-vaadin-7-4-app)
Comment: Sorry Supamiu, but the solutions proposed in http://stackoverflow.com/questions/29204674/update-grid-with-a-fresh-set-of-data-in-vaadin-7-4-app don't work. The link to them was in my original post but had been removed from it for some strange reason.

I had googled and searched beforehand and tried out some things, none of them really working.

So this is definitelly not a duplicate question, even though it might seem so. But as the solution from the link does not work, it is not a solution for me.
Answer: grid.getDataProvider().refreshAll();
Answer: Grid listens to changed property values. So if you are working on the property level of vaadin everything should be fine.
If you extract your bean out of the BeanItemContainer you directly manipulate the values of the bean. That way the property container can not recognize your manipluating action.
So you have to send your grid implementation a message, the properties have changed manually.
When your BeanItemContainer still has the hard reference to the changed object, grid.markAsDirty() should work.