Is there a way to list all of the email accounts on a cPanel server?

Question: I manage a number of cPanel / WebHostManager servers. On one of these servers we need to do an audit of all the email accounts from all the cPanel accounts. This server has nearly 300 cPanel accounts, each with between 1 and 50 email accounts within it...
Is there any way to get a list of all email accounts across all cPanel accounts on a cPanel / WebHostManager server?
Answer: I rolled my own solution. I found that each cPanel user has a YAML file in ~username/.cpanel/email_accounts.yaml which contains information about all email accounts under that cPanel user's account. Using that I whipped up a short Ruby script:
#!/usr/bin/ruby

require 'yaml'

puts "email,username,domain,usage,quota"

Dir.glob('/home/*/.cpanel/email_accounts.yaml') do |email_accounts_file|
  email_accounts = YAML.load_file(email_accounts_file)
  #puts email_accounts_file
  #puts email_accounts.inspect
  #puts "---------------------------"

  email_accounts.each do |domain,info|
    if info['accounts'] then
      #puts "domain: #{domain}"
      info['accounts'].each do |username,info|
        puts "#{username}@#{domain},#{username},#{domain},#{info['diskused']},#{info['diskquota']}"
      end
    end
  end
end

The script should br run as root and loops through all directories under /home looking for .cpanel/email_accounts.yaml files. (If your users' home directories are not stored as /home/username then edit line 7) The script prints out CSV separated data of all email accounts, what domain name they belong to, and what their disk usage and quota is, as reported by the YAML file.
Answer: I created a program to obtain this information directly from the cPanel's APIs.
The program requests a list of all the accounts in the server, extracts the users from the list and issues a new request per user to Email::listpopssingle to obtain all email accounts from the user's account.
Comment: Awesome, that's great! I'll check that out later. Nice work!


Is the limit superior of Radon measures at least a Radon outer measure?

Question: Let $\left(\mu_n\right)_{n=1}^\infty$ be a sequence of Radon measures on $X\subset \mathbb{R}^n$ such that for every compact subset $K\subset X:\sup_{n=1,2,\dots}\mu_n(K) < \infty$. Is it then true that $\tilde{\mu}(A)\equiv \lim\sup_{n\to\infty}\mu_n(A)$ is at least a Radon outer measure?
(Thoughts:) I got stuck trying to prove that the limit supremum is countable subadditive and that each Borel set $B\subset X$ is in fact measurable. My main question is regarding the 2.) below, as that seems to pose a serious threat to the claim, but I'd still like to hear about your thoughts on 1.) as well. My current feeling is that the claim is not true, for the Borel sets might not be measurable in the limit supremum. But without a further ado, let $(S_i)_{i=1}^\infty$ is a sequence of subsets of $X$. Then,
1.) (Countable subadditivity)
$$\lim\sup_{n\to\infty}\mu_n\left(\bigcup_{i=1}^\infty S_i\right) \leq \lim\sup_{n\to\infty}\sum_{i=1}^\infty \mu_n(S_i) \leq \sum_{i=1}^\infty \lim\sup_{n\to\infty}\mu_n(S_i)$$
where $(S_i)_{i=1}^\infty$ is a sequence of subsets of $X$ and the the first equality is by the virtue that $\mu_n$ is a Radon measure.
2.) (Measurability of Borel sets) Now, let $B\subset X$ be a Borel set. Then, we'd like to have that $$\lim\sup_{n\to\infty}\left(\mu_n(E\cap B) + \mu_n(E\cap B^c)\right) = \lim\sup_{n\to\infty}\left(\mu_n(E\cap B)\right) + \lim\sup_{n\to\infty}\left(\mu_n(E\cap B^c)\right)$$
As for 1.), I think that the inequality holds by the virtue that $\lim\sup_{n\to\infty}a_n = \inf_{m\to\infty}\sup_{n\geq m}a_n$ for a real sequence $(a_n)$ and for any $m\in \mathbb{N}$ we have that $\sup_{n\geq m}\sum_{i=1}^\infty \mu_n(A_i) \leq \sum_{i=1}^\infty \sup_{n\geq m}\mu_n(A_i)$, but I can't say for sure as I'm not that comfortable with infimum juggling .
As for 2.), if the equality does hold, it requires the use of some measure specific tools since it is my understanding that $\lim\sup_{n\to\infty}\left(a_n + b_n\right) \leq \lim\sup_{n\to\infty}\left(a_n\right) + \lim\sup_{n\to\infty}\left(b_n\right)$ for any real sequences $(a_n), (b_n)$.
(Bonus: Weak convergence) I am aware that under the assumptions on the sequence $(\mu_n)$, there exists a Radon measure $\mu$ and a subsequence of $(\mu_{n_k})$ such that $(\mu_{n_k})$ converges weakly to $\mu$, i.e.
$$\forall f \in C_0(X):\lim_{k\to\infty}\int_Xfd\mu_{n_k}=\int_Xfd\mu$$
As of now I am not sure how this fact could be exploited, or whether any additional weak convergence assumption could be used on the original sequence $(\mu_n)$. I decided to include this fact to my post as it seems related, although I can't really comment on its implications.
Answer: Take $X = \mathbb{R}$ and define $\mu_k = \delta_0$ if $k$ is even and $\mu_k = \delta_1$ otherwise (where $\delta_0$ and $\delta_1$ denote the Dirac measures at $0$ and $1$). Then if we set $\tilde{\mu}(A) = \limsup_{k \to \infty} \mu_k(A)$ for any Borel set $A \subset \mathbb{R}$ we have
$$ \tilde{\mu}(\{0\}) = \tilde{\mu}(\{1\}) = \tilde{\mu}(\{0, 1\}) = 1$$
which implies that $\tilde{\mu}$ is not a Borel measure.
Comment: @Fantastic counterexample. Thank you!


When is a stochastic process not differentiable?

Question: Assume $\frac{dX_t}{X_t} = \mu dt + \sigma d B_t$ where $\mu$ is a constant and $B_t$ is a Brownian motion, and let $Y_t = \ln X_t$. I understand that $B_t$ is nowhere differentiable and both $X_t$ and $Y_t$ are functions of $B_t$, so neither of them are differentiable. Thus, $d(\ln X_t) \neq \frac{d X_t}{X_t}$.
But can someone please explain why:

$\frac{\partial Y_t}{\partial X_t} = \frac{1}{X_t}$
$\int d Y_t = Y_t$

Assume the entire question is under Ito's Lemma if necessary.
Comment: I guess you define 1 in the Malliavin sense? Then it makes sense. Otherwise, what is the definition of 1?
Answer: (1) Forget about random variables for a second. You have a function $Y_t=Y(t,X_t):=\ln(X_t)$. Then as a function it's partial derivative is defined in the usual sense: $\frac{\partial Y_t}{\partial X_t}=\frac{1}{X_t}$. The fact that $Y_t$ is a random variable makes no difference: your $X_t=X(t,\omega)$, but you're still asking what the change in $Y_t$ is if you keep $t$ constant, and vary $X_t$. Whenever you see such partials involved, they are always in the classical multivariable calculus sense.
Don't confuse this with terms like $dY_t$ which are formally defined as stochastic differentials:
(2) This can be taken as the definition of $dY_t$. In fact:
$$Y_t-Y_0=\int_0^tdY_s,$$
where the integral is an Ito Integral, and not Lebesgue or Riemannian/Stieltjes.
If now $Y_t=Y(t,X_t)$, then Ito's lemma in it's full form says that:
$$dY_t=\frac{\partial Y}{\partial t}dt+\frac{\partial Y}{\partial X_t}dX_t+\frac{1}{2}\frac{\partial^2 Y}{\partial X_t^2}dX_t^2+...$$
This should look really familiar: It's the Taylor series of $Y_t$ expanded around $t,dX_t$, with higher order terms of $dt$ suppressed. If you now substitute $dX_t=\mu dt+\sigma dB_t$, you'll recover Ito's lemma after assuming $dt^2$ and $dtdB_t$ is higher order, and $dB_t^2=dt$. You can formaly prove those last facts using the definition of quadratic variation.


Erro ao passar valores de array JS para PHP simples

Question: Meu código é esse aqui embaixo:

<form method="post" action="recebe.php" >
<input type="hidden" id="send_string_array" name="send_string_array" value="" />
<input type="submit" value="Enviar" />
</form>

</heady>

O código PHP que deveria funcionar e mostrar o array é esse:
<?php

//transforma a string de itens separados em array
$array_produtos = explode("|", $_POST['send_string_array']);
//mostra o conteúdo do array
echo $_POST['send_string_array'];

?>

O PHP está me retornando isso Array ( [0] => );, mas deveria conter os valores no array, não?
Qual seria meu erro?
Comment: adailton sua nova edição removeu justamente a linha que eu comentei abaixo sobre ser o problema, com esse código atual permanece o problema?
Comment: é porque voce respondeu enquanto eu estava editando foi mau.
Answer: Seu erro está no javascript, mas uma observação antes, você está definindo o array_produtos duas vezes o que seria desnecessário.
var array_produtos = Array([1,2,3,4,5,6]); /* aqui */
var i, array_produtos; /* e aqui */

Agora sobre seu erro está nessa linha
array_produtos = array_produtos.split("|");

O array do javascript não possui nenhuma função split e fora que sua lógica não precisa dessa linha, remova ela e seu programa provavelmente irá funcionar.
Ficando assim o javascript:

A função split que você estava tentado utilizar é quando a variável é String, ou seja, você pode utilizar dessa forma:
var virouString = array_produtos.join("|");
var virouArrayNovamente = virouString.split("|");

Comment: @adailtonmoraes da um `echo $_POST['send_string_array']` no seu `php` pra ver como ele está enviando essa informação
Comment: @adailtonmoraes Perfeito, se possível marque a resposta como a solução. Boa noite!
Comment: sim mas nao passa pro php, e agora dei uma editada no codigo da uma olhada esta me retornando isso no recebe.php  Array ( [0] => 1,2,3,4,5,6 ) onde deveria ficar assim, [1,2,3,4,5,6]); .
Comment: funcionou perfeitamente editando o codigo com os erros corrigidos.


Como limitar exibição de registros a 30 dias a partir da data de hoje

Question: Gostaria de poder limitar a exibição de notícias que tenho gravadas em meu banco de dados a 30 dias a partir da data de hoje, por exemplo:
Exibir notícias cadastradas de 03/02/2016 à 02/01/2016, tentei usar algumas soluções que encontrei em pesquisa mas ao aplicar as tentativas nenhum registro é exibido, tentei esses duas dicas:
SELECT * FROM tabela_x WHERE Data BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE();

e também essa:
SELECT * FROM tabela_x WHERE data between NOW() and DATE_ADD(NOW(), INTERVAL 1 MONTH)

Mas como disse, nenhum registro é exibido, o que tenho é isso:
$aColumns = array( 'Titulo', 'Tipo', 'Data', 'Hora', 'IdNoticia' );
//$aColumns = array( 'IdNoticia', 'Titulo', 'Tipo', 'DATE_FORMAT(Data,"%d/%m/%Y")', 'Hora' );
//$aColumns = array( 'IdUnicoop', 'Descricao' );

/* Coluna de índice (usada para maior eficiência na cardinalidade da tabela - geralmente a chave primária) */
$sIndexColumn = "IdNoticia";

