Azure functions logging lots of performance counters on .net 7

Question: I've got a docker based (linux) azure functions app running in Azure App service which we've recently updated to using .net 7. Since this update we've seen a massive spike in the amount of application insight logs for performance counters.
Previously we were getting around 1 performance counter per name per minute (so 60 "Private Bytes" entries per hour, 60 "% Processor Time" entries per hour and 60 "% Processor Time Normalized" entries per hour) All of these entries have the instance of "??APP_WIN32_PROC??".
Now we're getting upto over 6000 entries per hour for each counter name.
We're using a classic application insights resource configured in the functions app using the APPINSIGHTS_INSTRUMENTATIONKEY configuration key.
I've been trying to work out where these counters are coming from and why they have suddenly increased.
Answer: We think we've found that this was a case of the functions app was actually failing to start with some exceptions and our best guess is that these performance counters are the app constantly rebooting but never starting cleanly.
Comment: Performance counters are a Windows OS feature and have to be collected by some other process, eg Performance Monitor on Windows. It's Azure itself that reads those counters when the application starts.


What does flow {| brace pipe |} syntax do?

Question: What does this Flow syntax mean/do?
type Options = {|
  assumeValid?: boolean,
  commentDescriptions?: boolean,
|};

I can't seem to find where the {| ... |} syntax is documented.
Answer: This is Flowtype's Exact Object type syntax.


What are the physical differences between tuned and non-tuned drums?

Question: Why do some drums (e.g. timpani, rototom) have a distinct pitch, while others do not?  What is the physical difference that results in a distinct pitch or lack thereof?
Comment: This has to do with how what modes of vibration the drum has, and what the frequency relationship of those modes with each other is. https://www.acs.psu.edu/drussell/Demos/MembraneCircle/Circle.html has some nice graphics of modes, though won't fully answer your question about the differences. Note that there's not a clear distinction between 'tuned' and 'non-tuned'; there's a lot of woolly 'kinda-tuned' space in the middle.
Comment: @RishiNandha_M if you think every sound has a pitch, I'm interested how you are defining "pitch". What's the pitch of white noise, for example?
Comment: Interested to read some drummers' replies to this. AFAIK most drums have some pitch they can be tuned to; for instance, there are different ways to tune a standard drum kit. But you're right, some drums are considered pitched, and others less so.
Comment: I used pitch in a loose sense. I didn't really mean the most prominent frequency when I said pitch. I was just trying to say that "non-tuned" drums don't mean the sound produced doesn't have a frequency. Sorry for using it in a loose sense.
Comment: To start with, have a look at my answer here: https://music.stackexchange.com/a/24696/9198 The other answers and question are interesting too. There are a few other related posts on Music.SE that can be found by searching for "drums tuning".
Comment: Just saying, Every sound has a Pitch. Even the non-tuned ones have pitch, just that the drummer (or Percussionist) doesn't care about the pitch. Like in Rock Music, Drums is mostly used for Beats. Thus pitch doesn't matter. But in Orchestra Timpani's maybe used in Building Tension, and also Harmonies. Thus Timpanis are Tuned. In Indian Music, all the Percussion Instruments are tuned ireespective of the role it plays (Beats or Harmonic). I'm also looking forward for a Percussionist to share info on how They differ Physically.
Comment: @RishiNandha_M - 'every sound has a pitch' - most sounds have several more pitches too!
Answer: It's a much simpler job to tune a drum which has one head. Timps are such. One head will produce one pitch (plus the complex overtones) when it's stretched regularly. When it's uneven, it'll try to produce several different pitches, resulting in a bit of a mess. Rototoms are similar, with just the one head. I don't think they'd do the job with two.
The majority of drums with two skins/heads are not impossible to tune so that they produce a pitch, but for that to be successful, both heads need to be in tune, which is often not what happens on a standard drumkit, for instance. It's bad enough getting the beater head regulated, without then repeating the operation on the other.
Answer: Some physical differences that result in a distinct pitch of an instrument may include but are not limited to, size of the drum, tightness of the drum, the type of material of the drum, etc. I hope that helps.


Validação com expressão regular considera inválido mesmo quando encontra um match

Question: Gostaria de saber o que está de errado nesse código. Todos os valores válidos que insiro no nome, email e telefone acusam como inválido, mesmo estando no formato solicitado pela regex.
import re

class Contato:

def __init__(self):
    self.__nome = ''
    self.__email = ''
    self.__telefone = ''

def __validaEmail(self, email):
    result = re.match('(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', str(email))
    if result == None:
        return False

def __validaTelefone(self, telefone):
    result = re.match('(d{2}) d{4,5}-d{4}', str(telefone))
    if result == None:
        return False

def inicializaContato(self, nome, email, telefone):
    if self.__validaTelefone(telefone) and self.__validaNome(nome) and self.__validaEmail(email):
        self.__nome = nome
        self.__email = email
        self.telefone = telefone

def getNome(self):
    return self.__nome
def getEmail(self):
    return self.__email
def getTelefone(self):
    return self.__telefone

def setNome(self, nome):
    str(nome).strip()
    if str(nome).isalpha():
        self.__nome = nome
    else:
        print('\033[1;31m','NOME INVALIDO!','\033[m')

def setEmail(self, email):
    if self.__validaEmail(email):
        self.__email = email
    else:
        print('\033[1;31m','EMAIL INVALIDO!','\033[m')

def setTelefone(self, telefone):
    if self.__validaTelefone(telefone):
        self.__telefone = telefone
    else:
        print('\033[1;31m','TELEFONE INVALIDO!','\033[m')

Comment: E quais são os valores aparentemente válidos que você inseriu e acusou como inválidos?
Answer: Vamos analisar o método que valida email:
def __validaEmail(self, email):
    result = re.match('(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', str(email))
    if result == None:
        return False

Quando result for None, você retorna False, mas quando ele não for None (ou seja, quando o email for válido), você não retorna nada. E com isso o "retorno" da função acaba sendo None. Exemplo:
def f(x):
    if x == 1:
        return True

print(f(2)) # None