/* Tabela */
$sTable = "Noticia";

/* Informações de conexão do banco */
$gaSql['user']       = "";
$gaSql['password']   = "";
$gaSql['db']         = "";
$gaSql['server']     = "";

/*
 * Funções locais
 */
function fatal_error ( $sErrorMessage = '' )
{
    header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
    die( $sErrorMessage );
}

/*
 * Conexão MySQL
 */
if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password']  ) )
{
    fatal_error( 'Não foi possível abrir conexão com o servidor' );
}

if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
{
    fatal_error( 'Não foi possível selecionar o banco de dados ' );
}

/*
 * Paginação
 */
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
    $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
        intval( $_GET['iDisplayLength'] );
}

/*
 * Ordenação
 */
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
    $sOrder = "ORDER BY  ";
    for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
    {
        if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
        {
            $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
                ($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
        }
    }

    $sOrder = substr_replace( $sOrder, "", -2 );
    if ( $sOrder == "ORDER BY" )
    {
        $sOrder = "";
    }
}
//$sOrder = "ORDER BY Data DESC, Hora DESC";

/* Busca por termo */
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        //Customização para filtrar pela coluna de data de forma correta
        if ($aColumns[$i] == "Data")
        {
            $oldData = mysql_real_escape_string($_GET['sSearch']);
            $novaData = implode(preg_match("~\/~", $oldData) == 0 ? "/" : "-", array_reverse(explode(preg_match("~\/~", $oldData) == 0 ? "-" : "/", $oldData)));
            $sWhere .= "`".$aColumns[$i]."` LIKE '%".$novaData."%' OR ";
        }
        else {
            $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( utf8_decode($_GET['sSearch'] ))."%' OR ";
        }
    }
    $sWhere = substr_replace( $sWhere, "", -3 );
    $sWhere .= ')';
}

/* Filtro individual por coluna */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
    if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
    {
        if ( $sWhere == "" )
        {
            $sWhere = "WHERE ";
        }
        else
        {
            $sWhere .= " AND ";
        }
        $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
    }
}

/* Customização para este script */
if ( $sWhere == "" )
{
    $sWhere = "WHERE Publicar = 'S' AND Data between NOW() and DATE_ADD(NOW(), INTERVAL 1 MONTH)";
}
else
{
    $sWhere .= " AND Publicar = 'S' AND Data between NOW() and DATE_ADD(NOW(), INTERVAL 1 MONTH)";
}

//debug($sWhere);
//debug($sOrder);

/*
 * SQL query
 * Obtenção de dados para exibição
 */
$sQuery = "
    SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
    FROM   $sTable
    $sWhere
    $sOrder
    $sLimit
    ";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );

/* Quantidade de registros encontrados após a filtragem */
$sQuery = "
    SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];

/* Totalizador de linhas */
$sQuery = "
    SELECT COUNT(`".$sIndexColumn."`)
    FROM   $sTable
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];

 /*
 * Saída
 */
$output = array(
    "sEcho" => intval($_GET['sEcho']),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => array()
);

while ( $aRow = mysql_fetch_array( $rResult ) )
{
    $row = array();
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ($aColumns[$i] == "Data")
        {
            $novaData = date('d/m/Y', strtotime($aRow[ $aColumns[$i] ]));
            $row[] = $novaData;
        }
        else if ($aColumns[$i] == "Titulo")
        {
            /* Saída especial para a coluna Título devido a acentuação */
             $row[] = utf8_encode($aRow[ $aColumns[$i] ]);
        }
        else if ( $aColumns[$i] != ' ' )
        {
            /* Saída Geral */
            $row[] = $aRow[ $aColumns[$i] ];
        }
    }
    $output['aaData'][] = $row;
}

echo json_encode( $output );

Answer: A solução que eu aplicaria é verificar se a data do banco é maior ou igual a -30 dias contado da data de hoje.
Assim:
$data = (new DateTime('-30 days'))->format('Y-m-d H:i:s');

$sql = sprintf('SELECT * FROM tabela WHERE campo_data >= "%s"', $data);

Atualização
É bom lembrar que se a gente utilizar -30 days, estamos fazendo com que a pesquisa esteja também relaciona ao horário exato, pois o horário retornado será o atual.
Se tivermos por exemplo uma solicitação registrada 10:10 e o horário atual for 10:12, e a data coincidir com a última data possível para verificação, você teria problemas.
Se isso for um problema  no seu caso, você pode também fazer "zerar" o horário da data que estamos gerando pelo DateTime (antes de passar para seu SQL, é claro):
  $date = new DateTime('-30 days');

  $date->setTime(0, 0, 0);

Comment: Eu só sei em php :p
Comment: Excelente script, atendeu as minhas necessidades, obrigado @Wallace Maxters
Answer: Em MySQL um SELECT pode fazer isso:
SELECT
    *
FROM
    tabela_x
WHERE
    Data
BETWEEN
    DATE_ADD(NOW(), INTERVAL -30 DAY)
AND
    NOW();


Which model evaluation scoring strategies are appropriate for different regression problems?

Question: Scikit-learn provides 4 different scoring objects for evaluation of regression problems:

'neg_mean_absolute_error'
'neg_mean_squared_error'
'neg_median_absolute_error'
'r2'

E.g. r2 is a particularly poor choice when the cross-validation strategy includes a very small test size (variance becomes extreme as the test size approaches leave-one-out CV).
When would you tend to utilize each of these four scoring methods for regression model evaluation? What about when performing hyperparameter selection?
Answer: I too find the default of R2 to be the not the ideal metric for assessing regression results, as I am typically interested in either forecasting or predicting a value, so the error is more useful. I tend to use the mean squared error (somewhat confusingly referred to as 'neg_mean_squared_error' in the scikit scorer here) when validating my models.
And there is actually even a fifth now, explained_variance (see here: http://scikit-learn.org/stable/modules/model_evaluation.html)
In terms of evaluation vs. hyperparameter selection, IMO if there is a metric you are aiming for, keep it consistent.


Alguém conhece o framework Ionic?

Question: Vcs conhecem o framework Ionic? É similar ao PhoneGap.
Comment: Qual a sua duvida ou problema exatamente?
Comment: Quero aprender a mexer nele.
Comment: Neste caso você deve procurar por matérias e cursos sobre o assunto e em caso de duvidas deve postar aqui. O site é sobre duvidas e problemas específicos. Maiores informações você encontra neste link: http://pt.stackoverflow.com/help
Answer: Não é que seja similar, ele usa o próprio PhoneGap.
O Ionic não substitui o mesmo, ele é voltado para aparência e funcionalidades mas a plataforma é o Cordova ( PhoneGap ). Atualmente ele usa o angular JS então é bom sabê-lo também.
Leia isso é muito útil:
http://ionicframework.com/blog/where-does-the-ionic-framework-fit-in/
E se quiser aprender mais sobre ele indico: https://thinkster.io/ionic-framework-tutorial/
Comment: vc já usou ou usa o Ionic?
Comment: @GustavoSevero sim, uso e já usei. Esse aplicativo na qual já trabalhei é criado com ele: http://www.makemynight.com.br/
Comment: Falando no Ionic, aqui um link de um artigo lançado recentemente em portugues: http://tableless.com.br/introducao-ao-ionic-framework/

Alguém conhece o framework Ionic?


Excel merging spread sheets

Question: I was wondering how to merge excel spread sheets. I have 2 spread sheets with a Ticker# column, I need to insert all the columns in sheet 2 to the corresponding Ticket# in sheet1. I can't just copy and paste them all because the ordering is different, and not all Ticket# exist in both sheets.
Any help would be appreciated. Thanks
Comment: You will need to use VBA code to do this. If you do not know VBA, you should read tutorials and then ask us question. One method to help you is to use the macro recorder and then modify the code it produced to better match your need.
Comment: i'm familiar with VBA, although still inexperienced. Could you point me in the direction as to how I should go about doing this with VBA?
Comment: This does not have to be VBA, you can also use the MS Query method if you are farmiliar with SQL or Access. Are you?
Answer: Use vlookup (or hlookup if you're using rows instead of columns) to find the information, if you need it saved in one sheet, you can copy that sheet and paste it into a new file and save as.
if you can share the file, I can give you the correct function. Or if you can give me a sample of data of both sheets.
Comment: vlookup will not merge, it will fill in the gaps on 1 spreadsheet from data gathered from the 2nd spreadsheet. If your data is going to continue to be generated in 2 spreadsheets, this is your permanent solution. If your data is going to switch to be kept in 1 spreadsheet, then this would get your data to 1. You can also use VBA as referenced above, but it'd take more effort than a vlookup, copy and paste and give you the same result
Comment: It depends on the needs for this application. Here at work we almost always need our tasks to be entirely automated because most users have  only functional knowledge of Excel. If you can assume your users to be experienced with Excel then this is easier though in long run the cost in time of coding the algorithm to achieve this tasks will pay itself. Also, using VLOOKUP() you will need to make sure that your temporary list contains all the elements to be matched against otherwise you will be missing some while in VBA you can just iterate through all elements and not miss any.
Comment: hey, I was able to create a quick fix using Vlookup, and it worked like a charm. Thanks!!
Comment: Good to hear, if the answer worked for you, make sure you mark it as an answer to your question for future users

Excel merging spread sheets


Prove that $\lim_{n\rightarrow \infty} \int_{0}^{1} (f(x))^n dx$ exists if and only if $\mu ( \{x \in [0,1]: f(x)>1 \})=0$

Question: I am trying to solve this problem, but I have not succeeded. Can someone help me? Thanks in advance. The problem is:
Let $f$ be nonnegative measurable function on $[0,1]$. Prove that $lim_{n\rightarrow \infty} \int_{0}^{1} (f(x))^n dx$  exists if and only if $\mu ( \{x \in [0,1]: f(x)>1 \})=0$.
Comment: Try it with some function where that _isn't_ true, like $f(x) = 1.01x$, and see what happens.
Answer: Hints: let be $G = \{x\in[0,1]\,:\, f(x) > 1\}$, $G_\epsilon = \{x\in[0,1]\,:\, f(x) > 1+\epsilon\}$.
Case $\mu(G) > 0$: this implies $\mu(G_\epsilon) > 0$ for some $\epsilon > 0$; use that $f\ge(1+\epsilon)\chi_{G_\epsilon}$.
Case $\mu(G) = 0$: this implies that $\lim_{n\to\infty}(f(x))^n$ exists for almost all $x\in[0,1]$; apply the [...] convergence theorem.


Can you explain the following definition in layman's terms?

Question: Probability $P_X$ on the space $(\Bbb R^n, \mathcal B_n)$ defined in the following way:
$\forall B \in \mathcal B_n : P_X(B) = P(X^{-1}(B))$
is called the Distribution of a random variable $X$.
Comment: Banach spaces in layman terms? Can you describe your image of a layman in this case?
Comment: The definition is incomplete as you need a probability $P$ defined on the space $\Omega$ on which $X$ is defined;$$X:\Omega\rightarrow\mathbb{R}^n$$
Comment: @Aksakal, just translate the definition into simple English.
Answer: $\mathcal{B}_n$ is apparently some $\sigma$-algebra on $\mathbb{R}^n$ and so $B$ is a set in $\mathbb{R}^n$.  The probability that $X$ belongs to this set is the probability that we draw an $\omega \in \Omega$ that maps to $B$ under the function $X$, i.e., $X(\omega) \in B$.  The collection of all such points is $X^{-1}(B)$ and the probability of this collection is $P(X^{-1}(B))$.
Answer: You describe all the events that may happen. Then attach real numbers to non-overlapping sets of events. You already have the probabilities attached to events and sets of events. So, you should be able to track the probability of the real number by first finding the events to which it's attached, then finding the probability of these events. That is the probability distribution, i.e. mapping of real numbers to probabilities.
To a layman, this whole events and Banach business is behind the curtains. All the layman sees is the final mapping of real numbers (random variable realizations) to probabilities. This works the same way for continuous and discrete distribution, though the latter is often easier to explain to a total dummy since you can do this with coin flip examples or similar trivial exercises.
Comment: I am unable to understand what this answer is trying to say or how it is an answer to the question being asked. Has Aksakal's account been hacked and the answer is spam written by the hacker? What on earth is "the probability of the real number"? What is the probability attached to $1.2345$ and how does if differ from the probability attached to $9.8765$?


Probabilities in logistic regression

Question: I'm learning about logistic regression from the Andrew Ng Machine Learning course. In the course it is stated that the hypothesis is the probability of the output $y=1$ for a given input $x$. That is, $$h_\theta(x)=P(y=1|x;\theta).$$
It is assumed that $h_\theta(x) = \frac{1}{1+e^{-\theta^T x}}$, i.e. the sigmoid function.
What I am confused about is how we know this? Is this a definition?
Answer: Because probabilities are bounded between 0 and 1, we need some function which can take the unbounded input $\theta^T x$ and map it to the unit interval.  Mathematically, we need
$$f : \mathbb{R} \to [0,1]$$
The sigmoid function accomplishes this, but it is not the only one.  If we had used the CDF of the normal, we would be doing something called Probit regression (closely related to logistic regression).
In short, we have constructed logistic regression (i.e. defined it) in a way that $P(y=1) = h_\theta (x)$.
Comment: Thanks for the response. I am still a bit confused. If we have a choice of any function with the property you specify then will we not get different answers for P(y=1)?
Comment: @Kris slightly different answers, yes.  Its important to remember that "all models are wrong..." in this context.
Answer: There are many "sigmoid" functions, but the one you quote, the logistic function, is the most common in machine learning and statistics. I can think of two reasons for it:

It fits well into the formalism of generalised linear models, and
it appears naturally as the class probability if the classes are normally distributed.

The first reason is likely to be given more often by statisticians and the second by machine learners.
1. Statistical approach
Assume a random variable $Y$ which can take two discrete values, say $A$ and $B$---these can be regarded as "class labels". Define a variable $y$ which is one when $Y=B$ and zero otherwise (i.e. $Y=A \Leftrightarrow y=0$). Assume also that the probability of a point with a real (possibly vector) value $x$ to belong to the class $B$ depends somehow on that value, i.e. $P(y) = p(x)$, with $p(x)$ being a function of $x$, $p :  \mathbb{R} \rightarrow [0, 1]$.
With some mathematical acrobatics, $P(y)$ can be rewritten to fit into the general notation for the exponential family of probability distributions:
$$
  P(y) = h(y) \exp(\eta(x) \cdot T(y) - A(\eta(x)))
$$
Depending on the choice of $h(y)$, $\eta(x)$, $T(y)$ and $A(\eta(x))$ you can construct many different distributions: Gaussian, Poisson, Gamma etc. For our distribution ("Bernoulli"), we need to set:
$$
\begin{align}
  h(y)    &= 1\\
  \eta(x) &= \ln \frac{p(x)}{1-p(x)} \\
  T(y)    &= y \\
  A(\eta) &= \ln \left( 1 + \exp(\eta(x)) \right)
\end{align}
$$
For the moment, you can safely ignore everything except the second line, defining $\eta(x)$. This relationship is called log-odds. Now, if we express $p(x)$ in terms of $\eta(x)$ we get:
$$
  p(x) = \frac{1}{1 + e^{-\eta(x)}}
$$
Generally, $\eta(x)$ can have any form, but it is mathematically appealing to assume it to be a linear function, or, in your notation, $\eta(x) = \theta^T x$. Hence the "generalised linear model".
2. Machine learning approach
Assume two probability distributions ("classes"), $A$ and $B$. Regardless of their probability distributions, it follows from the Bayesian formula:
$$
P(B | x) = \frac{P(x | B) P(B)}{P(x)} =
           \frac{P(x | B) P(B)}{P(x | A) P(A) + P(x | B) P(B)} =
           \frac{1}{1 + \frac{P(x | A)P(A)} {P(x | B)P(B)}}
$$
If $x$ is continuous, so that the classes can be described by their respective probability density functions (PDFs), $f_A(x)$ and $f_B(x)$, the fraction $P(x | A) / P(x | B)$ can be expressed as:
$$
\frac{P(x | A)} {P(x | B)} = \lim_{\Delta x \rightarrow 0}
  \frac{f_A(x) \Delta x}{f_B(x) \Delta x} =
  \frac{f_A(x)}{f_B(x)}
$$
Now, let us assume that the two classes are normally distributed, with equal variances:
$$
f_A(x) = \frac{1}{\sqrt{2 \pi} \sigma}
  \exp \left( -\frac{(x - \mu_A)^2}
  {2 \sigma^2} \right), ~ ~ ~ ~ ~ ~ ~ ~ ~ f_B(x) = \frac{1}{\sqrt{2 \pi} \sigma}
  \exp \left( -\frac{(x - \mu_B)^2}
  {2 \sigma^2} \right)
$$
Then the fraction $f_A(x) / f_B(x)$ can be written as:
$$
\frac{f_A(x)}{f_B(x)} = \exp \left(
 - \frac{(x - \mu_A)^2} {2 \sigma^2}
 + \frac{(x - \mu_B)^2} {2 \sigma^2}
\right) =
\exp \left(
 \frac{\mu_B^2 - \mu_A^2} {2 \sigma^2}
 + \frac{\mu_A - \mu_B} {\sigma^2} x
\right)
$$
and the whole term
$$
\frac{f_A(x)P(A)}{f_B(x)P(B)} =
\exp \left(
  \ln \frac{P(A)}{P(B)} +
  \frac{\mu_B^2 - \mu_A^2} {2 \sigma^2} +
  \frac{\mu_A - \mu_B} {\sigma^2} x
\right)
$$
Denoting
$$
\theta_0 = \frac{\mu_A^2 - \mu_B^2} {2 \sigma^2} - \ln \frac{P(A)}{P(B)}
 ~ ~ ~ ~ ~ ~ ~ ~ \text{and} ~ ~ ~ ~ ~ ~ ~ ~
\theta_1 = \frac{\mu_B - \mu_A} {\sigma^2}
$$
leads to the form commonly used in logistic regression:
$$
P(B | x) = \frac{1}{1 + e^ {-\left(\theta_0 + \theta_1 x \right)} }
$$
or, in vector notation and assuming $x$ with the zeroth element fixed to 1:
$$
P(B | x) = \frac{1}{1 + e^ {-\theta^T x} }
$$
Whichever approach you take, you are making assumptions about the data. In the statistical approach, you assume that the log-odds depend linearly on $x$. In the machine learning approach, you assume normal distributions of the classes (the log-odds assumption follows from the normality assumption, but not the other way round). In practice, normally distributed data are quite common, so this assumption is likely, but not guaranteed to be true. If not, your modeled probabilities will deviate from the true probabilities.

Probabilities in logistic regression


Delayed require - Uninitialized constant error

Question: I have a conditional bit of code that only can be loaded under certain conditions. It's platform specific code.
module MyGem
  module MyPlatformSpecificThing
    #stuff
  end
end

My current attempt at lazily requiring this is performed like:
module MyGem
  class AClass

  def DoSomething
    if thing_is_true
      require 'my_platform_specific_thing.rb'
      MyGem::MyPlatformSpecificThing.init
      #more stuff
    end
    #even more stuff
  end
end

This seemed like a solid enough plan, unfortunately it is not working. This code results in an error:
uninitialized constant MyGem::MyPlatformSpecificThing (NameError)
The stack trace indicates the source of the error is the call to init within the DoSomething method.
I am not entirely sure why Ruby is giving me fits here. What am I doing wrong, and how should I be doing this?
edit:
For specifics, I'm referring to the Platform class located here. The above should help narrow down the details of what I'm referring to, but in case anyone wanted/needed to see the specific classes/modules I'm work with.
Comment: To the person who voted to close this question as "unclear as what is being asked"; I am pretty sure I spelled that out in the question, but if there is a specific aspect of this I can elaborate on please do let me know.
Comment: can you elaborate on what `thing_is_true` checking? does it have to be in an instance of `AClass`? If you can move it out of instance, then I would recommend doing require on class level and not instance level.
Comment: Sure. The actual check is checking a class variable to see if it's null. I am not sure I am following the last part of moving the require to the class level, could you elaborate a little more on that?
Comment: FWIW, AClass was simply a stripped down version of this -> https://github.com/jwhitehorn/pi_piper/blob/develop/lib/pi_piper/platform.rb
Comment: by class level I meant move it out from instance method into class declaration or module declaration: https://gist.github.com/igagnidz/54d846e984f620b3c5a3
Comment: Ok, I see what you mean. Unfortunately I do not see immediately how that is possible. You see the DoSomething method in this case is a factory and is delaying the loading of a platform specific gem. Under certain cases (e.g. unit testing) I do _not_ want the platform specific gem loaded. Under those scenarios the factory has a mock injected into it. See the link I posted for the specifics I'm working from.
Comment: `require` actually needs the file name without `.rb`, i.e. `require 'my_platform_specific_thing'`. Could this be the cause of the problem? Are you doing a `rescue LoadError` somewhere in your actual code that may prevent a potential `LoadError` from bubbling up?
Comment: @p11y I've used 'class.rb' before in require... but, I will admit that's one thing I haven't tried. I'll give it a try and let you know.
Answer: You've almost got it right. Remember that the require method loads relative to the load path. As you're making a gem you've added the gem lib directory to the load path. Change the require to:
require 'my_gem/bcm2835'

Or for PiPiper:
require 'pi_piper/bcm2835'

This should load only when a driver hasn't been set.
Comment: lol! Don't feel too bad @JasonWhitehorn, I'm just happy to help :-)
Comment: Thanks!!! https://github.com/jwhitehorn/pi_piper/commit/de359540749a587c68ef065331d0956719796553
Comment: Oh no!!!!!! After I accepted your answer the website re-ordered the answers, and I accidentally awarded the bounty to the wrong "answer"... Oh, I feel so bad!!! I've requested mod help. Hopefully they can sort it out.
Comment: Thanks @BoltClock. Totally my fault, I realized it 2 seconds after it happened :-(
Answer: Your pseudo-code is bit incomplete and confusing, but I think this may be the problem.  If I am mistaken, seeing your real code as complete as necessary could help.
Check this response as a reference.
Your example shows:
MyGem::MyPlatformSpecificThing.init

However, that format should have Module::Class.method.  Your sample shows Module::Module.method.
The error message
uninitialized constant

is very specific in that it is recognizing the call as a constant, not a class.
Comment: For completeness "my real code" was linked to in the question. Look under the "edit" section at the bottom. - I am not sure this is a solution at all... I will review, but I'm pretty sure I understand what `::` does ;-)
Comment: BTW, it's a module level function I'm calling, so it'd be Module::Module.method... not Module::Class.method.
Comment: I saw the PiPiper code.  And, I saw how you modified it.  I don't see the init method.  Regardless, I believe there is an issue in the definition that is driving the problem since the error indicates an invalid reference to a constant.  Just trying to help here, so please accept it in that light.  Thanks.
Comment: Thank you, I understand and appreciate the help!
Answer: The error you've specified...

uninitialized constant MyGem::MyPlatformSpecificThing (NameError)

... will only occur if the inner module (PiPiper::Bcm2835) is never actually defined; if the problem was with the actual call to the module function (init), you'd get a different error.
I notice you've commented out the require on line 10, platform.rb:
#require 'bcm2835.rb'

... and required the file elsewhere. Let me guess, this is solving your problem, no?
Taking another look, in its original form the require statement should have failed, unless you've already added the pi_piper directory to $LOAD_PATH. Try changing the lazy require to this:
require './bcm2835.rb'

But note that this will not work either if you change the working directory (Dir.chdir) at any point prior to executing this line.
Comment: For future visitors, this is _NOT_ the correct answer. The site reordered the results causing this to incorrectly receive the bounty.
Comment: I'll gladly hand over the bounty to the intended user if that's possible. If you felt someone did a better job of answering the question, that's up to you. But voting it down? What part did you feel was incorrect??


Execv with files in a child process

Question: I have two programs in the same directory main and example, if I execute ./example file.txt it manages simple tasks with that file. Then my main program is
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main (int argc, char* argv[]){
   pid_t const pid1 = fork();
   if(pid1==0){
     execv();
   }
   else{
     wait(NULL);
   }
 }

So I want to execute ./main file.txt and the child process should execute ./example file.txt with the same file that main has received in argv[1].
My question is how to use the execv parameters to accomplish this.
Sorry if I did not explained myself simple.
Thank You
Comment: Please just show [minimum necessary](http://stackoverflow.com/help/mcve) code to reproduce your problem.
Comment: Do you have a specific question?
Comment: Give me 5 mins to modify the question, thanks.
Comment: @PCLuddite There you have my modified question. Sorry for that
Comment: @melpomene There you have my modified question. Sorry for that
Answer: The easiest way to do this would be:
execl("./example", "example", argv[1], (char *)NULL);

Alternatively,
char *args[] = {
    "example",
    argv[1],
    NULL
};
execv("./example", args);

Execv with files in a child process


Centos 6 webserver - best use of spare memory

Question: Just after any quick easy suggestions about where I should try tweaking my web server to use some of the spare memory and reduce CPU usage.
Sorry if I'm a bit vague, but this isn't really my area.
I run Apache 2 with MySQL 5 and most websites are PHP - either bespoke or usual Wordpress / Drupal etc.
I already have some MySQL cahcing and optimizing going on along with APC object caching, but am wondering whether to crank up the settings on these.
I also set my PHP memory limit right up to 512mb, but I still don't seem to be running out of memory.
Any ideas?
Thanks,
Chris
Answer: Assuming you've done basic tuning for your APC cache, and have Apache's memory/worker settings appropriately sized (most common error here is to overdo it), the best approach for the rest is to leave it alone and let Linux do its thing. It will use that spare memory for caching and buffering.
And, if your site suddenly becomes popular, you're not already overcommitted and have room to handle thta. (In fact, for that reason, I'd suggest putting the PHP memory limit back down to a smaller number.)
Answer: As I explained in my answer to How do I improve the performance of my Drupal site with a lot of concurrent users?, you need to tune Apache to your site (or in this case, your server). Ever server is unique, and you should never use a stock, out-of-the-box config. It takes time.
That said, I imagine that you've misinterpreted the amount of memory you have (and how your memory is handled on your server). Take a look at this Question (and the answers) on Meaning of the buffers/cache line in the output of free.


error: cannot bind 'std::basic_ostream' lvalue to 'std::basic_ostream&&'

Question: I have already looked at a couple questions on this, specifically  Overloading operator<<: cannot bind lvalue to 'std::basic_ostream<char>&&'
was very helpful.  It let me know that my problem is I'm doing something that c++11 can't deduce the type from.
I think a big part of my problem is that the instantiated class I'm working with, is templated, but originally obtained from a pointer to a non-template base class.  This is something I did advised from another stackoverflow.com question about how to put template class objects into an STL container.
My classes:
class DbValueBase {
  protected:
    virtual void *null() { return NULL; }   // Needed to make class polymorphic
};

template <typename T>
class DbValue : public DbValueBase {
  public:
    DbValue(const T&val)  { data = new T(val); }
    ~DbValue() { if (data) delete data; }

    T   *data;

    const T&    dataref() const { return *data; }

    friend std::ostream& operator<<(std::ostream& out, const DbValue<T>& val)
    {
        out << val.dataref();
        return out;
    }
}

And, the code snippet where the compile error database.cc:530:90: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&' occurs:
//nb:  typedef std::map<std::string,DbValueBase*>  DbValueMap;
    const CommPoint::DbValueMap&    db_values = cp.value_map();
    for (auto i = db_values.cbegin() ; i != db_values.cend() ; i++) {
        // TODO: Need to implement an ostream operator, and conversion
        // operators, for DbValueBase and DbValue<>
        // TODO: Figure out how to get a templated output operator to
        // work...
 //     DbValue<std::string> *k = dynamic_cast<DbValue<std::string>*>(i->second);
        std::cerr << "  Database field " <first << " should have value " << *(i->second) << endl;
    }

If my output tries to print i->second, it compiles and runs, and I see the pointer.  If I try to output *(i->second), I get the compile error.  When single-stepping in gdb, it seems to still know that i->second is of the correct type
(gdb) p i->second
$2 = (DbValueBase *) 0x680900
(gdb) p *(i->second)
warning: RTTI symbol not found for class 'DbValue<std::string>'
$3 = warning: RTTI symbol not found for class 'DbValue<std::string>'
{_vptr.DbValueBase = 0x4377e0 <vtable for DbValue<std::string>+16>}
(gdb) quit

I'm hoping that I'm doing something subtly wrong.  But, it's more complicated than I seem to be able to figure it out on my own.  Anyone else see what thing(s) I've done wrong or incompletely?
Edit:
@PiotrNycz did give a good solution for my proposed problem below.  However, despite currently printing values while doing development, the real need for these DbValue<> objects is to have them return a value of the correct type which I can then feed to database operation methods.  I should've mentioned that in my original question, that printing is of value, but not the end of my goal.
Comment: `virtual void *null() { return NULL; }   // Needed to make class polymorphic` Instead you can just declare the destructor virtual, which you need to do anyway. `virtual ~DbValueBase() = default;`
Comment: @barnes53 Thanks.  It meant I had to add a `noexcept (true)` to the template subclass destructor, something to do with `default` I'm sure, but cleaner than an unused method.  Thanks.
Answer: If you want to print object by base pointer - make the ostream operator in base class:
class DbValueBase {
  protected:
    virtual ~DbValueBase() {}
    virtual void print(std::ostream&) const = 0;
    friend std::ostream& operator << (std::ostream& os, const DbValueBase & obj)
    {
       obj.print(os); return os;
    }
};

template <typename T>
class DbValue : public DbValueBase {
  public:
    void print(std::ostream& os) const
    {
        out << dataref();
    }
};

Comment: Thanks.  I'm sorry that I can't figure out how to get the compiler to figure out the correct form of DbValue<>::operator<< to call, and sorry too that this means I have to write a function which basically does what I want operator<< to do.  But, the above does solve the problem without resorting to the `if dynamic_cast> else dynamic_cast> else ...` I was using in the interim.  :-)
Comment: However, I realize that I was solely talking about printing because that's what I'm doing so far.  But, I'll also need to convert these objects into the templated type because I need to insert them into the database.  Your solution will work for performing operations, but not to returning values.  Perhaps I should extend my original question to indicate this.
Comment: @cross This is general approach. try this: `virtual void DbValueBase::insertMe(Database& db) = 0;  `
Answer: Although the debugger correctly identifies *(i->second) as being of the type DbValue<std::string>, that determination is made using information that is only available at runtime.
The compiler only knows that it is working with a DbValueBase& and has to generate its code on that basis. Therefore, it can't use the operator<<(std::ostream&, const DbValue<T>&) as that does not accept a DbValueBase or subclass.

For obtaining the contents of a DbValue<> object through a DbValueBase&, you might want to loop into the Visitor design pattern.
Some example code:
class Visitor {
public:
    template <typename T>
    void useValue(const T& value);
};

class DbValueBase {
public:
    virtual void visit(Visitor&) = 0;
};

template <class T>
class DbValue : public DbValueBase {
pblic:
    void visit(Visitor& v) {
        v.useValue(m_val);
    }
private:
    T m_val;
};

Comment: Was a bit complicated to figure out how to use a Visitor design pattern in my situation, to apply the values (`typename T`) as arguments to an object, but was able to do it.  Just had to build the object into the Visitor during construction, then was able to use it like your example above.  This is now the accepted answer.  Thanks again!
Comment: I'll have to see if this works for my needs.  I want to effectively return the value, I was thinking, and this doesn't seem to adjust to that.  I'll need to see if I can code up a visitor function to apply the values.  When I get a little closer to utilizing the data value objects, I'll have a better idea.  Thanks for the pointer/suggestion!

error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’


How to implement moderation of data entered through user profile pages?

Question: I am in need of a means of moderating data entered through a user's profile page, or at least for certain specified fields, both upon initial submission and after any revision. These are fields that have been added to the basic user profile (example.com/user/#), editable through the "my account" link -- fields that can be added through Home » Administration » Configuration » People » Account settings.
The most important fields to moderate involve text entry fields and selected taxonomy terms. By moderation, I mean administrative approval of the changes before they go live on the site. My preference would be for the old data to remain visible pending approval of the new data.
I hired a web development company to create a site that includes a directory, with the explicit instruction that moderation of directory data was necessary. They nonetheless set the site up such that the information is provided through fields on users' profile pages and did not incorporate any means of moderation.
I have seen a potential work-around that involves the installation of the Profile2 and Profile2 Moderation modules, but simply implementing those modules does not fix the problem and creating new profile pages would unfortunately risk breaking the site.
If you have any ideas, I would be very grateful to hear them.
Comment: Can you edit your question to explain what your specs about that "moderation" means to you. And also explain what you mean with "profile pages", like is it OK to assume that they are just user "entities", with "some" extra fields added to them. If so, what kind of fields. And if you say "certain fields", what kind of fields are they? Also, if a user EDITs some of the fields content you want to moderate, what should the (visible) field value be while the moderation process is ongoing (ie while it is not approved yet)). PS: Do you want another dozen of question to "gather your requirements"?
Answer: What I've done in a similar situation was this:

Turn on moderation of users /admin/config/people/accounts ie:
Who can register accounts?
--> Visitors, but administrator approval is required
Install Views Bulk Operations https://www.drupal.org/project/views_bulk_operations
Create a view that shows the moderated users by default (but you can use an exposed filter to change this). Include the fields that you want the administrator to be able to see (your text entry and taxonomy referred to above). Set your view to the VBO options and enable the operations for approving (or blocking) users.

Hope this helps!
Comment: That seems like a reasonable way to monitor changes to the fields, but not to pre-moderate changes that a user makes to the fields after they complete the registration process. But as they say, every little bit helps.
Answer: I suggest you try to use a combination of 2 approaches, as further detailed below.
Approach 1 - Prior approval required
The Profile 2 Moderation module, which depends on the Profile 2 module, enables moderation on user profiles. Here is a (self explaining?) screenprint from its project page.

Be aware however that, as mentioned on the project page, it requires a Profile revisions patch.
Disclaimer: I've never used this approach myself (yet ...), but worth invetigating!
Approach 2 - After the facts
Use the Rules module to be informed about all sorts of updates that you're interested in, similar to what's detailed in my answer to "How to send an eMail notification when a specific field of a node or user has been changed?".
With this approach, and as per all the great things you can do with this module, your options are virtually unlimited. This approach also allows you to implement this type of moderation using a granular approach: start small, maybe only using a few custom rules, and keep refining them and continue adding new ones.
Not to forget all the available integrations with tons of other modules, of which the Flag module is probably your very first priority to also add that module for what you're trying to implement. Just a basic usecase about this:

Use Rules to flag any user for which some profile update happened.
Use Views to build a list of such flagged users.

If you'd only implement these 2 bullets of this approach (a matter of minutes, say an hour at max), you'd already have a Version 1.0 of your User Profile Moderation Dashboard (call it your MVP if you prefer ...).
Approach 3 - Build your own spam filter
You could also use an approach/strategy similar to what I described in my answer to "How to use the Rules module to perform a simple anti-spam validation using specific keywords?", at least for anything that is text-field like. For those situations it would be an ongoing process to keep refining the various text strings you want to stop from being entered.
If you're not familiar with Rules, checkout the video tutorials Learn the Rules framework. And/or the similar set of 8 video tutorials about the Flag module.
Comment: Thanks for your suggestions. The Profile2 moderation activation has no effect on the data going immediately live. It is probably the case that if the site were build with those modules from the start, moderation would work. Unfortunately it was not. I may have to try to rework the site around those modules.... As for the second suggestion, that seems like a means of monitoring changes as opposed to moderating them, which would still leave me in the situation of having the data go live before it was moderated. Am I misunderstanding? Thanks.
Comment: @Aaron You're correct in your understanding, but I just thought of yet another approach (= 3), which I just added to my answer. So while waiting for some day to implement approach 1, you could use approach 2 to get inspiration for all sorts of things you want to intercept with Approach 3.
Comment: I will likely have to take that sort of approach. Thank you for all of your ideas.


Getting FW/1 version info

Question: I have been able to get other system information, but how do I get FW/1 version info? I have tried
Application info
<cfoutput>

Java Version: #CreateObject("java", "java.lang.System").getProperty("java.version")#

ColdFusion Version: #server.ColdFusion.ProductVersion#

FW/1 Version:

</cfoutput>

<cfdump var="#application#">

Answer: You can look it up in framework.cfc. It's in the variables scope:
#variables._FW1_VERSION#

Getting FW/1 version info


Show that a point lies on the diagonal of quadrilateral

Question: In a quadrilateral ABCD we choose a point E on the side AD and a point F on the side CD. Then we choose a point G on the line EF. Let H be the second point of the intersection between the circles that have the triangles EGD and BGF inscribed. Show that the point H lies on the diagonal BD.
Can anyone solve this?
Comment: That cannot be true in general. If we change the position of $G$, $H$ moves on a circle through $B$ and $D$.
Comment: If it were true you would have to have ED parallel to BF
Comment: Sorry I forgot to name the point on the line EF. I updated now!
Comment: So it cant be proven that the point lies on the diagonal?
Comment: No see my answer
Answer: Consider the problems the other way round. Start by drawing two intersecting circles and label the points of intersection $G$ and $H$.
Now draw a straight line through $G$ meeting one circle at $E$ and the other at $F$.
Now choose points $D$ on the $E$ circle and $B$ on the $F$ circle.
Clearly we can construct this so that $DHB$ is not a straight line, and obviously we can construct the points $A$ and $C$, thus fulfilling the rubric of the question.
Therefore the hypothesis as originally stated is not generally true.


Add user picture to profile 2/any entity for that matter...

Question: I've disable user pictures in User account and want it displayed in Profile2.  I can add a field for Image.  But, it's not recognized as the official user picture; so it can be used in comments, node views, etc.  How can I do that?
Answer: You have two options.
You can use theming to make whatever you want use the profile picture.
Another options would be to add the picture from the profile to the user object on load.
Third option would be to sync the user picture with the profile picture whenever the profile entity is saved.
If you want it to take effect everywhere, you need to use the third options, which adds the headache of having the same data store twice and the problems such a thing can bring,


Three way join in sql

Question: I have three tables titled "Guest", "Guest_Address" and "Acutal_Address".
Guest_Address is a linking table between guest and acutal_address.
This is what I have so far.
  SELECT GUEST_ADDRESS.ADDRESS_CODE,(GUEST_FNAME+' '+GUEST_LNAME) AS GUEST_NAMES
  FROM GUEST JOIN GUEST_ADDRESS
  ON GUEST.ADDRESS_NUM = GUEST_ADDRESS.ADDRESS_NUM;

This only joins the Guest and Guest_address table, but I need to join Guest and Acutal_Address.
Here is the ERD.
Answer: What you want to do is do an additional join to the actual_address table like so :
  SELECT GUEST_ADDRESS.ADDRESS_CODE,(GUEST_FNAME+' '+GUEST_LNAME) AS GUEST_NAMES
  FROM GUEST
  JOIN GUEST_ADDRESS ON GUEST.ADDRESS_NUM = GUEST_ADDRESS.ADDRESS_NUM
  JOIN ACTUAL_ADDRESS ON GUEST_ADDRESS.ADDRESS_CODE = ACTUAL_ADDRESS.ADDRESS_CODE

Mind if I ask why you have that linking table? Can guests have multiple addresses? If that is the case I would change the join from GUEST_ADDRESS to GUEST based on GUEST_ID, rather than ADDRESS_NUM as using ADDRESS_NUM makes the relationship between the two tables a one-to-one relationship rather than one-to-many
Also worth noting that the query above will not return a record for the guest if there is no address in the ACTUAL_ADDRESS table or link in the GUEST_ADDRESS table due to the JOIN. If you want it to return guest details regardless of address you can simply change the JOIN to LEFT JOIN
Answer: There is a many-to-many relationship between Guest and ActualAddress via the auxilary table GuestAddress. So to join then all together into one set do:
select *
from Guest
left join GuestAddress on GuestAddress.Guest_ID = Guest.Guest_ID
left join ActualAddress on ActualAddress.AddressCode = GuestAddress.Address_Code

Then use the where clause to filter the set down to a smaller one (sub-set), if need be

Three way join in sql


twig to print paragraph field in controller

Question: new to d8. I have a result page where I need to print the field values of a paragraph field into my controller. The paragraph field is called field_classrooms.
text values print fine for example
{{ teacher.field_last_name.value }}
but when I need to print
{{ teacher.field_classrooms.value }}
I get

User error: "target_id" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
Comment: have you tried {{ teacher.field_classrooms|raw}} Also what spits out when you do {{ dump(teacher.field_classrooms)}} ?
Comment: Exception: Object of type Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList cannot be printed    and the dump just gives me the wsod
Comment: What is the field type of field_classrooms?
Comment: it is a paragraph reference, but the field type is `Entity reference revisions`
Comment: Also can you share the code where you're declaring the field_classrooms
Comment: im trying to declare the field_classrooms in the controller twig template. The field is coming from the teacher content type.
Answer: You have to get the paragraph entity with .entity
{{ teacher.field_classrooms.entity.field_in_paragraph.value }}

where you can get the fields inside of the paragraph.
Comment: Thank you! Is there a way to print all of the fields in the entity, without writing out each one?
Comment: I don't know exactly in what kind of twig you are, but usually you should let the paragraph render by itself. If there is a problem with that, ask another question with more details.
Comment: there are multiple fields in the classrooms entity. I want to print all of them rather than typing each out. like so: `{{ teacher.field_classrooms.entity.field_in_paragraph.value }}`  I was hoping to do something more along the lines `{{ teacher.field_classrooms.entity.value }}`
Comment: It should do this.. otherwise there could be something about your setup that you aren't sharing.
Answer: I came accross the same issue while trying to add paragraph field content manual in simplenews email twig template
I too did not find a way to use one twig statement so i used a loop...
Check how many values we have in paragraph
{{ teacher.field_classrooms|length }}
Getting 1st value of paragraph field
{{ teacher.field_classrooms[0].entity.field_in_paragraph.value }}
And this is how I used it finally: Loop trough paragraph items
{% for entity in teacher.field_classrooms %}
    {{ teacher.field_classrooms[loop.index0].entity.field_in_paragraph.value }}
{% endfor %}


WebPacker CI Caching Required Folders

Question: Given a Ruby on Rails project using WebPacker project, which folders need caching on a CI service to ensure system specs perform optimally? My build pipeline is currently caching public/packs-test and tmp/cache/webpacker.
Testing locally I see this behaviour:
time rake assets:precompile RAILS_ENV=test # 2.0m
time rake assets:precompile RAILS_ENV=test # 5.0s
rm -rf ./public/packs-test ./tmp/cache/webpacker
time rake assets:precompile RAILS_ENV=test # 2.0m

This is promising - compilation times are 2 minutes initially, then 5 seconds subsequently. However, on CI I consistently see 2 minute runtimes for asset compilation. Here's the full list of folders I'm caching / restoring between runs:
public/packs-test
tmp/cache/webpacker
tmp/yarn
node_modules

Note: setting YARN_CACHE_FOLDER ENV variable to tmp/yarn and using CircleCI.
Edit:
To cache using the following snippets are used in the CircleCI configuration.
type: cache-restore
keys:
  - assets-{{ .Branch }}-{{ .Revision }}
  - assets-{{ .Branch }}
  - assets

type: cache-save
key: assets-{{ .Branch }}-{{ .Revision }}
paths:
  - public/packs-test
  - tmp/cache/webpacker
  - tmp/yarn
  - node_modules

Comment: How do you save and restore your cache? Can you post that particular part of config.
Comment: Did you ever figure this out? I have the same exact problem.
Answer: The following steps are working pretty well for me:
  - restore_cache:
      name: Restore webpacker cache
      keys:
        - v1-webpacker-{{ .Branch }}-
        - v1-webpacker-

  - restore_cache:
      name: Restore compiled packs
      key: v1-packs-{{ checksum "tmp/cache/webpacker/last-compilation-digest-test" }}

  - save_cache:
      name: Store webpacker cache
      key: v1-webpacker-{{ .Branch }}-{{ epoch }}
      paths:
        - tmp/cache/webpacker

  - save_cache:
      name: Store compiled packs
      key: v1-packs-{{ checksum "tmp/cache/webpacker/last-compilation-digest-test" }}
      paths:
        - public/packs-test