Se eu passar 2, a função não entra no if e não retorna nada, por isso o código acima imprime None.
E como None é interpretado como falso, if self.__validaEmail(email) estará testando o mesmo que if None, o que é sempre falso.
Sendo assim, você deve alterar a validação para sempre retornar o resultado do match:
def __validaEmail(self, email):
    return re.match(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', str(email))

def __validaTelefone(self, telefone):
    return re.match(r'(d{2}) d{4,5}-d{4}', str(telefone))

Se a regex encontrar um match, ela retorna o resultado correspondente, e ao testá-lo no if, ele será considerado verdadeiro (objetos por padrão são avaliados como verdadeiro).
Mas ainda há um problema na regex do telefone. Você usou d{2}, que significa "duas ocorrências da letra d". Se você quis dizer "dois dígitos", na verdade deve usar o atalho \d.
Outro detalhe é que os parênteses possuem significado especial em regex (servem para formar grupos de captura). Se você quer que o telefone tenha parênteses de fato, deve escapá-los com \, assim eles perdem seu significado especial e passam a ser interpretados como os caracteres ( e ).
Então os métodos de validação ficam assim:
def __validaEmail(self, email):
    return re.match(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', str(email))

def __validaTelefone(self, telefone):
    return re.match(r'\(\d{2}\) \d{4,5}-\d{4}', str(telefone))

Testando:
c = Contato()
c.setEmail('firstname.lastname@example.com')
print(c.getEmail())
c.setTelefone('(11) 92812-1221')
print(c.getTelefone())

Saída:

email@example.com
  (11) 92812-1221

Sobre as regex
As regex usadas acima são relativamente simples, mas elas podem ser melhoradas, se for o caso.
A do telefone, por exemplo, aceita valores como (00) 00000-0000. Se isso é aceitável para os seus testes, tudo bem, mas você pode melhorar e usar as opções desta pergunta.
Já a regex do email aceita valores como _.+@-.-. Claro que dá para melhorar, e eu comento mais sobre isso aqui, aqui, aqui e aqui (este último link tem algumas opções no final, só não recomendo a última regex).
Mas basicamente, quanto mais simples a regex, mais casos "estranhos" ela vai considerar válidos, porém quanto mais precisa, mais complexa ela vai ficando. Escolha o que for melhor para o seu caso, sempre lembrando de balancear a praticidade (se é fácil de entender e manter) e a precisão (ela valida o que eu quero, e também não valida o que eu não quero).

Sobre o nome
O método isalpha não aceita espaços (e acredito que você deve ter testado com nomes que tem espaços). Mas como não foi dito quais valores você usou para testar, podemos especular um pouco.
Podemos usar algo simples como \w+(?: \w+)+. O \w é um atalho para "letras, números ou o caractere _". Esta regex aceita vários \w, seguido de (várias ocorrências de "espaço seguido de vários \w"). Ou seja, nomes contendo vários sobrenomes, todos separados por espaço.
É claro que por ser muito simples, esta regex aceita coisas como 123 a b_. E aqui é a mesma história dos emails e telefones: quanto mais precisa a regex, mais complicada ela vai ficando.
Podemos eliminar os números e o _ e ficar somente com as letras usando [^\W\d_]:

\W é "tudo que não for \w"
\d é qualquer dígito numérico
_ é o próprio caractere _

O [^ serve para negar tudo isso. Ou seja, o resultado é o mesmo que "\w sem os números e o _", sobrando somente as letras. Fiz isso porque no Python 3 o \w considera todas as letras definidas no Unicode, o que inclui caracteres acentuados e outros alfabetos (como o japonês, árabe, cirílico, etc). Então ficaria assim:
def setNome(self, nome):
    if re.match(r'^[^\W\d_]+(?: [^\W\d_]+)+$', str(nome).strip()):
        self.__nome = nome
    else:
        print('\033[1;31m', 'NOME INVALIDO!', '\033[m')

Mas se quiser se limitar ao alfabeto latino e caracteres acentuados, pode usar algo como [a-zçáéíóúãõâêô....] (inclua todos os caracteres que quiser entre os colchetes). E para não ter que repetir as letras maiúsculas, use a flag IGNORECASE:
if re.match(r'^[a-zçáéíóúãõâêô]+(?: [a-zçáéíóúãõâêô]+)+$', str(nome).strip(), flags = re.IGNORECASE):

Essa regex ainda vai recusar nomes que contém apóstrofo (como "D'aquino", por exemplo), mas creio cabe a você decidir o quanto quer "complicar" a regex e o quão precisa quer que ela seja.
Comment: Muitissimo obrigado pela resposta, explicacao e referencias. Grato.
Answer: Tente usar essas regex:
def __validaEmail(self, email):
    result = re.match(r'''(
            [a-zA-Z0-9,_%+-]+   # username
            @                   # @ simbol
            [a-zA-Z0-9.-]+      # domain name
            (\.[a-zA-Z]{2,4}){1,2} # dot-something
            )''', str(email), re.VERBOSE)
    if result == None:
        return False

def __validaTelefone(self, telefone):
    result = re.match(r'''(
            (\d{2}|\(\d{2})\)?  # area code
            (\s|-|\.)?          # separator
            (\d{4})             # first 3 digits
            (\s|-|\.)           # separator
            (\d{4})             # last 4 digits
            (\s*(ext|x|ext.)\s*(\d{2,5}))? # extention
            )''', str(telefone), re.VERBOSE)
    if result == None:
        return False

Comment: Para poder usar uma regex em várias linhas e com comentários, tem que passar a flag [`VERBOSE`](https://docs.python.org/3/library/re.html#re.VERBOSE). Mas o principal problema nem é a regex em si, e sim o fato da função não retornar nada quando `result` não for `None`
Comment: @hkotsubo, entendi. Bom, acho que a resposta de cima vai te ajudar, boa sorte!


After upgrade from Debian 10 Buster to Debian 11 Bullseye security updates 404 not found

Question: Doing recommended upgrade path, TL;DR:
Backup data, then
apt update
apt upgrade
apt dist-upgrade
reboot

Also disable all non main stream packages if any.
Then preparing APT for new distribution:
sudo sed -i 's/buster/bullseye/g' /etc/apt/sources.list
sudo sed -i 's/buster/bullseye/g' /etc/apt/sources.list.d/*

The first command shows error:
apt update
...
Err:6 http://security.debian.org/debian-security bullseye/updates Release
  404  Not Found
Reading package lists... Done
E: The repository 'http://security.debian.org/debian-security bullseye/updates Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

Answer: This is regarding change in Debian 11 repository structure. RTFM requires time, so bump answer here:

For Debian bullseye, the security suite is now named bullseye-security
instead of codename/updates and users should adapt their APT
source-list files accordingly when upgrading.

Thus execute one more command:
sed -i 's/bullseye\/updates/bullseye-security/g' /etc/apt/sources.list

Now apt update works great again, and you can continue with upgrade:
sudo apt update
sudo apt upgrade --without-new-pkgs
sudo apt full-upgrade
reboot

Check if system is upgraded:
lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 11 (bullseye)
Release:    11
Codename:   bullseye

After successful reboot, you can remove old kernel:
sudo apt purge linux-image-4.19*

Also review log files, for error messages of conflicting configurations or other issues.
Issue list is here: https://www.debian.org/releases/bullseye/amd64/release-notes/ch-information.html
Comment: -1 because answer is fragile.  If user has made any manual changes to sources.list, it might not work, and user not given the next thing to try.   c.f. [this answer](https://serverfault.com/a/1100638/288263) which gives what I consider a more robust answer.
Comment: I'd like to think they would just make a simple symlink on the server side and a package to update the apt sources list on the machine side to prevent these kinds of issues...
Comment: I like the new Debian repository structure, much less confusing than buster/updates and buster-updates imho.
Answer: If you followed the various Debian update tutorials out there, chances are you ended up with an incorrect /etc/apt/source.list
As most "fixes" are listed sed command patches, they may not work if your original source.list content isn't a perfect match.
In that case, just edit source.list manually, the new bullseye security lines should read like
deb http://security.debian.org/debian-security/ bullseye-security main
deb-src http://security.debian.org/debian-security/ bullseye-security main

or alternatively if you do not need source and want contrib, it could be just
deb http://security.debian.org/debian-security/ bullseye-security main contrib

Comment: Informative, robust and also correct answer.


Why is this expectation calculation wrong?

Question: Let
$E(X|A)=2+E(X)$
$E(X|B)=3+E(X)$
$E(X)=0.5E(X|A)+0.3E(X|B)$
Now to find $E(X^2)$ I did
$E(X^2|A)=4+E(X^2)$
$E(X^2|B)=9+E(X^2)$
$E(X^2)=0.5E(X^2|A)+0.3E(X^2|B)$
What did I do wrong here?
Edit.
For a recursive conditional expectation like this,
$E(X^2|A)=E[(2+X)^2]$
$E(X^2|B)=E[(3+X)^2]$
$E(X^2)=0.5[4+4E(X)+E(X^2)]+0.3[9+6E(X)+E(X^2)]$
where $E(X)=9.5$
Comment: How did you get the second set of equations?? You can't just replace $X$ with $X^2$, and square some of the constants.
Comment: @CalvinLin The first set is given, the second set I thought you would just square the X and sum it up like E(X)?
Comment: It doesn't work that way. $E(X) = \int x \, dx$, $E(X) = \int x^2 \, dx$. Also, it is not clear what you want to do. Are you trying to find $E[X^2]$? Is $A\cup B$ the entire probability space?
Comment: You can calculate that $E[X] = 9.5$ from the first 3 equations. However, you can't find much about $E[X^2]$ otherwise.
Comment: @calvinlin It's a discrete probability. The probability sapce is A,B,C with $E(X|C)=0$. From what I understand is $E(X^2)=X_1^2P(A)+X_2^2P(B)+0P(C)$
Comment: The discrete version is that $E[X] = \sum x f(x), E[X^2] = \sum x^2 f(x) $. Also, you should mention that $E[X|C]= 0$. Please state your problem properly, we are not mind readers.
Comment: @calvinlin X is a count that goes up until you choose C which it would end, and you would keep going if you choose A and B. $X|C=0$ $X|A=2+X$ $X|B=3+X$.
Comment: YOu should state the problem properly and completely. No one is able to read your mind, nor know what you are trying to talk about.
Answer: This is not an answer, as the question is not stated. I'm merely trying to show what information can be gleamed from the question. I'd delete this answer later.

Judging from your comments, the probability space is $\{ A, B, C \}$. Hence,
$\begin{align}
E[X] & = P(X=X(A) ) \times E[X|A] + P(X=X(B)) \times E[X|B] + P(X=X(C)) \times E[X|C] \\
& = P(X=X(A)) \times X(A) + P(X=X(B)) \times X(B) + P(X=X(C)) \times X(C) \\
\end{align}$
However, we do not know what $P(X=X(A))$ is.
From the first 3 equations, we get that
$$E[X] = 0.5 (2+E[X]) + 0.3 (3 + E[X]) \Rightarrow E[X] = 9.5, E[X|A] = 11.5, E[X|B]=12.5 $$


Alterar a cor de um slot de tempo específico no Fullcalendar

Question: Eu estou trabalhando com a view agendaDay no FullCalendar. Eu tenho uma função que bloqueia quando o usuário clica em slots de tempo específicos, em dias específicos (esses valores são gravados no banco). Gostaria de saber como posso definir uma cor diferente apenas para determinados slots de tempo em um dia, especificamente, para as linhas bloqueadas. Como posso identificar uma linha de tempo específica para mudar a sua cor?
Answer: Talvez utilizar as annotations possa servir para o que você precisa.
Para adicionar as annotations:
<code>$('#calendar').fullCalendar({
    ....
    events: [
        {
            title: 'All Day Event',
            start: new Date(y, m, 1)
        }
    ],
    annotations: [{
        start: new Date(y, m, d, 13, 0),
        end: new Date(y, m, d, 15, 30),
        title: 'My 1st annotation', //OPCIONAL
        cls: 'open', //OPCIONAL
        color: '#777777', //OPCIONAL
        background: '#eeeeff' //OPCIONAL
    }, { proxima annotation }, ...]
});
</code>
Mais exemplos e os arquivos de suporte dessa feature nesse link:
https://github.com/elhigu/fullcalendar/blob/master/demos/annotations.html
Resultado final:
Answer: A solução que melhor atendeu meu problema foi a sugerida neste link do Stackoverflow em inglês: https://stackoverflow.com/questions/29473143/set-different-color-to-specific-time-slots-in-fullcalendar.
Utilizei uma função javascript mais ou menos como a que segue abaixo. Para dar certo é preciso configurar o intervalo dos slots de tempo de modo a aparecer o rótulo da hora para as linhas. Visto que se colocamos a duração dos slots como 00:15:00 ou 00:00:30:00, 00:45:00 os rótulos das linhas não aparecem, deve-se acrescentar 1 segundo ao final da duração do slot: 00:15:01. Segue o código sugerido:
<code>$('tr').find('span').each( function(){
   var timeSlot = $(this).text();
   if(timeSlot> 13 && timeSlot < 18)    //Change 13 and 18 according to what you need
   $(this).closest('tr').css('background-color', '#000');
});
</code>


Scripting query issue in join query

Question: I am facing issue in SQL query and need some help.
I am newbie in Php and sql. So any help would be aprreciated.
I have the following Sql query and tries.
<code> $insert_on_duplicate_update_query = "INSERT INTO test (`store_client_id`,`customer_id`,`first_name`,`last_name`,`email`,`created_on`,`updated_on`,dob) VALUES('1','123','abc','xyz','firstname.lastname@example.com','2018-10-12 00:00:02','2018-10-12 00:00:02','NULL') ON DUPLICATE KEY UPDATE `store_client_id`='1',`customer_id`='123',`first_name`='abc',`last_name`='xyz',`email@example.com',`updated_on`='2018-10-12 00:00:02',dob='null';";

  $insert_on_duplicate_update_query = str_replace(array("'NULL'","'null'"), null, $insert_on_duplicate_update_query);

 echo $insert_on_duplicate_update_query;
</code>
So here i want to replace 'NULL' to NULL in the follow place -
<code> VALUES('1','123','abc','xyz','firstname.lastname@example.com','2018-10-12 00:00:02','2018-10-12 00:00:02','NULL')
</code>
Answer: Just need to change second parameter in str_replace function as bellow.
<code>`$str = "abc = 'NULL'";
$a = str_replace("'NULL'", 'NULL', $str);
echo ($a);`
</code>
Second parameter with '' quotes.
Answer: You need to change in str_replace() function on second line.
It should be as below.
<code>$insert_on_duplicate_update_query = str_replace(array("'NULL'","'null'"), 'null', $insert_on_duplicate_update_query);
</code>
Hope it make sense

Scripting query issue in join query


Simplifying the upper and lower sum of a Riemann Integral

Question: I have been working on solving a sum for a few hours now but I am not getting the answer on my answer sheet (which is $\frac{46}{3}$).
I have two sums:
$16(\sum^n_{i=1} \frac{i^2-2i+1}{n^3}+\frac{i-1}{n^2})+\sum_{i=1}^n\frac{2}{n}$
$16( \sum_{i=1}^{n}\frac{i^2}{n^3}+\frac{i}{n^2})+\sum_{i=1}^n\frac{2}{n}$
both of which should converge to $\frac{46}{3}$ as n approaches infinity.
I know I also need to use this $\sum^n_{i=1} i^2 = \frac{n^3}{3}+ \frac{n^2}{2}+ \frac{n}{6}$ in order to answer the questions.
Could someone please help me do these?  I've tried many different approaches all day with no success.
Answer: Notice that $n$ has no dependence on $i$.
$$  \sum_{i=1}^n \frac{f(i)}{g(n)} = \frac{1}{g(n)} \sum_{i=1}^n f(i)  \text{,}  $$
which gets all the $n$s in the denominators out of the sums...
Then
$$  \sum_{i=1}^n (a i^2 + b i + c) = a\sum_{i=1}^n i^2 + b \sum_{i=1}^n i + \sum_{i=1}^n c  $$
(... since addition is commutative).
Answer: Here's how to do one.  If you follow it, you should be able to do the other.
$$16\left[ \sum\limits_{i=1}^{n}\frac{i^2}{n^3}+\frac{i}{n^2}\right]+\sum\limits_{i=1}^n\frac{2}{n}$$
$$=16\left[\frac1{n^3}\left(\frac{n^3}3+\frac{n^2}2+\frac n6\right)+\frac1{n^2}\left(\frac{n^2}2+\frac n2\right)\right]+2$$
$$\to 16\left[\frac13+\frac12\right]+2=\dfrac{46}3$$
Comment: also used $\sum\limits_{i=1}^n i=\frac{n^2}2+\frac n2$


Next closest date and time in MySQL

Question: Inside of my Auctions table, I have a column called Auction_StartDate. The value of a row is like this: 2012-10-27 13:45:30.
I need a query that will return the next closest date and time after that.  So if the next Auction_StartDate is 2012-10-27 18:30:00, it should return that before the date turns to 2012-10-28.
Comment: Do you just need the next closest date? Or do you need the id of the row with the next closest date? Also, are you saying you only want results on the same calendar day? Or if the next closest was "2012-10-28 05:00:00" would that be acceptable?
Comment: Yes, I need it with the time.
Comment: That's not quite what I asked. Do you need the id of the row with the next closest date? Also does the next closest date have to be on the same calendar day?
Answer: You can use MIN to find the closest value without using LIMIT and ORDER BY clause.
SELECT  MIN(DATE(Auction_StartDate)) closestDate
FROM    Auctions
WHERE   DATE(Auction_StartDate) > '2012-10-27'

SQLfiddle Demo
Comment: Instead of MIN function we can use the LIMIT to optimize the query
Comment: thanks for correcting John, I checked in your fiddle and now saw that you already arranged in the ASC order, so in your case it works perfectly :)
Comment: -1 This won't work in the example provided by the OP. If the next closest date is on the same calendar day this won't return that value.
Comment: @Alec, Just remove `DATE`, do this `MIN(Auction_StartDate) closestDate`
Answer: If you mean to do this for every row, try this:
SELECT a1.id,
    (SELECT MIN(a2.Auction_StartDate)
     FROM Auctions a2
     WHERE a2.Auction_StartDate > a1.Auction_StartDate) AS nextStartDate
FROM Auctions a1

Answer: May be this one helps
    SELECT DATE(Auction_StartDate) closestDate
    FROM    Auctions
    WHERE   DATE(Auction_StartDate) > '2012-10-27'
    order by Auction_StartDate ASC
    limit 1

Comment: you need to add `ORDER BY closestDate ASC`, this will happen [click here](http://sqlfiddle.com/#!2/933ae/1)
Comment: @JohnWoo: We need closes date and DESC will never give us closest, I think it must be ASC :)
Comment: I need the time included in the result.
Comment: @Alec: replace "SELECT DATE(Auction_StartDate) closestDate" to "SELECT Auction_StartDate closestDate", will give you full column value
Comment: @Alec This doesn't actually answer your question. This won't return 2012-10-27 18:30:00 as per the example in your question.
Answer:    SELECT (case when Hour(StartDate)>=12 then DATE_ADD(StartDate,
                  INTERVAL 1 DAY) else StartDate end) as 'date' FROM table
    ------------------------------
    pleaes add your column name where is static date :
    est on : http://sqlfiddle.com/#!2/b8435/19

    SELECT (case when Hour(StartDate )>=12 then
       DATE_FORMAT( DATE_ADD(StartDate ,INTERVAL 1 DAY), '%Y-%m-%d')
       else DATE_FORMAT(StartDate , '%Y-%m-%d') end) as 'date'  from tabel

Next closest date and time in MySQL


How to only rerender react component when specific state changes?

Question: I have a custom input component like this:
<MyComp
   propA={data.someData}
/>

say I render this 10 times on my page, how do I stop it re-rendering unless the specific instance has changed?
Currently when I update the first instance, all the other components will make a graphql request (my component is somewhat complicated) because when they do this they update some state and then that state is what you see in propA which gets passed back down
I know I can wrap it in memo but I'm struggling to see how I say "only re-render if this specific instance changes". I can't do prop comparison because the same data is getting passed down through all the props if that makes sense. I almost need to say "if NOT typing in specific instance, do not call query" not sure how I do that in react though?
is the only way to pull out my state from data and set each set individually?
Comment: Why don't you move the graphql request to a higher level to make it once in a parent component and pass the result in child components?
Answer: you can do a deep comparison, for example using this library https://github.com/kentcdodds/use-deep-compare-effect
useDeepCompareEffect(
() => {
  // make an HTTP request or whatever with the query and variables
  // optionally return a cleanup function if necessary
},
// query is a string, but variables is an object. With the way Query is used
// in the example above, `variables` will be a new object every render.
// useDeepCompareEffect will do a deep comparison and your callback is only
// run when the variables object actually has changes.
[query, variables],

)


python regular expression to parse div tags

Question: a question about python regular expression.
I would like to match a div block like

any news stuff

I was thinking a pattern like
p = re.compile(r'
[^(div)]+
')

but it seems not working properly
another pattern
p = re.compile(r'
[\W|\w]+
')

i got much more than i think, it gets all the stuff until the last  tag in the file.
Thanks for any help
Comment: `[^(div)]` matches any character except 5 listed in brackets. Thus this won't work if any of them could occur in div content. `\W` is an opposite for `\w`, so `[\W|\w]` is meaningless. The simplest way is to use non-greedy regexps like `.+?`. Consider using some real HTML parser if this is not the only task you are going to do with it.
Comment: tried .+? , seemed no luck... Thanks!
Answer: You might want to consider graduating to an actual HTML parser.  I suggest you give Beautiful Soup a try.  There are many crazy ways for HTML to be formatted, and the regular expressions may not work correctly all the time, even if you write them correctly.
Answer: try this:
p = re.compile(r'
.*?
')

Answer: Don't use regular expressions to parse XML or HTML. You'll never be able to get it to work correctly for nested divs.
Comment: Actually, you can, but it's a complete PITA and you're far better off to just use a proper X/HTML parser.
Comment: "Real" regular expressions can't deal with nesting. That's what separates regular languages from context free languages. Most regex implementations are more powerful than strictly regular, but most still aren't powerful enough to deal with nesting.


Exporting rule from sentinel and then using azure devops to deploy ARM template

Question: My task is to deploy ARM template that has been exported from Sentinel. It looks like this:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "22.214.171.124",
    "parameters": {
        "workspace": {
            "type": "String"
        }
    },
    "resources": [
        {
            "id": "[concat(resourceId('Microsoft.OperationalInsights/workspaces/providers', parameters('workspace'), 'Microsoft.SecurityInsights'),'/alertRules/6db8cd19-9b27-4bf0-84f3-2111204460b9')]",
            "name": "[concat(parameters('workspace'),'/Microsoft.SecurityInsights/6db8cd19-9b27-4bf0-84f3-2111204460b9')]",
            "type": "Microsoft.OperationalInsights/workspaces/providers/alertRules",
            "kind": "Scheduled",
            "apiVersion": "2022-11-01-preview",
            "properties": {
                "displayName": "Informational rule 1",
                "description": "Rule to test ARM template deployment",
                "severity": "Medium",
                "enabled": true,
                "query": "AzureActivity\r\n| where OperationName == \"Create or Update Virtual Machine\" or OperationName ==\"Create Deployment\"\r\n| where ActivityStatus == \"Succeeded\"\r\n| make-series dcount(ResourceId)  default=0 on EventSubmissionTimestamp in range(ago(7d), now(), 1d) by Caller",
                "queryFrequency": "PT5H",
                "queryPeriod": "PT5H",
                "triggerOperator": "GreaterThan",
                "triggerThreshold": 0,
                "suppressionDuration": "PT5H",
                "suppressionEnabled": false,
                "startTimeUtc": null,
                "tactics": [],
                "techniques": [],
                "alertRuleTemplateName": null,
                "incidentConfiguration": {
                    "createIncident": true,
                    "groupingConfiguration": {
                        "enabled": false,
                        "reopenClosedIncident": false,
                        "lookbackDuration": "PT5H",
                        "matchingMethod": "AllEntities",
                        "groupByEntities": [],
                        "groupByAlertDetails": [],
                        "groupByCustomDetails": []
                    }
                },
                "eventGroupingSettings": {
                    "aggregationKind": "SingleAlert"
                },
                "alertDetailsOverride": null,
                "customDetails": null,
                "entityMappings": null,
                "sentinelEntitiesMappings": null,
                "templateVersion": null
            }
        }
    ]
}

I was trying to use azurerm template :
resource "azurerm_template_deployment" "template" {
    name = "template-deploy"
    resource_group_name = var.resource_group_name
    template_body = file("${path.module}/ARM_rule_templates/example_rule.json")
    deployment_mode = "Incremental"
    parameters =jsonencode({
        "workspace" = {value = data.azurerm_log_analytics_workspace.log_analytics.name}
         })
}

Is it even possible to deploy that template from azure devops without adding something to it ? Do you have any thoughts how to move on? I am out of hope at this moment.
Regards,
Michal
Answer:
I tried to deploy the ARM template that has been exported by calling with the file URL and I was able to provision the requirement successfully.

To deploy an ARM template that's been exported from Azure Sentinel using Azure DevOps, there are a few key steps and considerations to make. The ARM template you've provided is specifically for deploying an alert rule in Azure Sentinel.
The approach mentioned in the query was possible but the template used i.e.,
resource "azurerm_template_deployment"
was depreciation resource module which doesn't help in provisioning the requirement you are looking for. The resource resource "azurerm_resource_group_template_deployment" will help.
However, the JSON shared in the query was missing with certain parameters we cannot achieve the requirement so I took a demo example JSON to demonstrate how this works.
My JSON file for demonstration:
{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "18.104.22.168",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    }
  },
  "variables": {
    "location": "[resourceGroup().location]",
    "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
    "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
    "publicIPAddressType": "Dynamic",
    "apiVersion": "2015-06-15",
    "dnsLabelPrefix": "terraform-acctest"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[variables('storageAccountName')]",
      "apiVersion": "[variables('apiVersion')]",
      "location": "[variables('location')]",
      "properties": {
        "accountType": "[parameters('storageAccountType')]"
      }
    },
    {
      "type": "Microsoft.Network/publicIPAddresses",
      "apiVersion": "[variables('apiVersion')]",
      "name": "[variables('publicIPAddressName')]",
      "location": "[variables('location')]",
      "properties": {
        "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
        "dnsSettings": {
          "domainNameLabel": "[variables('dnsLabelPrefix')]"
        }
      }
    }
  ],
  "outputs": {
    "storageAccountName": {
      "type": "string",
      "value": "[variables('storageAccountName')]"
    }
  }
}

My Terraform configuration:
provider "azurerm" {
    features {}
}

data "azurerm_client_config" "core" {}

resource "azurerm_resource_group" "example" {
  name     = "demovkr-rg"  # Update this to your desired resource group name
  location = "east us"
}

resource "azurerm_log_analytics_workspace" "example" {
  name                = "demovk-workspace"  # Update this to your desired workspace name
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  sku                 = "PerGB2018"  # Choose an appropriate SKU
}

resource "azurerm_resource_group_template_deployment" "template" {
  name                = "template-deploy"
  resource_group_name = azurerm_resource_group.example.name
  template_content      = file("/home/bolli/demo/demo.json")
  deployment_mode     = "Incremental"

  parameters_content = jsonencode({
    "storageAccountType" = { value = "Standard_LRS" }  // Example parameter, adjust as needed
  })

}

Output:


Java - How to shorten a lengthy String.format so that it is <120 chars

Question: I tried searching for similar threads, but I am not finding much.
I am trying to shorten the following statement so that it is under 120 characters (currently 145):
return (String.format("I am driving a %s and I love it. It has a top speed of %d miles per hour and it is worth %d", carMake, topSpeed, value));
Thank you!
Comment: so it's not about the length of the string but the length of the line of code?!
Answer: You can break the string literal in some lines:
return String.format(
        "I am driving a %s and I love it. " +
        "It has a top speed of %d miles per hour and " +
        "it is worth %d",
        carMake, topSpeed, value);

The format can be changed as desired, but note the empty space on the end of each line (but the last).
The concatenation of string literals is done by the compiler, so there is no performance loss involved when splitting string literals in that way.
Answer: You can also do it like this if you want multiline output.  It became an official construct in Java 15.
String carMake = "Ferrari";
int topSpeed = 100;
int value = 250000;

String s =
        """
        I am driving a %s and I love it.
        It has a top speed of %d miles per hour and
        it is worth $%,d.
        """.formatted(carMake, topSpeed, value);

System.out.println(s);

prints
I am driving a Ferrari and I love it.
It has a top speed of 100 miles per hour and
it is worth $250,000.

Comment: I have not worked with text blocks yet, and I apparently misunderstood what `stripIndent()` was for.  Thanks.
Comment: Isn't this indented unless you trim it?
Comment: No, because they are they are all indented the same.  So the leading white space is incidental white space and is removed.  If the second line was indented an extra 2 spaces, they would print the same as before except the second one would be indented the extra spaces.  Check out [this](https://docs.oracle.com/en/java/javase/13/text_blocks/index.html#incidental-white-space) as a fairly detailed overview.
Answer: Store the string in a variable
String strToFormat = "I am driving a %s and I love it. It has a top speed of %d miles per hour and it is worth %d";
return String.format(strToFormat, carMake, topSpeed, value);

Comment: @vicbur04 that is nothing related at all with Java. That is only a variable store, that exists in most languages ;)
Comment: Thank you! I am still learning how Java works, but hopefully I will get better in the future :)


Trouble with calling a method in Objective C (Apple Documentation example)

Question: I'm following along with Apple's "Programming with Objective C" document, the link being: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html#//apple_ref/doc/uid/TP40011210-CH4-SW1
Anyways, I've gotten to the point where it ask for calling the sayHello method.

"Create a new XYZPerson instance using alloc and init, and then call the sayHello method."

#import <Foundation/Foundation.h>
#import "XYZPerson.h"

int main(int argc, const char * argv[]);

XYZPerson *firstPerson = [[XYZPerson alloc] init]; //Initializer element is not a lime-time constant
[firstPerson sayHello]; //No Visible @interface for 'XYZPerson' delcares the selector 'sayHello'

@implementation XYZPerson
- (void)sayHello {
    [self saySomething:@"Hello, World"];
}

- (void)saySomething: (NSString *)greeting {
    NSLog(@"%@", greeting);
}

@end

I believe I'm having a misunderstanding with how apple is explaining the work or just have no clue.
Wishing apple had these examples done for us to review over.
Comment: Are you implemented the XYSperson.h and .m the say way they did ?
Comment: Your `XYZPerson.m` should *not* have a declaration for `main`. Get rid of that line. Also, why do you have code before the `@implementation` line? That code must not be there.
Comment: This code is in the main.m file while i also have the XYZPerson.h and XYZPerson.m from the last chapter present and why is bad to have it before? The example is asking me to create a instance using allocate and init in the main.m file. Unless I'm doing it wrong?
Answer: As you can only access public functions which are declared in .h file with the class object.

Kindly declare that function in .h file and it will solve your problem
Answer: You need to put the code inside the main function. Right now you have the code just sitting in your file, outside of any function. It should be:
int main(int argc, const char * argv[]) {
    XYZPerson *firstPerson = [[XYZPerson alloc] init];
    [firstPerson sayHello];
}

Also, according to the docs you should have a separate main.m file that has your main function inside of it.
Comment: While everything in your answer is good info, it doesn't actually address the actual problem of the question which is that the method needs to be declared in the .h file for `XYZPerson`.
Comment: Initially declaring it in the interface didn't get rid of the errors but opening the function did, thanks. Also all of this code is in the main.m file if thats what your talking about.


Excluir arquivo.php, juntamente com exclusão de registro no banco de dados, obtendo o nome do arquivo.php por uma variável

Question: Olá, pessoal! Possuo um cadastro com id, titulo, data e evento, no banco de dados, de modo que, ao se fazer um novo cadastro, a pessoa preenche somente o titulo e a data, e o campo "evento" é automaticamente preenchido com o "titulo" que a pessoa escreveu, acrescido de ".php".
Então é criada uma página nova com o mesmo nome do título e a extensão .php, que é acessada em um calendário, de modo que a data cadastrada se torna um link para tal página nova criada.
Então eu criei uma listagem dos dados do banco, com um botão de excluir. Contudo, não consigo, ao apertar este mesmo botão, excluir junto o arquivo.php referente àquele id que será excluído do banco.
o registro é listado em admin.php, onde há o botão de excluir:

<?php

$resgatando_dados = $pdo->prepare("SELECT * FROM eventos ORDER BY data ASC");
$resgatando_dados->execute();
echo '

Id
Título
Data
Encontro
Excluir

';

foreach($resgatando_dados as $apresenta_dados):
echo '

 '.addslashes(trim(strip_tags($apresenta_dados['id']))).'
 '.addslashes(trim(strip_tags($apresenta_dados['titulo']))).'
 '.addslashes(trim(strip_tags($apresenta_dados['data']))).'
 '.addslashes(trim(strip_tags($apresenta_dados['encontro']))).'
Excluir

';
endforeach;
echo '
';

?>

E o arquivo php "deletar_dados" é este:
<?php
include 'conexao.php';
?>

<meta charset="UTF-8">

<?php

$id = strip_tags($_GET['id']);

$encontro = $_GET['encontro'];
unlink ('encontros/'.'$encontro');

$deletando_dados = $pdo->prepare("DELETE FROM eventos WHERE id = '$id'");
$deletando_dados->execute();

if($deletando_dados):
echo '';
//echo '';

else:
echo '';
endif;
?>

*obs: o arquivo a ser excluído está em uma pasta dentro da pasta onde estão os arquivos aqui exibidos (  daí o unlink ('encontros/'.'$encontro');  ).
Considerando que se trata de um problema que articula várias dúvidas frequentes, somando arquivos excluídos em outra pasta, com exclusão automática juntamente com os dados do banco, e ainda cujo nome se obtém por meio de uma variável, penso que a solução será de utilidade para muita gente.
Answer: A variável $_GET['encontro'] é do tipo null, por isso não está encontrando o arquivo e deletando-o.
É necessário que você adicione-a como parâmetro da URL.
Excluir

Porém, o ideal é faze algumas validações antes: verificar se tem permissão para fazer isso; buscar o nome do arquivo no banco de dados ao invés de passa-lo como parâmetro etc.
Quando você pasa essa informação sem validar, dar margens para uma pessoa remover qualquer arquivo do seu servidor.
Comment: Opa! Já melhorou! Obrigado, Valdeir!

Mas outra coisa está errada no meu código que eu não havia percebido...

Agora surge a seguinte mensagem:

"Warning: unlink(encontros/$encontro): No such file or directory in C:\wamp64\www\administrador\calendario\deletar_dados.php on line 19"

Quando eu coloco diretamente o nome da página.php a ser excluída, eu consigo... a exclusão acontece com o arquivo que está dentro da pasta "encontros", que é uma pasta dentro da pasta onde estão os demais arquivos. Mas quando faço a exclusão por meio da variável, então esse erro surge. alguma sugestão aí?
Comment: @A.Goes é porque você está passando a variável entre aspas simples. O correto é `unlink ('encontros/'.$encontro);`
Comment: Pois é! Eu tava olhando isso exatamente agora... perdão pela gafe. E muito obrigado pela rapidez na ajuda!


Provedor de serviços do Registro.BR

Question: Quero me submeter como provedor de serviços no Registro.BR e isso exige que um teste seja feito no ambiente de homologação deles. Estou achando as informações desencontradas, então me restaram algumas dúvidas
1- Eles citam uma aplicação chamada shepp. Pelo que entendi você envia um comando passando algumas parâmetros e recebe um XML como reposta. Entendimento desse shepp é necessário para a realização do teste? Qual a vantagem de utiliza-lo? Li algum colaborador em uma thread do email list dizendo que é totalmente opcional e ele incentiva a utilização do servidor de testes
2- Para ter acesso a esse servidor de teste eu preciso preencher aquele formulário de homologação, é isso? O mesmo formulário vai me gerar as credenciais de teste e mais tarde eu me submeto ao ambiente de homologação?
3- Existe uma biblioteca em C++ chamada libepp-nicbr. Pelo que entendi ela é opcional. O que não é opcional é a utilização de "XML sobre TLS aplicada ao protocolo EPP", certo? Existe alguma vantagem sobre utilizar a biblioteca que eles provem? Eu nao tenho muita experiência com C++, com exceção dos tempos de faculdade. A idéia seria utilizar alguma outra linguagem, como o Ruby, PHP ou  Node para executar um comando dessa biblioteca?
Essa biblioteca não tem documentação, é isso mesmo? Exemplos de respostas eu posso encontrar nesse link, confere? ftp://ftp.registro.br/pub/libepp-nicbr/pt-policy-restrictions-espec.txt
Answer: 1 - O shepp é útil para o teste pois o procedimento de homologação (ftp://ftp.registro.br/pub/libepp-nicbr/pt-epp-accreditation-proc.txt) está descrito em sintaxe do shepp, mas se você não vai usar a libepp-nicbr para integração, fazer a homologação com o shepp te dará a falsa impressão de que está tudo pronto do seu lado.
2 - É necessário preencher o formulário de homologação para acesso ao servidor de testes, mas o servidor de testes é o mesmo do ambiente de homologação.
3 - Se você vai utilizar uma outra linguagem, o que faz mais sentido é usar uma biblioteca nativa de EPP dessa linguagem, mas você terá que implementar as extensões específicas do .br (ftp://ftp.registro.br/pub/libepp-nicbr/draft-neves-epp-brorg-06.txt e ftp://ftp.registro.br/pub/libepp-nicbr/draft-neves-epp-brdomain-05.txt). Uma possibilidade seria pegar esta implementação em PHP que é de um gateway para um plataforma de billing em PHP, e adaptar para o seu uso:
https://github.com/registrobr/whmcs-registrobr-epp
A biblioteca tem sim documentação, é um arquivo doxygen relativo a cada versão que pode ser consultado num browser. Por exemplo, a versão mais recente:
ftp://ftp.registro.br/pub/libepp-nicbr/libepp-nicbr-1.15.tar.gz
Tem sua documentação neste arquivo:
ftp://ftp.registro.br/pub/libepp-nicbr/libepp-nicbr-1.15-doxygen.tar.gz
Minha sugestão é olhar quais os casos de uso que seu projeto requer, e quais os casos de uso que a homologação requer. Os que forem necessários para ambos, desenvolver e utilizar tanto na integração quanto para a homologação. Os que forem necessários apenas para homologação, fazer com o shepp ao invés de desenvolver, mas deixar documentado como utilizar o shepp para os casos não implementados.

Provedor de serviços do Registro.BR


compiler output (.class files) differs if sources were compiled in different directories

Question: I have the following problem: While compiling some set of classes different .class files are generated if compilation was executed in different directories. The diff between generated .class files is following:
1) version:
1062: aload_3
1063: invokevirtual <some_method>
1066: goto 1078
1069: astore 15
1071: aload_3
1072: invokevirtual <some_method>
1075: aload 15
1077: athrow
1078: aload_3
1079: areturn

2) version:
1062: jsr 1076
1065: goto 1084
1068: astore 15
1070: jsr 1076
1073: aload 15
1075: athrow
1076: astore 16
1078: aload_3
1079: invokevirtual <some_method>
1082: ret 16
1084: aload_3
1085: areturn

Above code has the same execution logic. Unfortunately, I have to have explanation why the compiler behaves this way. What is strange, sometimes even when compiling in the same directory, the same sources, a difference between consecutive compilations occurs (always the same, mentioned above).
Any idea what happens?
Thanks in advance for response!
Comment: Are you compiling from the command line then? What are your javac calls?
Comment: Your methods are obviously too long if you have instruction numbers over thousand.
Comment: Which version (or better, versions) of the JDK are you using?  It looks like your second version was created with an older javac than the first one.
Comment: Also, for the next time: have a look at the [markdown editing help](http://stackoverflow.com/editing-help) (the white question mark on orange ground).
Comment: This looks like the typical difference between the old implementation `finally { }` (using subroutines invoked via `jsr`) and the up-to-date implementation, duplicating the code (which seems to be the `aload_3; invokevirtual ;` sequence). In case of `javac`, the switch was made with Java 6.
Answer: Does the class use any methods or classes which have package level scope?  This could account for any differences.
Comment: Why should this influence anything?


Want to make sense of array dimensions in logistic regression algorithms

Question: I am trying to implement a simple logistic regression algorithm from scratch in python (for learning purposes). Every article I've seen online so far presents the following expression for $z$ (argument of the sigmoid function):
$$z = \theta^T\cdot x$$
However when they implement it in code they always use np.dot(X, theta) which is not the same thing. I have carefully tried to trace the dimensions of all the arrays as follows:
Dot product property: $\theta^T\cdot x = x^T\cdot\theta$

$x$ is of dimension $l\times (d+1)$ where $l$ is the number of records and $d+1$ the number of features (including the $x_0$ ones vector).
$\theta$ is of dimensions $1 \times (d+1)$ (every column in $x$ gets a weight, including the ones vector).
$z$ is the dot product of $\theta^T$ and $x$, it will have dimensions $((d+1)\times 1)\cdot(l \times 1) =  ??$ --> DOES NOT WORK!

There appears to be no way to arrange the last statement such that it remains mathematically correct and yielding an array of dimension $l\times1$ or $1\times l$ without fundamentally changing either:

the shape of $x$
the equation itself (as they do in the code)

So which is correct? The code or the mathematical statement? Why are they contradictory? Please help. Thanks.
Answer: So there's a few options here:

the code raises an exception because the shape is wrong;
the code is semantically wrong and computes the wrong value but does not raise an exception;
the code works correctly and the text simply mis-states the shape (equivalently, the code implements a different shape than stated in the text);
both the text and the code are wrong.

We know that logistic regression is just a transformation of a linear model. Therefore, we know that a linear combination of the features is correct. This implies that the code is correct given specific shapes for inputs, and also that the text is correct given a different specific shapes for inputs. Therefore, we can exclude (4).
That leaves explanations (1), (2) or (3). I can't validate whether the code works correctly based on the content of the original post.
If I had to place a bet, my money would be on option (3): the author wrote the code and the text separately, and never bothered to check whether the description matched the code. As you point out, one can rewrite a matrix-vector product using transposes -- it seems that the authors did so but their notation is sloppy.


Time-wise treatment effect / survival analysis

Question: Let's say I have some kind of survival data - i.e. I'm giving a drug that may cause mortality. So I have three patients: A, B and C. All are given the drug at Time t1.
Let's say patient A dies at time t2. Patient B dies at time t3. And Patient C dies at time t100.
Clearly, the likelihood that the drug caused the death of patient A and B is higher than the likelihood that the drug cause patient C's death (i.e. patient C likely died from natural causes whereas patients A and B probably died because of the drug).
Are there any techniques in survival analysis/related fields that allow one to quantify the probability/likelihood that the treatment caused the effect. For example, if I could calculate the probability that the treatment caused the death given that patient was treated at time X and died at time Y.
Thanks!
Answer: I think you're going to struggle with any sort of definitive treatment effect, because you lack a comparison group of any sort. You need not necessarily have a control group - there's plenty of methods in the case-crossover literature for using cases as their own controls during unexposed time periods, but if everyone got the drug at t=1, then no one has unexposed time to act as a control.


How to select different type of columns dynamically to use in where clause?

Question: I have a following table in sqlalchemy:
class FieldType(enum.Enum):
   INT_FIELD = 0
   FLOAT_FIELD = 1
   STRING_FIELD = 2

class EAVTable(Base):
     __tablename__ = 'EAVTable'

     field_name = Column(Stirng, primary_key=True)
     field_type = Column(Enum(FieldType))
     int_field = Column(Integer)
     float_field = Column(Float)
     string_field = Column(String)

This is to model the EAV model which fits my business purpose.
Now to use it easily in the code I have the following hybrid_property.
@hybrid_propderty
def value(self):
    if self.field_type == FieldType.INT_FIELD:
         return self.int_field
    ...

@value.setter
def value(self, value):
    if type(value) == int:
        self.field_type = FieldType.INT_FIELD
        self.int_field = value
    ...

This works fine when I try to get and set the fields in Python code. But I still have a problem:
session.query(EAVTable).filter(EAVTable.value == 123)

This does not work out of the box but I had an idea of using hybrid.expression where we use a case statement:
@value.expression
def value(cls):
    return case(
        [
            (cls.field_type == FieldType.INT_FIELD, cls.int_field),
            (cls.field_type == FieldType.FLOAT_FIELD, cls.float_field),
            ...
        ]
    )

This in theory works, for example, the SQL generated for query session.query(EAVTable.value = 123 looks like:
select * from where case
    when field_type = INT_FIELD then int_field
    when field_type = FLOAT_FIELD then float_field
    when field_type = STRING_FIELD then string_field
    end = 123;

Which semantically looks like what I like, but later I find that the case expression requires all the cases have the same type, or they are cast into the same type.
I understand this is a requirement from the SQL language and has nothing to do with sqlachemy, but for more seasoned sqlalchemy user, is there any easy way to do what I want to achieve? Is there a way to walk around this constraint?
Answer: You could move the comparison inside the CASE expression using a custom comparator:
from sqlalchemy.ext.hybrid import Comparator

class PolymorphicComparator(Comparator):
    def __init__(self, cls):
        self.cls = cls

    def __clause_element__(self):
        # Since SQL doesn't allow polymorphism here, don't bother trying.
        raise NotImplementedError(
            f"{type(self).__name__} cannot be used as a clause")

    def operate(self, op, other):
        cls = self.cls
        return case(
            [
                (cls.field_type == field_type, op(field, other))
                for field_type, field in [
                    (FieldType.INT_FIELD, cls.int_field),
                    (FieldType.FLOAT_FIELD, cls.float_field),
                    (FieldType.STRING_FIELD, cls.string_field),
                ]
            ],
            else_=False
        )

class EAVTable(Base):
     ...

     # This replaces @value.expression
     @value.comparator
     def value(cls):
         return PolymorphicComparator(cls)

This way the common type is just boolean.


Connection between $\nu=\frac{N}{2}-1^\text{th}$ order Hankel transforms and hyperspherically symmetric functions?

Question: In The Transforms And Applications Handbook 2nd edition chapter 9 (Hankel Transforms), Piessens briefly mentions that the Fourier transform of an $N$-dimensional hyperspherically symmetric function has a radial profile given by the Hankel transform of order $\nu=\frac{N}{2}-1$ of the input radial profile.
However, he does not include any citation, and does not prove the result (the chapter is mainly devoted to the case $N=2$). I believe it is wrong, because in the case $N=3$, we have
$$\mathcal{F}\left(f(|\mathbf{r}|),\mathbf{r},\mathbf{k}\right)=\sqrt{\frac{2}{\pi}}\int_0^\infty f(r)r^2 j_0(kr)\,\mathrm{d}r\\=\sqrt{\frac{2}{\pi}}\int_0^\infty f(r)r\frac{\sin(kr)}{k}\,\mathrm{d}r$$
where $j_0$ is the zeroth order spherical Bessel function, whereas
$$\mathcal{H}_{1/2}\left(f(|\mathbf{r}|),\mathbf{r},\mathbf{k}\right)=\int_0^\infty f(r)rJ_{1/2}(kr)\,\mathrm{d}r=\sqrt{\frac{2}{\pi}}\int_0^\infty f(r)\sqrt{r}\frac{\sin(kr)}{\sqrt{k}}\,\mathrm{d}r$$
which are not the same.
Is this an error? If so, what is the correct $N$-dimensional analogue of the Fourier transform of radially-symmetric functions?
Answer: I actually also struggled a lot with this one ;)
The correct formula can be found for example in (Grafakos & Teschl, 2013)
$$\mathcal{F}\left(f(|\mathbf{r}|),\mathbf{r},\mathbf{k}\right)= \frac{2\pi}{k^{N/2-1}}\mathcal{H}_{N/2-1}(f(r)r^{N/2-1},r,2\pi k).$$
Grafakos, L., & Teschl, G. (2013). On Fourier transforms of radial functions and distributions. Journal of Fourier Analysis and Applications, 19(1), 167-179.
Comment: If you could copy the formula from the arxiv paper into this answer (with citation), that would be very useful.  Links can "rot" over time (i.e. webpages get deleted), which means that answers which rely on links for primary content can become outdated.  Copying the relevant formula into this answer enables the answer to be useful past the lifetime of the linked page.


Rails ajax form with join table associations

Question: I am trying to build a 'ajaxy' form between two models in rails. here are the relevant files and code snippets...
my models
ActiveRecord::Schema.define(:version => 20140214134314) do

  create_table "group_shots", :force => true do |t|
    t.integer  "shot_id"
    t.integer  "group_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "groups", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "shots", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

end

app/views/groups/show.html.erb
  Name:
  <%= @group.name %>
<%= link_to 'New Shot', new_shot_path, id: "new_shot", remote: true %>

app/views/shots/new.js.erb
$('#new_shot').hide().after('<%= j render("form") %>');

app/controllers/shots_controller.rb
 def create
    @shot = Shot.new(params[:shot])

    respond_to do |format|
      if @shot.save
        GroupShot.create! :shot_id => @shot.id

 def new
    @shot = Shot.new
 end

My end goals is when a user is viewing a group (show action/view) that they can add a shot that is associated with that group, and consequently when the user adds the shot, the correct entries are made in the group_shot table.
You can see in the shots controller, when the shot is saved I am creating a new entry in the group/shots table successfully. I am getting the shot_id, but not the group_id. So that is the question, how do I get the group_id in this view/controller. Any help appreciated.
Answer: My suggestion is to pass the group_id as a parameter to the call to new_shot_path in your groups_controller#show view:
new_shot_path(group_id: @group)

then include it as a hidden field in the form you're using in your shots_controller#new view, using hidden_field_tag:
<%= hidden_field_tag 'group_id', @group.id %>

and then include it in your call to GroupShot#create in the shots_controller#create method:
GroupShot.create! :shot_id => @shot.id, :group_id => params[:group_id]

Just keep in mind that you're doing two things in shots_controller#create -- both creating the shot and associating it back to the group, which is perhaps a little unorthodox.  From your code, it appears that a Shot is never created without an associated Group.  If your starting point is always from viewing groups (then adding shots to a selected group), you may want to consider moving this to the groups_controller as something like #add_shot, but that's going to be depend on how the rest of your application is architected.
Comment: @group won't carry over automatically. You have to pass it from the 'new' form to 'create' - I usually do this with a hidden_field_tag.  In the form you're using in 'new' add a hidden_form_tag, name it group_id and give it the value of @group.id.  Then, in your show#create method, pull it out of the params hash `group_id = params[:group_id]`.  In your call to GroupShot.create, replace `:group_id => @group.id` with `:group_id => group_id`
Comment: Thanks guys, I don't this is going to work. Note while this in the groups view, all of the logic is happening in the shots_controller, but cause thats where the ajax/partials are being called from. The group has already been created, I just need to somehow get the id.
Comment: Then I think you're going to have to pass it as a param in the call to new_shot_path in groups/show.html -- `new_shot_path(group_id: @group)`. But I believe you will have to modify the route to recognize the parameter if you do this.
Comment: Thats a good call, and definitely gets me closer. Here is what I have now http://pastie.org/8734318. I am able to create @group in the new action, but this does not persist over to create. I am going to keep looking at it. So close!!!
Comment: Wow...this is terrible. I feel like a need to go take a shower. Here is what I ended up with: http://pastie.org/8734492 This feels dirty. Does ajax always feel this dirty in rails?
Comment: Well, using hidden fields always feels a little unclean to me, but bear in mind you're really doing 2 things in the Shot#new method - creating the new shot and associating it with a group -- that's why you're having to do something unorthodox with the hidden field.  Doing this straight from the groups_controller with something like #add_shot would probably be cleaner, but it sounds like you have implementation reasons to not want to do that.
Comment: OK thanks. My ajax and js stink, so its a learning process. This was just a test app to experiment with. Hopefully my implementation in my production app will be cleaner. If you want to edit your answer above I will accept it. Thanx for the help!


undefined angles with arcsin

Question: I have this problem but I couldn't solve it. In a paper I'm reading for controlling a device, I need to generate the following angle
$$
\theta = \tan^{-1}\left( \frac{Y_{2} - Y_{1}}{ X_{2} - X_{1}} \right)
$$
where $Y_{2} = 10, Y_{1} = 0, X_{2} = 10$ and $X_{1} = 0$.
Now I need to generate the following angle
$$
\phi = \sin^{-1} ( 0.401*\sin(\theta) - 2.208*\cos(\theta) )
$$
where $\theta = 0.7854$ (rad). The next angle $\psi$ is then generated as follows
$$
\psi = \sin^{-1} \left( \frac{0.401*\sin(\theta) + 2.208*\cos(\theta)}{\cos\phi} \right)
$$
In my code, both angles $\phi$ and $\psi$ are undefined. I know that $\phi$ should be wrapped so that $\cos\phi \neq 0$ to avoid singularity but the problem $\phi$ is already undefined.
Comment: How can $\theta = 0.7854$ if $\theta = 1$?
Comment: @GFauxPas, sorry I fixed it.
Comment: isn't $\phi$ defined by $\sin^{-1} ( 0.401*\sin(\theta) - 2.208*\cos(\theta) )$?
Answer: When $\theta=0.7854\approx \frac{\pi}{4}$, $\sin{\theta}=\cos{\theta}=\frac{\sqrt{2}}{2}$. Evaluating your definition for $\phi$ at $\frac{\pi}{4}$ comes out to $-1.278$, which is not in the domain of $\sin^{-1}{\theta}$ (because it is not in the range of $\sin{\theta}$). The problem seems to be in the part of your expression for $\phi$ that is inside the inverse sine function, since it is generating values that are not in $[-1,1]$.

undefined angles with arcsin


Android: How to Encrypt and Decrypt a resource file and use in App

Question: I need to store resource files(text doc,imgaes,music etc) in my app in encrypted format and in runtime i need to decrypt the files and use it. Thanks in advance.
Comment: Why do you need to do this? Remember that typically, you don't gain any security this way. Also, try and show some research effort in your question.
Comment: Yes i'm trying to protect the resource files. Then how can i achieve this? Thats what i'm trying to find
Comment: Take a look at these questions:
http://stackoverflow.com/questions/4275311/how-to-encrypt-and-decrypt-file-in-android
Answer:
Then how can i achieve this?

If you do not want the user to have access to the data, do not store it on the user's phone.
A sufficiently interested user can access anything on their device. This would include your code along with your resources. Any encryption algorithm and key that you use will be in your code; finding that and using that to decrypt your resources is not especially difficult.
Comment: yes i know that but how can we avoid easy access like simply unzipping the apk to view the resources in the app.
Comment: I have 1 more questions other than this one i will raise this separately 1) How to create app using ARM instruction for android like media player etc? Please guide me in this where to start any tutorials


Webpack - how can I merge/load .js files in specific order?

Question: I've been looking everywhere for an answer to this and I just can't seem to find one.
It's a simple (it seems) issue. I want to be able to merge .js files in the order they are appear in i.e. if I have this in my index.js (JSX), I would like them to be loaded in the order listed: -
require('script1.js');
require('script2.js');
require('script3.js');

However, no matter what I try, webpack seems to just load them in whatever order it wants. The obvious example of this is so I can load jQuery first to expose $ to preceding scripts. However, I would just like to know how to do this in general as I am building a rather complex app.
I hope this has not been answer somewhere before and I have missed it... this is driving me INSANE. I am used to work with Gulp SASS, which was so much easier.
If it makes any difference, I am using this with ReactJS/ReactDOM.
Answer: This is a fundamental misunderstanding of how require (and ES2015's import, which Webpack also partially supports) works.
Instead of manually maintaining the order of your scripts, the point of require/import is to free you from having to do this manual maintenance of script order.
Instead, each script (module) declares its dependencies via require/import, and then the environment (Webpack, in your case) works out the dependency graph and ensures the files are loaded in the correct order (and in the case of true import, does clever things around live bindings).
Your jQuery example would work like this: Any of your scripts (modules) that requires jQuery does this:
var $ = require("jquery");

or
import $ from "jquery";

or similar.
That makes jQuery's main function available as $ within that module. It also tells Webpack that that module requires jQuery. So it loads the scripts in the correct order to make that happen.
More in the Webpack Introduction.
Comment: A (belated) thanks for your response... had to take time away from dev but I am back on it now. Regarding what you wrote... does this apply only to the NPM install of jQuery? I have tried 'import $...' from a file in my scripts folder for jQuery slim but I am getting the $/jQuery is not defined error. Hoping this can help me better understand how Webpack makes function available.
Comment: @U4EA: I think there's some Webpack-specific configuration required which I'm not up-to-speed on.


Multiple tables JOIN with WHERE clause including rows that haven't common id

Question: I suppose this topic has been discussed already here. However I couldn't find something useful for my issue, so links are welcomed.
There are three tables. Table test2 have two foreign keys for tables test1 and test3.
    test1                          test2                               test3
+----+------+           +----+-------+-------+----+             +----+------------+
| id | name |           | id | t1_id | t3_id | v2 |             | id |   co_date  |
+----+------+           +----+-------+-------+----+             +----+------------+
|  1 |   A  |           | 1  |   3   |   1   | 15 |             |  1 | 2018-12-02 |
|  2 |   B  |           | 2  |   4   |   1   | 20 |             |  2 | 2019-02-03 |
|  3 |   C  |           | 3  |   9   |   2   | 35 |             |  3 | 2019-03-04 |
|  4 |   D  |           | 4  |   5   |   3   | 12 |             |  4 | 2019-04-05 |
|  5 |   E  |           | 5  |   6   |   3   | 12 |             |  5 | 2019-09-01 |
|  6 |   F  |           | 6  |   9   |   4   | 20 |             |  6 | 2019-10-02 |
|  7 |   G  |           | 7  |   6   |   5   | 10 |             |  7 | 2019-11-03 |
|  8 |   H  |           | 8  |   7   |   5   | 10 |             |  8 | 2019-12-04 |
|  9 |   I  |           | ....................... |             |  9 | 2020-02-05 |
| 10 |   J  |           | 20 |   4   |  10   | 30 |             | 10 | 2020-03-06 |
+----+------+           +----+-------+-------+----+             +----+------------+

I want to sum all v2 values from test2 by names within certain period. The problem is that some names are not presented in that period. And they are excluded from query result.
I've tried to use LEFT-hand join, but it doesn't work that way.
SELECT t1.name, SUM(v2) FROM test1 as t1
LEFT OUTER JOIN test2 as t2
  ON t1.id = t2.t1_id
LEFT OUTER JOIN test3 as t3
  ON t2.t3_id = t3.id
WHERE co_date BETWEEN '2019-01-01' AND '2019-12-31'
GROUP BY name
ORDER BY name

The following result doesn't include 'A' to 'D' names.
+------+---------+
| name | SUM(v2) |
+------+---------+
|   E  |    26   |
|   F  |    37   |
|   G  |    35   |
|   H  |    10   |
|   I  |    90   |
|   J  |    20   |
+------+---------+

But I expected that they should be with NULL in other fields. Isn't this the way how LEFT JOIN works: all rows from left table and intersections with other tables?
Also, I have tried Cartesian product, but I don't know how to group it correctly.
Here's the demo.
I will appreciate any advice about how should I think here.
Answer: You need to include the date filters in the ON clause:
SELECT t1.name, SUM(v2)
FROM test1 t1 LEFT JOIN
     test2 t2
     ON t1.id = t2.t1_id LEFT JOIN
     test3 t3
     ON t2.t3_id = t3.id AND
        t3.co_date BETWEEN '2019-01-01' AND '2019-12-31'
GROUP BY t1.name
ORDER BY name;

The WHERE clause turns the outer join into an inner join -- which is why you lose rows.  It does so because NULL fails the comparison in the WHERE clause filtering the "outer joined" results.
Comment: @Gennadiy: They don't do so explicitly, but they mention that the outer joined columns are null, so `WHERE t3.co_date BETWEEN DATE '2019-01-01' AND DATE '2019-12-31'` must necessarily dismiss these rows.
Comment: Why the one doesn't tell about it in various JOIN types descriptions?.. Thank you, Gordon.


Fatal error: Call to undefined function db_transaction()

Question: I am new to drupal. In an attempt to achieve transaction, I get this fatal error. I am using drupal 6.22 and MySQL. db_query() and all the other database related functions are working fine, but db_transaction() alone fails. What am I doing wrong here? Any help is appreciated.
$transaction = db_transaction();

try {
       $result = db_query($query);
}
catch (Exception $e) {
       $transaction->rollback();

}

Answer: There is no fuction db_transaction() for Drupal 6 (see Drupal 6 API search).
It is available for Drupal 7 though: db_transaction()
Comment: so, is there any way to achieve transaction in drupal 6?
Comment: Yes, as mentioned by Berdir in his answer
Answer: The DB API in Drupal 6 does not have support for transactions.
You can however use the Backport of the Drupal 7 DB API: http://drupal.org/project/dbtng


Matrix view with content as columns and taxonomy terms as rows

Question: I need to build a view in a matrix format. I have a content type mycontent and a taxonomy vocabulary myvocabulary. The content type has a term reference field in it, which refers to the vocabulary and can have multiple values.
I want to list all contents in a view as columns, and all terms in the vocabulary as a table header, i.e. columns. I want to put a check mark to the corresponding field in the table according to the terms that the content has.
An illustration of what I want to achieve:
+-----------+--------+--------+--------+--------+-----
|           | Term 1 | Term 2 | Term 3 | Term 4 | ...
+-----------+--------+--------+--------+--------+-----
| Content 1 |   x    |   x    |        |   x    |
+-----------+--------+--------+--------+--------+-----
| Content 2 |        |        |        |   x    |
+-----------+--------+--------+--------+--------+-----
| Content 3 |   x    |        |   x    |        |
+-----------+--------+--------+--------+--------+-----
| Content 4 |   x    |    x   |        |        |
+-----------+--------+--------+--------+--------+-----
|    ...    |        |        |        |        |

So, my challenge is to dynamically create a column for each taxonomy term in the vocabulary. I.e. if I add a new taxonomy term, the view should have this column automatically added to the table.
With the default table display I can list the nodes as rows, but it is not possible to make a column for each term in the vocabulary.
I have found 3 different modules which seem to related to my problem:

Views Crosstab
Pivot Tables for Views
Views Matrix

None of those modules have a stable version for Drupal 7, nor they have a good documentation, if any at all.
I am going to download and install the dev versions of each module and play around. However, before I do that I wanted to ask if anyone has done such a view and if there would be another better approach to achieve such a matrix view.
Other suggestions that doesn't include using Views are also welcome.
Comment: Flipping would put "content" to the top and "terms" to the left, which isn't the goal here.
Comment: I think this is possible with views module's table display. Please see http://knackforge.com/service/professional-support-quiz. But would like to know about your opinion on this before I make any suggestions.
Comment: With the table display, the columns are the view fields. That means in theory I should add a new field for each term. However, I want to have the columns are also generated from the taxonomy terms. For example, if I a new term is added to the vocabulary, the view should have a new column automatically.
Comment: @ЕлинЙ. I think you can play with slick grid https://drupal.org/project/slickgrid
Comment: @Bala I've taken a look at the Slick Grid, but I didn't see any point on its description that it would suit my needs. I think I didn't express well enough what I want to achieve and where the challenge is. I'll update my question to make it clearer.
Comment: What about the [Views Flipped Table](https://www.drupal.org/project/views_flipped_table) module? Here is a quote about it (from the module's project page): > ... provides a views table style with rows and columns flipped. This is useful for views showing few entities with many fields, such as product comparison.
Comment: Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference.
Comment: This module was already suggested, and as @Kari Kääriäinen also stated, it only flips the table and nothing else.
Comment: I have found these three modules now have stable versions except crosstab - [Views Crosstab](https://drupal.org/project/views_crosstab) - [Pivot Tables for Views](https://drupal.org/project/views_pivot) - [Views Matrix](https://www.drupal.org/project/views_matrix) Croostab and Pivot (especially pivot) solves the problem of the OP
Answer: It can be achieved with the help of hook_views_pre_view() and the Views field template file.
Create a View
Create a view showing content in table format and add two fields:
Content: Title (Title)
The title will populate each row of the first column.
Content: All taxonomy terms (All taxonomy terms)
select: Exclude this field from display
select: Limit terms by vocabulary and tick the vocabulary you want to use.
Your module file
Create a column for each term and populate all the fields with the value from the field Content: All taxonomy terms (All taxonomy terms) Then we'll be able to check if a comma-separated list of terms contains the term of the current column.
/**
 * Implements HOOK_views_pre_view().
 *
 * Adds a field to the "YOUR VIEW NAME" view.
 */
function YOURMODULE_views_pre_view(&$view) {

  if ($view->name == 'YOUR_VIEW_NAME') {
    // the vocabularies defined in the view.
    $vocabularies = array_filter($view->display['default']->handler->options['fields']['term_node_tid']['vocabularies']);
    $terms = YOURMODULE_terms($vocabularies);
    $count = count($terms);
    for ($i=0;$i<$count;$i++) {
      // define the field id.
      $view->display['default']->handler->options['fields']['tid_' . $i]['id'] = 'tid_' . $i;

      // define the table (it has to be the same table for all fields)
      // if you aren't sure what the table name should be, uncomment the line below and take a look at the first field data.
      // debug($view->display['default']->handler->options['fields'], 'VIEW FIELDS', TRUE);
      $view->display['default']->handler->options['fields']['tid_' . $i]['table'] = 'node';

      // here we need to define our custom field output
      // the field "Content: All taxonomy terms (All taxonomy terms)" has a mashine name 'term_node_tid'
      // this field need to be excluded from display because we do not want to display one column only.
      // Instead we will use the data to populate all our custom fields, then we'll be able to define the output for each of them.
      $view->display['default']->handler->options['fields']['tid_' . $i]['field'] = 'term_node_tid';

      // define the field label
      $view->display['default']->handler->options['fields']['tid_' . $i]['label'] = $terms[$i];

      // we need only term names, not links.
      $view->display['default']->handler->options['fields']['tid_' . $i]['link_to_taxonomy'] = 0;
    }
  }
}

A helper function that returns an array of terms. This function will be used twice. First time in hook_views_pre_view() where the argument is available from the View. Second time in YOURMODULE_has_term() where the argument would be hardly to get. That's why I decided to use a static variable here.
/**
 * Helper function that returns an array of terms.
 *
 * @return array
 *   An array of terms from all vocabularies (defined in the view).
 */
function YOURMODULE_terms($vocabularies = NULL) {
  $terms = &drupal_static(__FUNCTION__);
  if (!isset($terms)) {
    $terms = array();
    foreach ($vocabularies as $key => $vocabulary) {
      $vid = taxonomy_vocabulary_machine_name_load($vocabulary)->vid;
      $taxonomy_tree = taxonomy_get_tree($vid);
      foreach ($taxonomy_tree as $term) {
        $terms[] = $term->name;
      }
    }
  }
  return $terms;
}

A helper function to determine if a field value (comma-separated list of terms) contains the term of current column. This function will be used in a Views field template file.
/**
 * Determines if a comma-seperated list of terms contains the term of the
 * current column.
 *
 * @param $field_alias string
 *   The field alias corresponding to the $field->field_alias value in the view.
 * @param $field_position integer
 *   The column corresponding to the $field->position value in the view.
 * @param $output string
 *   Comma seperated list of terms.
 * @return 'X' if TRUE or bool if FALSE.
 */
function YOURMODULE_has_term($field_alias, $field_position, $output) {

  // because we are using two fields to provide the data
  // we need to skip two positions to get appropriate term keys.
  $field_position = ($field_position > 1) ? $field_position-2 : $field_position;

  // because this is a second call for this function,
  // we don't need to define vocabularies as an argument.
  $terms = testmodule_terms();

  // each column has their own term name.
  $term_name = $terms[$field_position];

  // first column fields.
  if ($field_alias == 'node_title') {
    return $output;
  }

  // the fields in all terms columns.
  else {

    // explode terms and trim whitespaces.
    $term_names = array_map('trim',explode(",",$output));

    // check the term names array contains the term of the current column.
    if (in_array($term_name, $term_names)) {
      return 'X'; // check mark
    }
    return FALSE;
  }
}

Views field template file
Create a View template file views-view-field--[YOUR-VIEW-NAME].tpl.php and place in your templates folder.
<?php
// We're assuming that this view only has two fields (Content: Title)
// and  Content: All taxonomy terms (All taxonomy terms).
// All other fields are generated by the HOOK_views_pre_view() function in the
// `YOURMODULE.module` and those should be passed through the
// `YOURMODULE_has_term` function which determines if the $output
// (comma-seperated terms) contains the column term name.
print YOURMODULE_has_term($field->field_alias, $field->position, $output);
?>

I was able to answer this question thanks to a similar post here and added lots of comments to the code. Hope it helps.
Comment: Thanks a lot Jack-PL. Actually I have already solved this problem using [Views Matrix](https://drupal.org/project/views_matrix). However, your answer look very reasonable and I'll definitely test it on this weekend and accept your answer.
Comment: You're welcome. Please don't forget to share your opinion after testing. It might be useful for the others, including myself :)
Comment: I've just tested your answer and it works. I've already awarded you the bounty. However, as I already stated, I've done it with [Views Matrix](https://drupal.org/project/views_matrix) and it required no template hacks.
Answer: I solved this issue using Views Matrix module. Although, as I stated in my question, it has only a dev version, it is working without problems.

Views matrix provides a matrix style plugin for Views. This overrides pager settings to display items grouped by a row and column header field. Row items that match a row and column value are grouped into the same cell.

The steps how I did it:

Create a view showing content in Matrix format, which is provided by Views Matrix module.
Add the Relationship to the taxonomy term Content: Term reference field (field_my_term_reference)
Add fields Taxonomy term: Name and Content: Title and exclude them from display, they are for the table headers.
Add more fields to display in table content, or a Global: Custom text field for just showing an "X" or a check-sign or whatever else..
Under Format -> Settings, select for column header Content: Term reference field and for row header Content: Title


Nome de propriedade dinâmica em Javascript

Question: Tenho o seguinte código:
var nomePropriedade = "Propriedade1";
var meuObjeto = {
    "nome" : "valor"
}

Gostaria que o nome da propriedade de meuObjeto recebesse o valor da variável nomePropriedade.

Assim, ao tentar acessar as propriedades de meuObjeto eu teria que fazer:
meuObjeto.Propriedade1

E o retorno seria: "valor". Ou seja, seria o mesmo que:
var meuObjeto = {
    Propriedade1 : "valor"
}

É possível fazer isso?
Answer: Para criar uma propriedade com um nome dinamico como sugeres podes usar colchetes assim:
var nomePropriedade = "Propriedade1";
var meuObjeto = {
    "nome" : "valor"
}
meuObjeto[nomePropriedade] = 'foo';

Se quiseres atribuir o valor de nome será
meuObjeto[nomePropriedade] = meuObjeto.nome;

Se o que pretendes é substituir a propriedade nome então isso não dá, ou seja rename não dá. Tens de criar nova propriedade e fazer delete da anterior.
Neste caso de substituição deves fazer:
meuObjeto[nomePropriedade] = meuObjeto.nome;
delete meuObjeto.nome;

Nota:
Na nova versão ECMAScript 6 (ECMAScript 2015) já é possivel defenir objetos com parâmetros dinâmicos no momento da criação do Objeto. Chama-se a isto [computed prope
Exemplo:
function foo() { return 1; }
var o = {
  [foo() + 1]: 'foo bar'
};
console.log(o); // dá: Object {2: "foo bar"}

Answer: Se pretende apenas criar e acessar propriedades variáveis, ou seja, criar e acessar propriedades de um objeto dinamicamente, basta usar os braquetes:
var a = 'foo';
var b = 'bar';

var myObj = new Object;
myObj[a] = b;

console.log( myObj[a], myObj.foo );

Answer: Como complemento das respostas anteriores, também é possível verificar, dinamicamente, se a propriedade foi definida, da seguinte forma
var nomePropriedade = "Propriedade1";
var meuObjeto = {
    "nome" : "valor"
}

if (meuObjeto.hasOwnProperty(nomePropriedade)) {
    // Sim, possui a propriedade
}

Comment: Wallace, só gostaria de fazer uma resalva, `hasOwnProperty` vai retornar falso caso a propriedade exista no prototipo e não no objeto, neste caso um `typeof meuObjeto[nomePropriedade] !== 'undefined'` funcionaria, certo que duvido que o AP vá precisar usar Contrutores e Prototipos. Você pode ver o que estou apontando neste fiddle: https://jsfiddle.net/6nxmvsLx/


$ABAB=0_{n\times n}$, then must $BABA$ be $0_{n\times n}$?

Question:
Let $A$ and $B$ be $n\times n$ matrices such that $ABAB=0_{n\times n}$.  Can we conclude that  $BABA$ must be  $0_{n\times n}$?

I fail to give a counterexample when $n=2$, so I guess the answer is "yes"
Comment: This is a duplicate question, but I have difficulty locating the question it is duplicate of (maybe it used different letters).
Answer: This is Putnam 1990-A5, and there are counterexamples for every $n\ge 3$, i.e.,
$$
A=\begin{pmatrix} 0 & 0 & 0 \cr 0 & 1 & 0 \cr 0 & 0 & 1\end{pmatrix},\; B=\begin{pmatrix} 0 & 1 & 0 \cr 0 & 0 & 1 \cr 0 & 0 & 0\end{pmatrix},
$$
Then $ABAB=0$, but $BABA=\begin{pmatrix} 0 & 0 & 1 \cr 0 & 0 & 0 \cr 0 & 0 & 0\end{pmatrix}$.
Answer: My idea for a counterexample: For $n=3$: take $A$ to be the homomorphism that kills the first line of a vector, $B$ the homomorphism that moves every line one up, but the first line which will stay in place. This can be generalized for $n \geq 3$.
Edit: As I was typing this question was still without answer. Of course the answer above is just the matrix version of what I wrote (with the slight difference that above $B$ actually 'kills' the first line)
Answer: For $n = 2$: The characteristic polynomials of $AB$ and $BA$ are the same (for any $n$). Therefore, if $AB$ is nilpotent then $BA$ is also nilpotent. So, we necessarily have $BABA = (BA)^2 = 0$.


Why is gunicorn calling sys.exit(1)?

Question: Gunicorn is sometimes crashing my server, actually  exiting the Python interpreter with sys.exit(1)! Why is this? Note that the failure is not always at the same point. In the two cases shown below, there is a different last codeline before  gunicorn's exit.  This code running here is openpyxl , which should not be causing interpreter shutdown!
Is the server running out of memory? Some other cause?
(This is Flask on Gunicorn on Docker in Google Container Engine.)
Case 1
  File "/virtualenv_for_docker/lib/python3.7/site-packages/openpyxl/descriptors/base.py", line 166, in __set__
    super(Bool, self).__set__(instance, value)
  File "/virtualenv_for_docker/lib/python3.7/site-packages/gunicorn/workers/base.py", line 196, in handle_abort
    sys.exit(1)
SystemExit: 1

Case 2
  File "/virtualenv_for_docker/lib/python3.7/site-packages/openpyxl/descriptors/serialisable.py", line 164, in __eq__
    def __eq__(self, other):
  File "/virtualenv_for_docker/lib/python3.7/site-packages/gunicorn/workers/base.py", line 196, in handle_abort
    sys.exit(1)
SystemExit: 1

Comment: Looks like the server is catching a SIGABRT: https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/base.py#L169-L195
Comment: Hello. I am currently facing the same issue, can you please tell what was your solution?
I tried increasing the timeout parameter value but still faced the same issue. Running Flask + Gunicorn on Azure Container Instance. My processing time isn't that long either.
Comment: The solution is to ensure that the request completes faster. However, if "processing time isn't that long" then your problem may be different.
Answer: As wrote @maxm the server is catching a SIGABRT, that call generally happens on timeout.
You should increase the timeout value or reduce the request processing time. Also you can setup the signal handler to try to log what happened in the worker after a timeout.
Comment: If you cannot reduce processing time, you can set a timeout for the function like [this](https://stackoverflow.com/a/14924210/9070016)

Why is gunicorn calling sys.exit(1)?


How can I compare a not-continuous metric from two samples?

Question: Let's say I have a store and I want to know which people buy the most. I decide to divide my customers between young people (under 40) and old people (above 40). I have a data frame and in each of its row there is the individual name, id and the information if he bought anything (1) or no (0). How can I know if the difference between the number of people who bought is significant? Is it by using a t-test? And what would be the input here?
AFAIK, the t-test compare two means, so I can't see how would that make sense for a binary variable.
Comment: See [this Q&A](https://stats.stackexchange.com/questions/90893/why-use-a-z-test-rather-than-a-t-test-with-proportional-data?noredirect=1&lq=1) for a discussion of z vs t test for proportions.
Answer: You could look at $2 \times 2$ table with columns Y and O for age
groups and rows P and N for whether a purchase was made.
          Y     O     Tot
 P       35    43      88
 N       98    82     180
 Tot    143   125     268

To see
if the proportions of sales for Y and O groups are significantly different, you have your choice of several kinds of tests. The following ones are shown here:

If you have lots of data you could do a chi-squared test.
With fewer observations, you might want to use Fisher's exact test.
(If you get an error message from a chi-squared test, saying that you
don't have enough data, then use Fisher's exact test.)
A test of two proportions of purchases.

Here is how to run the various tests in R. Most of the tests can use a matrix of the data as follows:
MAT = matrix(c(35, 53, 98, 82), byrow=T, nrow=2)
MAT
     [,1] [,2]
[1,]   35   53
[2,]   98   82

Chi-squared tests. Here are two slightly different versions of essentially the same test.
chisq.test(MAT)

        Pearson's Chi-squared test
        with Yates' continuity correction

data:  MAT
X-squared = 4.5194, df = 1, p-value = 0.03351

I don't believe it is  good idea to do the Yates correction
for samples as large as this. The correction can make the
chi-squared statistic smaller, and thus the P-value larger.
Without correction, the test is as follows:
chisq.test(MAT, cor=F)

         Pearson's Chi-squared test

data:  MAT
X-squared = 5.0894, df = 1, p-value = 0.02407

Fisher's exact test. This test uses the row and column totals and (and one of the four individual counts) along with a
hypergeometric distribution. Before modern computation,
Fisher's test was used mainly for small counts because
of difficulties computing cumulative hypergeometric probabilities.
Typically, as here, the P-value for Fisher's exact test
is between the P-values for the chi-squared test, with and
without the Yates correction.
fisher.test(MAT)

       Fisher's Exact Test for Count Data

data:  MAT
p-value = 0.02729
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.3177046 0.9568553
sample estimates
odds ratio
 0.5537864

Test of two proportions. As implemented in R, prop.test is a test to see if two proportions
$\hat p_y = 35/143 = 0.245$ and $\hat p_o = 53/135 = 0.424$ are consistent with each other or
significantly different.
This test, comparing proportions of people in each age group who
made purchases, may be easier to explain in a report for
non-statisticians.
Notice that the two numbers of purchases and the two group sizes
are used for the data in this test. [As of 5/10/20, instructions in the R documentation for prop.test give information about the use of a matrix that is incompatible with R version 3.4.4.]
prop.test(c(35,53), c(143,135))

    2-sample test for equality of proportions
    with continuity correction

data:  c(35, 53) out of c(143, 135)
X-squared = 6.3486, df = 1, p-value = 0.01175
alternative hypothesis: two.sided
 95 percent confidence interval:
  -0.26344067 -0.03223402
sample estimates:
   prop 1    prop 2
0.2447552 0.3925926

Without the continuity correction, the P-value is a little smaller:
prop.test(c(35,53), c(143,135), cor=F)$p.val
[1] 0.008081579

For comparison, here is a test of two proportions from Minitab statistical software, which uses a normal approximation, seemingly with no continuity correction. [Computations agree with the formula given in the
NIST link near the beginning of this Answer.]
Test and CI for Two Proportions

Sample   X    N  Sample p
1       35  143  0.244755
2       53  135  0.392593

Difference = p (1) - p (2)
Estimate for difference:  -0.147837
95% CI for difference:  (-0.256240, -0.0394342)
Test for difference = 0 (vs ≠ 0):
    Z = -2.67  P-Value = 0.008

Note: I am just showing you tests in R that are appropriate for your data.
I am not advocating that you should do them all.
For your own data, you should decide which one test to do.
The P-values of these tests will generally be pretty
much the same (here, significant at levels roughly from 3% to 1%). Even so, it would be cheating to do them all and pick the
one with the smallest P-value.


Using container element in form widget

Question: I want to build a widget for a own field type. In the function formElement() of my widget I want the elements wrapped in a div. For this purpose exists the container element. So I build my form:
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

    $element['myitem'] = [
      '#type' => 'container',
      '#attributes' => [
          'class' => ['my-item'],
      ],
      '#attached' => [
        'library' => [
          'mymodule/mylibrary',
        ],
      ],
    ];
   $element['myitem']['label'] = [
    '#type' => 'select',
    '#title' => t('Label'),
    '#default_value' => isset($items[$delta]->label) ? $items[$delta]->label : NULL,
    '#options' => $options_for_label,
  ];
  //… more elements

  return $element;
}

The rendering of the form looks like I wanted.  But the form entries will not be saved. There are no errors and none of the elements were attached to the entity and persisted in the database.
If I don't use the container element and place my elements direct in $element like this:
 $element['label'] = [
  '#type' => 'select',
  '#title' => t('Label'),
  '#default_value' => isset($items[$delta]->label) ? $items[$delta]->label : NULL,
  '#options' => $options_for_label,
];
//… more elements

then all will be saved.
How can I get my widget-form elements be wrapped within a div for styling?
(A similar question using container element within form widget, here with focus at form state was asked 3 years ago, but without answer :-( )
Answer: From WidgetBase::form:
// Most widgets need their internal structure preserved in submitted values.
$elements += ['#tree' => TRUE];

When customizing the structure you also need to make sure the values get submitted correctly. For example by adding WidgetInterface::massageFormValues to your widget.
A much easier way though is to keep the standard structure, which is using property names as form keys, and add the container by #prefix/#suffix:
$element['label'] = [
  '#type' => 'select',
  '#title' => t('Label'),
  '#default_value' => isset($items[$delta]->label) ? $items[$delta]->label : NULL,
  '#options' => $options_for_label,
  '#prefix' => '
',
  '#suffix' => '
',
];

Using container element in form widget


How do I know if a user has access to a private file in the node template

Question: I have a a content type with Title, Body, File (private), and Taxonomy. I want all users to be able to see the Title, Body and Taxonomy for marketing purposes, but they should only be able to download the file if they are an authenticated and their account is related to the same taxonomy term(s) the node is related to.
I have private files set up properly and have written a custom module implementing hook_file_download_access to check if the user should have access. If I go directly to the file URL, access is working as I think it should.
My problem is I'd like node page to say:

"Register Now" if the user is not logged in
"Contact Customer Support" if they are logged in but don't share the proper taxonomies
or "Download Now" if all checks out.

Just not sure how to get it done?
Answer: If your using the Pages Module then you can make different page 'variants' for different roles.