WebPacker CI Caching Required Folders


Método void retorna itens para a lista, mas quando tento usar essa lista, ela está vazia!

Question:  listaPedidoComprador.setLayoutManager(new LinearLayoutManager(this));
    listaPedidoComprador.setHasFixedSize(true);
    adapterPedidoComprador = new AdapterPedidoComprador(itensCarrinho2);
    System.out.println(String.format("quantidade que está no itensCarrinho pro adapter:  %d ", itensCarrinho.size()));
    listaPedidoComprador.setAdapter(adapterPedidoComprador);
    adapterPedidoComprador.setOnItemClickListener(new AdapterPedidoComprador.OnItemClickListener() {
        @Override
        public void onAddClick(int position) {
            ItemPedido itemPedido = itensCarrinho2.get(position);
            Toast.makeText(CompradorPedidos.this, itemPedido.getNomeProduto(), Toast.LENGTH_SHORT).show();
        }
    });
    recuperarItemPedidoJamilton();

    System.out.println(String.format("pedidos recuperados: %d", produtos.size()));
    System.out.println(String.format("quantidade que está no carrinho:  %d nesse exato momento fora do método", itensCarrinho.size()));
    System.out.println(String.format("quantidade que está no carrinho2:  %d nesse exato momento fora do método", itensCarrinho2.size()));
    System.out.println("eae bb");

}

@Override
protected void onStart() {
    super.onStart();
    //recuperarItemPedidoJamilton();
}

private void recuperarItemPedidoJamilton() {
    DatabaseReference produtoref = firebaseRef
            .child("pedidos_usuario")
            .child(idUsuarioLogado);

    produtoref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue() != null) {
                pedidoRecuperado = dataSnapshot.getValue(Pedido.class);
                itensCarrinho2 = pedidoRecuperado.getItens();
                adapterPedidoComprador.notifyDataSetChanged();
                System.out.println(String.format("quantidade que está no carrinhoJamilton:  %d nesse exato momento", itensCarrinho2.size()));
            } else {
                //Toast.makeText(CompradorPedidos.this, "Seu carrinho está vazio", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            // Error
        }

    });
    System.out.println(String.format("quantidade que está no carrinhoJamiltonfora:  %d nesse exato momento", itensCarrinho.size()));

}

Como podem ver, eu estou tentando colocar essa lista no adapter, mas não da certo pois essa lista aparentemente está vazia. Não entendo o que está ocorrendo, já que coloquei um println dentro do método e ele está retornando que há tem itens dentro da lista!!
Já tentei deixar o itensCarrinho2 (lista de  que estou tentando preencher) estático, porém não consigo trabalhar bem assim pois há casos em que é necessário apagar a lista, pois é um carrinho de compras, e ao confirmar ou retirar um item do carrinho, não consegue ser retirado da lista que está estática... eu suponho...
Comment: Olá. Por favor, detalhe melhor a situação que está acontecendo. Eu entendi que você adicionou uma lista ao adapter que inicialmente deve estar vazia, e não está adicionando o item novo nela antes de chamar o `notifyDataSetChanged()` do adapter.
Comment: Estou tentando colocar pro meu adapter a lista itensCarrinho2, como pode ver no código, eu chamo um método pra preencher essa lista (recuperarItemPedidoJamilton();) e eu sei que essa lista está sendo preenchida dentro do método por conta do println que coloquei lá dentro (ele mostra que tem itens na lista usando o itensCarrinho2.size.), mas no momento que chamo pro adapter, ela está vazia
Answer: Entendi agora.
Bom, entenda que itensCarrinhos2 é uma referência para um objeto lista, não o próprio objeto. Você passou essa referência para o adapter e este não espera que ela seja mudada, apenas que mude o conteúdo do objeto em si (ou seja, o conteúdo da lista).
Portanto quando você faz itensCarrinho2 = pedidoRecuperado.getItens(); você aponta essa variável para outro objeto, no caso para o que está sendo retornado por getItens(), só que quando chama notifyDataSetChanged() o adapter não sabe disso e verifica o objeto apontado pela referência passada a ele anteriormente, que não mudou.
Lembre-se que em Java os argumentos são sempre passados "por valor", ou seja, é feita uma cópia do argumento que está sendo passado, no caso a referência para a lista, e o adapter passa a guardar internamente uma cópia dessa referência. Enquanto as duas estiverem apontando para o mesmo objeto ótimo, tudo funciona, mas você mudou isso no seu código quando fez itensCarrinho2 apontar para outro objeto.
A solução portanto é das duas uma, ou alterar somente o conteúdo da lista através da própria referência que o adapter já conhece:
itensCarrinho2.clear();
itensCarrinho2.addAll(pedidoRecuperado.getItens());
adapterPedidoComprador.notifyDataSetChanged();

...ou então procurar um método na classe/interface-base do adapter que permita atualizar a referência que foi passada ao adapter, um setDataSet() da vida ou algo assim, não lembro se tem um.
Comment: *"Lembre-se que em Java os argumentos são sempre passados "por referência""*  olha, que eu me lembro os valores em java são sempre *"pass-by-value"*, pode confirmar isso, só pra não deixar dúvidas? tem até essa pergunta afimando isso no SO: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value
Comment: @RicardoPontual Perdão, eu sempre confundo os dois termos. Você está certo, é por valor mesmo, já corrigi. Por isso particularmente gosto de falar "por cópia".
Comment: MUITO OBRIGADO!!!!!! <3 deu certo aqui :') muito obrigado @Piozevan, de verdade mesmo !!!!
Comment: @JoãoVitor Faça o [tour]
Comment: @RicardoPontual Se bem que falando "por cópia", se não souber que a referência está só apontando para o objeto e não é o próprio objeto irá continuar ambíguo do mesmo jeito.
Comment: sim, isso pode confundir. Eu prefiro como é dito em C#, "by value" e "by reference", me parece que fica mais claro pelo menos pra entender


How to create a stereo sound with independent channels in Adobe Audition?

Question: I'm trying to create a stereo sound with two independent channels. I mean, only the left channel is played via the left speaker, and only the right channel via the right speaker.
First I tried creating two mono sounds, and then inserting each one to a separate L/R channel. But the playback doesn't seem what I want! When I disconnect the left speaker, the right speaker has both the left and right sounds playing.
What should I do!? Am I missing some basics!? Any suggestions !?
Answer: Crerate two mono tracks.  Put one of your mono sounds on one of them, the other or the other.  Pan one track hard L, the other hard R.
Comment: This is exactly what I do @laurence-payne . Maybe you can test it yourself and see if I'm doing something wrong.
Answer: Are you using the same sound on both channels? or are they different sounds/instruments?
If they are the same sound and you want to make the sound wide, you need to add a bit of playback delay to one of them, I mean, moving a bit one of the tracks to the left or the right so they don't play exactly at the same time.
If they are different sounds, just panning one to the left and the other one to the right should be the only thing to do.
Comment: I use different sounds. I do what you say, but I don't know why I hear both sounds in the left speaker (after disconnecting the right speaker).
Comment: what if you use headphones? do you hear each sound panned correctly or u hear both in the center?
Comment: the same occurs with headphone.


convert emoji to their hex code

Question: I'm trying to detect the emoji that I get through e.g. a POST (the source ist not necessary).
As an example I'm using this emoji: ✊ (I hope it's visible)
The code for it is U+270A U+1F3FE (I'm using http://unicode.org/emoji/charts/full-emoji-list.html for the codes)
Now I converted the emoji with json_encode and I get: \u270a\ud83c\udffe
Here the only part that is equal is 270a. \ud83c\udffe is not equal to U+1F3FE, not even if I add them together (1B83A)
How do I get from ✊ to U+270A U+1F3FE with e.g. php?
Answer: Use mb_convert_encoding and convert from UTF-8 to UTF-32. Then do some additional formatting:

// Strips leading zeros
// And returns str in UPPERCASE letters with a U+ prefix
function format($str) {
    $copy = false;
    $len = strlen($str);
    $res = '';

    for ($i = 0; $i < $len; ++$i) {
        $ch = $str[$i];

        if (!$copy) {
            if ($ch != '0') {
                $copy = true;
            }
            // Prevent format("0") from returning ""
            else if (($i + 1) == $len) {
                $res = '0';
            }
        }

        if ($copy) {
            $res .= $ch;
        }
    }

    return 'U+'.strtoupper($res);
}

function convert_emoji($emoji) {
    // ✊ --> 0000270a0001f3fe
    $emoji = mb_convert_encoding($emoji, 'UTF-32', 'UTF-8');
    $hex = bin2hex($emoji);

    // Split the UTF-32 hex representation into chunks
    $hex_len = strlen($hex) / 8;
    $chunks = array();

    for ($i = 0; $i < $hex_len; ++$i) {
        $tmp = substr($hex, $i * 8, 8);

        // Format each chunk
        $chunks[$i] = format($tmp);
    }

    // Convert chunks array back to a string
    return implode($chunks, ' ');
}

echo convert_emoji('✊'); // U+270A U+1F3FE

Answer: Simple function, inspired by @d3L answer above
function emoji_to_unicode($emoji) {
   $emoji = mb_convert_encoding($emoji, 'UTF-32', 'UTF-8');
   $unicode = strtoupper(preg_replace("/^[0]+/","U+",bin2hex($emoji)));
   return $unicode;
}

Exmaple
emoji_to_unicode("");//returns U+1F4B5

Answer: You can do like this, consider the emoji a normal character.
$emoji = "✊";

$str = str_replace('"', "", json_encode($emoji, JSON_HEX_APOS));

$myInput = $str;

$myHexString = str_replace('\\u', '', $myInput);
$myBinString = hex2bin($myHexString);

print iconv("UTF-16BE", "UTF-8", $myBinString);

Comment: I don't want to create a list with all emojis. If there is a code, how did someone else get to it?
Comment: Sorry, I misunderstood your question. I guess you'll need a library for that.
Comment: Well that is still not that what I wrote as a final question ... you just corrected the json_encode and printed it again.

convert emoji to their hex code


Size of football pitches in England and European countries

Question: I recently enjoyed watching home fixtures of Chelsea, Liverpool and Manchester United. Then I thought about the size of football pitch with those played in other European top tier leagues. The ones in English Premier League seem to be small when compared to the stadiums of clubs in Serie A, La-Liga and Ligue 1.
Does the size of the pitch remain the same everywhere? Also would lack of space in metropolitan areas could be a plausible reason why the pitches are small in Premier League or is there any other reason?
Comment: Just to be clear, this isn't a discussion forum - hence the request to limit the question to a single answerable question.  (See the [faq] and the [help] for more about how the site operates).
Comment: I've answered part of your question - does the pitch vary.  I think your question as it stands is several different questions, and should probably be posed separately; in particular the pitch sizes vs. the why of the stadium size are very different questions (and I don't know if the 'why' is answerable, but it's definitely different).
Comment: Thanks @ Joe for the answer. I agree it's made of three different questions. It is apparently related to each other so I framed them in one big question. If you see other European countries, they have hosted Summer and Winter Olympics in different cities. France has hosted Olympics in Chamonix, Paris and Albertville while Germany has hosted the games in Berlin and Munich. Maybe this could have led to infrastructural development and thereby larger stadiums. Any discussion on this is welcome.
Comment: I've edited this to make it just about the pitch size - if you want to ask a separate question on stadium size, please do that (but Old Trafford at least isn't small, at least in terms of capacity)
Comment: Some pitches in Britain are built to host rugby league and union matches as well as football matches. So that would affect the dimensions of the stadium although not necessarily the pitch.
Answer: The pitch most definitely varies in size, but not terribly much.  See this page from the BBC for example.  England's rules are very wide - the pitch must be between 90m and 120m long, and between 50m and 100m wide (!!) - that's a big variety, though - but for major (or even mid-level) play, it's pretty close to standardized.
This page lists various Premiere league field sizes; they're in yards, so slightly larger numbers than meters, but same idea.  Most are about the same size (the Category 4 UEFA size of 115x74 yards).
Most follow this regulation (see this article on dimensions that mentions the possibility of exceptions). Note though that many of the Premiere League teams have identical dimensions to the requirements - 105mx68m - and those that don't are slightly smaller.
So - most likely, they're not really all that different, in the pitch, any difference you notice is due to how the overall stadium is constructed.
Comment: The FA applies the IFAB LOTG, which restrict pitches to width 45-90 metres and strictly less than the length.
Answer: For non international matches, the pitch size can vary quiet a bit. The pitch has to be a rectangle whose width can be anywhere from 45 to 90 meters, while the length can be anywhere from 90 to 120 meters. So a football pitch can almost be a square!
However, the international pitch dimensions are much stricter. The width has to be from 64 to 75 meters and length from 100 to 110 meters. So you'll notice much less variety on pitches that host international matches.
So you see pitches with varying legal sized. But specially in the EPL where many of the pitches are very old, or are in populated areas and size is a constraint (tho the variance tends to be in just a few yards/meters). Newer stadiums usually tend to be at or close to international size, so they can host international matches if need be. You can notice the same phenomenon with MLB ballparks where the stadiums come in all sorts of sizes due to space constraints.
Comment: I've seen it marked as 75 and 76 yards wide which translates to 68.5 and 69.5 meters. Either way, will remove that sentence as it might be misleading.
Answer: I'm not sure it's that true that EPL grounds are that much smaller than European grounds, at least for the "Big Six" (and Wembley):

Stadium
Club
Length (m)
Width (m)

Anfield
Liverpool
101
68

City of Manchester Stadium
Manchester City
105
68

Emirates Stadium
Arsenal
105
68

Old Trafford
Manchester United
105
68

Stamford Bridge
Chelsea
103
68

Tottenham Hotspur Stadium
Tottenham Hotspur
105
68

Wembley
None / England
105
68

Of these, four (plus Wembley) are the regulation international size of 105×68m and the others aren't that much smaller - even Anfield is 96% of the area of a 105×68m field. Running down other clubs in the Premier League, the only other club I could find with a field smaller than 105×68m was Goodison Park (Everton). It is notable that all three of the "small" stadia (Anfield, Goodison Park, Stamford Bridge) are traditional stadia built well before the era of standardisation.
This is in slight contrast to the other major European clubs, all of whom have 105×68m fields:

Stadium
Club
Length (m)
Width (m)

Allianz Arena
Bayern Munich
105
68

Camp Nou
Barcelona
105
68

Juventus Stadium
Juventus
105
68

Metropolitano Stadium
Athletico Madrid
105
68

Parc des Princes
Paris Saint-Germain
105
68

San Siro
A.C. Milan / Inter Milan
105
68

Santiago Bernabéu Stadium
Real Madrid
105
68

(All numbers taken from the stadium pages on Wikipedia)


Console output into html file every X seconds

Question: I have this code in which results get printed in console, and I want to output them into a .html file:
if (result.items.length > 0 && result.items[0].userName === player) {
      const element = result.items[0];
      const usrName = `<br>NickName: ${element.userName}`;
      const rnk = `<br>Rank: ${element.rank}`;
      const eloo = `<br>Elo: ${element.elo}`;
      const winrt = `<br>Win rate: ${element.winPercent}%`;
      console.log(usrName);
      console.log(rnk);
      console.log(eloo);
      console.log(winrt);
      found = true;

I used node file.js > file.html in terminal to do it but I want it to happen automatically every x amount of seconds. Is there any way to do it? I also want to overwrite the last output file.
Comment: you can use something like
   ``const interval = setInterval(function () {
    // Your code goes here
}, 5000);``
change the 5000 to X seconds as you want.
Comment: The above example will execute the code after X seconds. And inside the Loop, you'll still have the same behavior, the only concern here is to output to an HTML file.
When your code is running in a loop then using ``node file.js > file.html`` will write the output of your console to the HTML file.
Now if you want to clear the contents of your file then you are probably looking for the fs module. In each iteration of the loop, you can clear the contents of the file or delete the old one and create a new file to save the output.
Comment: I'd like to export the console log to a .html file with each loop, that's what I'd like to know how to do @MuhammadAdnan .
Answer: Do you just mean print it to the html page that you are looking at?
You can just put a div on the page where you are running this

"
and then
setInverval(function() {
  ...
  const usrName = `<br>NickName: ${element.userName}`;
  const rnk = `<br>Rank: ${element.rank}`;
  const eloo = `<br>Elo: ${element.elo}`;
  const winrt = `<br>Win rate: ${element.winPercent}%`;
  document.getElementById('output').innerHTML = usrName + rnk + eloo + winrt;
}, 5000);

but if you are planning on running a node script that outputs to a file, then you have to set a timeout, replace the last line inside the interval function with:
require('fs').writeFile(
  __dirname + "/file.html",
  usrName + rnk + eloo + winrt,
  'utf-8',
);

and that will delete the file every 5 seconds
... or.. if you don't want to handle writing the file in node, I just figured out from https://unix.stackexchange.com/a/10650 that you could go....
watch -n5 "node file.js > file.html "

It's already installed on my system, but it might not be installed on yours I guess
Comment: Awesome! thank you for your help!


How to setup multiple schedules with cron.yaml

Question: I am working with AWS Elastic beanstalk and have my worker server running and loading job successfully. However I have a cron job that I am wanting to run at multiple hours of the same day but I am stuck just loading the last schedule I've looked through the docs on AWS but they dont seem to cover multiple cron times for the same task
Here is my cron.yaml
version: 1
cron:
  - name: "SetOverdueStatusJob"
    url: "/periodic_tasks"
    schedule: "0 * * * *"

  - name: "Statistics::ComputeDailyKpiJob"
    url: "/periodic_tasks"
    schedule: "* 18 * * *"

  - name: "ProductStatusCheckJob"
    url: "/periodic_tasks"
    schedule: "0 0 * * *"

  # Notify Slack with order count
  - name: "SlackNotifierJob"
    url: "/periodic_tasks"
    schedule: "0 1 * * *" #9
    schedule: "0 6 * * *" #2
    schedule: "0 10 * * *" #6

  # Notify Slack with order count
  - name: "LabelDueDateJob"
    url: "/periodic_tasks"
    schedule: "* 18 * * *"

  - name: "Margin::CalculateJob"
    url: "/periodic_tasks"
    schedule: "* 18 * * *"

With the following I am trying to run all three but it is only loading the last one
  # Notify Slack with order count
  - name: "SlackNotifierJob"
    url: "/periodic_tasks"
    schedule: "0 1 * * *" #9
    schedule: "0 6 * * *" #2
    schedule: "0 10 * * *" #6

Answer: You can schedule multiple cron tasks using one cron expression. You can use crontab.guru to experiment with cron expressions.
To answer your question, you can achieve what you want by this expression
0 1,6,10 * * *
Cheers.


Specific rewrite in htaccess if there is a parameter

Question: I have a WordPress instalation with a different CMS on one folder.
Like this:
https://example.com/ => WordPress
https://example.com/en/ => Custom CMS.

My htaccess is:
RewriteEngine on
RewriteRule ^(en)($|/) - [L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This works fine.
Now, I am moving all the content to the WordPress instalation. I need to preview the changes. I need this:
https://example.com/ => WordPress
https://example.com/en/ => Custom CMS
https://example.com/en/?preview_id=142&preview=true => WordPress

This is, I need to show the WordPress website when there is a "preview" parameter on the URL.
I tried with this:
RewriteEngine on

RewriteCond %{QUERY_STRING} !preview=true [NC]
RewriteRule . /index.php [L]

RewriteRule ^(en)($|/) - [L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

But It doesn't work. Any ideas?
Answer: As you said I need to show the WordPress website when there is a "preview" parameter on the URL and what you did , this RewriteCond %{QUERY_STRING} !preview=true [NC] will make it in an opposite way so, change it to :
RewriteCond %{QUERY_STRING} preview=true [NC]

and also ad ? at the end of target here RewriteRule . /index.php? to avoid appending query string and make looping .
Your rules shlould look like this :
RewriteCond %{QUERY_STRING} preview=true [NC]
RewriteRule . /index.php? [L]

Comment: Take care with such simple regular expression to test the query string against. It _usually_ works fine, but will break in some situations. You want a more robust expression, something like `(^|&)preview=true(&|$)`.
Answer: Please just Delete the .htaccess file.
as soon you delete the file system will create new file auto that will work fine for you.
Comment: This is complete nonsense. "The system", even if it will create a new file (which you don't know), certainly will _not_ create rules to correctly handle the individual requirements the OP pointed out in the question, since "the system" is not a magical one.


Check starts-with for each element in array

Question: Is there any utility function in java which does String.startsWith for each element in string array?
For eg:
String[] s = {"ABC", "BBV", "ABCD", "NBHH"};

Is there any way to do :
array.startsWith("AB");

Returns ABC and ABCD
Comment: lol.  I see you're trying to enforce the python numpy vectorize everything ideology upon Java.  Your choices are to roll your own for loop and iterate every element or try to use one of Java8's mapreduce like features.
Answer: You can use the stream API:
String[] result =
      Arrays.stream(s)
            .filter(a -> a.startsWith("AB"))
            .toArray(String[]::new);

Answer: If you are using Java 8, you can use filter like this :
String[] result = Stream.of(s).filter(a -> a.startsWith("AB")).toArray(String[]::new);

If you want to return a List you can use :
List<String> rst = Stream.of(s).filter(a->a.startsWith("AB")).collect(Collectors.toList());

Answer: You could use Stream if you're using Java 8. In case you aren't then you'd probably want to use the solution below:
String s[]={"ABC","BBV","ABCD","NBHH"};

for (String i : s) {
    if (i.startsWith("AB")) { // startsWith() returns boolean
        System.out.println(i);
    }
}


How can I configure iptables to forward and forget?

Question: I'm trying to create a front controller where users can connect to a specific port and have their connection redirected to a different IP/port.  The reason I'd like to be able to do this is for statistical reasons, since I would like to parse the log files later to see how many connections have been made to specific game servers (which I do not have access to) so I can adjust the 'popularity' ranks of each server based on the amount of connections it received.
I've managed to achieve this by using the follow iptables command:
sudo iptables -t nat -A PREROUTING -p tcp --dport <PORT> -j DNAT --to <DEST_IP>:<DEST_PORT>
This will divert:
<IP>:<PORT> to a remote host <DEST_IP>:<DEST_PORT>.
Example:
mycoolserverdirectory.com:23456 -> somebodyscoolserver:25500
(Each server would be configured to listen on a unique port number).
The problem is, the connection seems to be reliant on the original <IP>, since I noticed shutting down the Ec2 instance will cause the redirected connection(s) to terminate.
I'd like to be able to forward the connection permanently and forget about the original connection once a connection has been forwarded.  Thus acting like a 'router' rather than a proxy.
Note: I had considered using AWS Route 53 for this (using SRV records).  Although I would be able to redirect the connection, it appears retrieving logs/stats isn't possible.
Comment: This isn't possible at the level you are asking, as the destination (and the route to get there) are already setup. You can do some tricks with `vrrp` to provide some high availability. Or using a transparent proxy / router like you mentioned, but that isn't feasible unless you can force all traffic through your server as a route.
Comment: I'm not sure what you are asking, the client goes directly to you're server as their endpoint. If your server goes offline you expect this not to happen?
Comment: Yeah.  Basically I'd like, if possible, to use redirect the connection to the new host/ip.  The same way a HTTP 301 would work in a browser, but at a network level.  I'm not a network engineer so I'm not sure if this is even possible, but I thought I would ask.
Answer: If you don't have access to the specific game servers (which are EC2 instances?) and thus aren't able to assign a static IP (Elastic IP in AWS lingo) to them then you need to be able to detect a change to the IP addresses and refresh the appropriate iptables rule when that happens (DNS names are only resolved when a rule is first loaded).
You could use a cron job similar to one here (I'm not crazy about this python code, but a search for "iptables dynamic DNS" should give some more ideas:
http://diginc.us/linux/2010/using-iptables-with-dynamic-ip-hostnames-like-dyndns-org/
(Note that the iptables manpage states that using hostnames is a "really bad idea" but I don't see how rewriting the rules using an IP from the results of a DNS query is any different, anyway you're working with outbound connections, not permitting inbound).
As far as logging, in iptables the typical practice is to add a matching rule with a LOG destination before the rule in question:
Personally, I'd create a separate chain for each server you want to redirect to:
iptables -t nat -N chain_gameserver1
iptables -t nat -A chain_gameserver1 -m state --state NEW -j LOG --log-prefix "Gameserver 1 new connection"
iptables -t nat -A chain_gameserver1 -j DNAT --to gameserver1.wherever.com:<port>

And then have the cron that checks for IP changes to gameserver1 just flush and reload chain_gameserver1 rather than all your tables.


Can "cross-validation" be used to choose a prior?

Question: To be clear, I doubt I am using the term "cross-validation" correctly here; what I am suggesting also seems similar to "boot-strapping" and "hyperparameter tuning". Terminology is not my strength.
Let's say we have a data set with $20$ observations, $D_1, \dots, D_{20}$. We don't know what prior to use for the data set, so we decide to use the maximum entropy prior given the population mean and variance, i.e. a normal prior. (This of course assumes the population distribution has finite second moment. I am not convinced that this assumption is innocuous, but it is common.)
But of course we don't know the population mean and population variance, so we estimate them. We can't use all of the data to estimate them, because then there wouldn't be any data left to do our inference on. So let's say we use observations $D_1, \dots, D_{15}$ to get an estimate $\hat{\mu}$ for the population mean $\mu$ and an estimate $\hat{\sigma}^2$ for the population variance $\sigma^2$. Then we choose $\mathscr{N}(\hat{\mu}, \hat{\sigma}^2)$ as our prior and then use the remaining $5$ observations $D_{16}, \dots, D_{20}$ to do inference with our prior.
No one would like this situation because we are no longer using all of our data for inference. So:

Question: In this situation would it make sense to:

Calculate priors $\mathscr{N}(\hat{\mu}_1, \hat{\sigma}^2_1)$, $\mathscr{N}(\hat{\mu}_2, \hat{\sigma}_2^2)$, $\mathscr{N}(\hat{\mu}_3, \hat{\sigma}_3^2)$, $\mathscr{N}(\hat{\mu}_4, \hat{\sigma}_4^2)$, the first using the exact same procedure above, the second using an analogous procedure with $D_{11}, \dots, D_{15}$ as the "holdout set", the third using an analogous procedure with $D_{6}, \dots, D_{10}$ as the "holdout set", and the fourth using an analogous procedure with $D_1, \dots, D_5$ as the "holdout set",

Choose as our prior either (a) the convex combination of these above four priors, which would be a Gaussian mixture model I guess, or (b) take as our prior $\mathscr{N}(\tilde{\mu}, \tilde{\sigma}^2)$, where $$\tilde{\mu} := \frac{1}{4}(\hat{\mu}_1 + \hat{\mu}_2 +  \hat{\mu}_3 + \hat{\mu}_4 ) \,, \quad \tilde{\sigma}^2 := \frac{1}{4}(\hat{\sigma}^2_1 + \hat{\sigma}_2^2 + \hat{\sigma}_3^2 + \hat{\sigma}_4^2) \,?$$

The above example generalizes readily, of course (for example I didn't even specify a particular method to get our estimates $\hat{\mu}$ and $\hat{\sigma}^2$), but I thought I would use this concrete example because I don't think I could explain myself clearly in full generality.
(Actually I even doubt that the above concrete example is explained clearly.)

Additional questions: Does something similar to the above procedure already have an established name? And is there any literature either showing its lack of optimality properties or otherwise analyzing it theoretically?

I think this question is different from this related question because that question has both an internal and external source of data. In this example, our parameter estimation and inference are both "competing" for the same data, so we use "cross-validation" or repeated sub-sampling ("bootstrapping") to accomplish the required "hyperparameter tuning" for the prior.
It is also different from the method suggested here which Andrew Gelman argued (probably convincingly, I don't understand the argument to be honest) does not work well. But that method is suggesting use of an "M-estimation" approach with cross-validation to get the prior, i.e. to select the "best-performing" prior from $\mathscr{N}(\hat{\mu}_1, \hat{\sigma}^2_1)$, $\mathscr{N}(\hat{\mu}_2, \hat{\sigma}_2^2)$, $\mathscr{N}(\hat{\mu}_3, \hat{\sigma}_3^2)$, $\mathscr{N}(\hat{\mu}_4, \hat{\sigma}_4^2)$, whereas I am suggesting using some combination of them. This makes more sense to me than the other method, in order to both (a) avoid "overfitting"  and (b) to use more of the data to inform the choice of prior.
Comment: It sounds strange to me the basic concept of using your present data to set a prior. I always thought prior should come from some previous knowledge (i.e. you decide them before seeing your data), like from literature or 'prejudice'. What do you hope to gain with your procedure in comparison to setting a flat prior and use all your data for inference (in case you have no prejudice/idea how to set the priors)? Also, in your procedure, aren't you using your data 'twice' (which would lead to an underestimate of final uncertainty)?
Comment: ok, then change flat for broad gaussian. I cannot imagine a quantity for which you have no idea, even of the order of magnitude. and my point was not that you do not use all the data for inference, but that you use them twice.
Comment: @fabiob If you use a flat prior, you can't make credible intervals. Also, this does use all of the data for inference; that's literally the entire point. There is always unquantifiable uncertainty in the choice of the prior, so that is also a moot point.
Comment: @fabiob Why would a broad Gaussian be appropriate if your sample suggests that the population variance is small? How do you choose a Gaussian that is "broad" enough? Anyway, data is used twice all of the time for bootstrapping and cross-validation and other methods. The whole point of the question is that the double usage proposed is essentially the same as in those methods. The manner in which it is used twice is specifically structured so as to mitigate possible overfitting.  The only reason to avoid using the data twice is to avoid overfitting, so that concern strikes me as being moot.
Answer: Since prior etymologically signifies before:

prior
adjective
existing or coming before in time, order, or importance.
"he has a prior engagement this evening"
synonyms:   earlier, previous, preceding, foregoing, antecedent, advance, preparatory, preliminary, initial.

using the data to build the prior is not correct within a Bayesian perspective. It is found however in the "empirical Bayes" methodology, initiated by Robbins (1955) and defended by Efron, which uses first the data to estimate the parameters in a prior, like your Normal example, and a second time to run a pseudo-Bayesian analysis as if the prior was a true prior. Some versions of this approach enjoy convergence properties, for instance in semi- and non-parametric settings.
The question however seems to shy away from this solution by making a single use of the data and separating it into learning and inference parts. This is connected with the construction of intrinsic Bayes factors in the 1990's, by Jim Berger and co-authors, where a fraction of the data is used to make a flat (or otherwise improper) prior into a proper posterior, and use the remaining fraction to compute a Bayes factor (and run a test decision). In order to avoid the choice of the partition impacting the final result, all possible permutations are considered and one form of average (among arithmetic, geometric, harmonic, median) is computed. A much more elegant alternative is O'Hagan's (1995) fractional Bayes factor where the likelihood $L(\theta)$ is replaced with a fractional power $L^\alpha(\theta)$, which is used to create a posterior and this posterior is then used as a prior for the remainder of the likelihood $L^{1-\alpha}(\theta)$. The difficulty with these approaches is in determining the "right" amount of partitioning, e.g., the value of $\alpha$.
Answer: In this paper Andrew Gelman used cross validation on a corpus of datasets to propose a weakly informative prior, intended for routine use. That seems a sensible approach!
Gelman, Andrew, et al. "A weakly informative default prior distribution for logistic and other regression models." The annals of applied statistics 2.4 (2008): 1360-1383.
Comment: Thank you for pointing this out to me! I'm downloading it to read later
Answer: Firstly, I completely understand the temptation to do this. You want your model assumptions to be defensible and your model to be valid out-of-sample.
The issue is: this is not Bayesian inference. This is regularization using Bayes theorem. That isn't "good" or "bad." The answer to your titular question is yes absolutely. The only thing I would say is that there are probably better regularization methods that would be easier and equally valid under cross-validation.
Bayesian priors are not validated, they are solicited. A prior reflects your preexisting beliefs. If you have to validate it, you didn't believe it in the first place and therefore it is not a genuine prior in the true sense of the term.
The related technique of using secondary data sources to generate a prior that has been brought up is what is refereed to as "empirical" Bayes method, using a second separate date set to generate a prior. This works well for hierarchical models and for situations in which you have empirical prior knowledge you can call on.
I don't see how this makes sense if you are taking one data set and splitting it into two specifically for that purpose. It begs the question why not perform inference on the whole data set with a subjective prior? If you're going to infer your prior from a subset of the data using Bayesian inference, that requires a non-empirical prior which you could just use on the entire data set (in other words, you're just splitting Bayesian inference into two unnecessary steps). If you are not using Bayesian inference to generate a prior, then you don't really believe in using Bayesian inference to solve your given problem in the first place. In that case, just use the method you really believe in on the entire data set.
TLDR: if you don't have a prior belief you feel comfortable using, don't use Bayesian inference; just skip to the method you would use to generate the prior and use it across the board.
Comment: I should clarify a bit more:

Technically, Bayesian inference *is* a form of regularization. It is regularization over prior beliefs.

The term "empirical Bayes" is also used to refer to what is basically simplified hierarchical Bayesian modeling. In normal HB, you have a hyper-prior that you pass through your overall data and then your sub-sample to get a robust model of the sub-sample. In empirical Bayes, you skip the hyper-prior and use a Frequentist approach to generate the top-level model.