Exibindo registros em tabela criada dinamicamente

Question: Montei uma tabela dinâmica com dados de uma consulta ao MySQL, quando mostro todos os registros a tabela é exibida corretamente, mas quando solicito somente um registro a tabela não exibe nenhum registro, vejam como está:
    mysql_select_db($database_conCurriculo, $conCurriculo);
    $query_rsRegistro = "SELECT nome, email, celular, id_municipio, id_uf, dt_nascimento FROM candidato WHERE id_candidato = 158";
    $rsRegistro = mysql_query($query_rsRegistro, $conCurriculo) or die(mysql_error());
    $row_rsRegistro = mysql_fetch_assoc($rsRegistro);
    $totalRows_rsRegistro = mysql_num_rows($rsRegistro);

//Pegando os nomes dos campos
$numCampos = mysql_num_fields($rsRegistro);//Obtém o número de campos do resultado

for($i = 0;$i<$numCampos; $i++){//Pega o nome dos campos
    $Campos[] = mysql_field_name($rsRegistro,$i);
}

//Montando o cabeçalho da tabela
$tabela = '

';

for($i = 0;$i < $numCampos; $i++){
    $tabela .= ''.$Campos[$i].'	';
}

//Montando o corpo da tabela
$tabela .= '
';
while($r = mysql_fetch_array($rsRegistro)){
    $tabela .= '
';
    for($i = 0;$i < $numCampos; $i++){
        $tabela .= ''.$r[$Campos[$i]].'	';
    }
    $tabela .= '
';

    echo $tabela;

}
exit;

//Finalizando a tabela
$tabela .= '
</tabela>';

//Imprimindo a tabela
echo $tabela;

Comment: Você tem certeza que está vindo um registro, ou seja que existe esse candidato 158?
Comment: Olá @bigown, sim esse candidato existe, pois faço uma pesquisa no MySQL e ele está lá.
Comment: Já tentou com outro valor? Faça um teste assim: `$query_rsRegistro = "SELECT \`nome\`, \`email\`, \`celular\`, \`id_municipio\`, \`id_uf\`, \`dt_nascimento\` FROM \`candidato\` WHERE id_candidato = '158'";`
Comment: Olá @user3043340, sim foi um dos primeiros testes que fiz, também não é isso.
Comment: Amigo, seu código aparenta estar correto, porém acho que ele não está exibindo nenhum registro porque não tem nenhum registro, acho que sua Query retorna zero, faça um teste, procure verificar se a variável: $totalRows_rsRegistro possui algum valor antes de fazer a listagem na tabela, outra coisa, tente executar sua Query dentro do Banco de dados para ver qual o retorno.
Answer: Refiz minha consulta montando e exibindo a tabela de uma modo diferente, pois não consegui resolver o problema de exibição dos dados da anterior.
// Consultando candidados no banco
foreach($checkboxes as $id) {

    mysql_select_db($database_con, $con);
    $query_rsRegistro = "SELECT nome, email, celular, id_municipio, id_uf, dt_nascimento FROM candidato WHERE id_candidato = '$id'";
    $rsRegistro = mysql_query($query_rsRegistro, $con) or die(mysql_error());
    $row_rsRegistro = mysql_fetch_assoc($rsRegistro);
    $totalRows_rsRegistro = mysql_num_rows($rsRegistro);

    $Nome = $row_rsRegistro['nome'];
    $Email = $row_rsRegistro['email'];
    $Celular = $row_rsRegistro['celular'];
    $Municipio = $row_rsRegistro['id_municipio'];
    $UF = $row_rsRegistro['id_uf'];
    $Nascimento = $row_rsRegistro['dt_nascimento'];

    $tabela = "

        Nome
        E-mail
        Celular
        Municipio
        UF
        Nascimento

";
    do {
      $tabela .= "

        {$Nome};
        {$Email};
        {$Celular};
        {$Municipio};
        {$UF};
        {$Nascimento};

";
    } while ($row_rsRegistro = mysql_fetch_assoc($rsRegistro));
    $tabela .= "
";

}

Comment: Neste caso, o autor da questão não está adicionando mais informações à pergunta, mas está publicando a solução a qual ele chegou. Portanto, no meu entendimento, esta não deixa de ser uma resposta válida.
Comment: Você pode fornecer mais informação para sua pergunta através do link de editar


Counter-example for abelian category that is not concrete

Question: I am trying to figure out a counter-example for abelian category that is not concrete category. Does the category of representations over a quiver work?
Thanks,
Comment: Well, one way would be to pick a concrete category and forget about the existence of the forgetful functor ...
Answer: The term you want is concretizable (meaning admits a faithful functor to $\text{Set}$). The category of representations of a quiver does not work: if $Q$ is such a quiver with vertex set $Q_0$ and $V$ is a representation, then
$$V \mapsto \prod_{q \in Q_0} V(q)$$
is a concretization.
More generally, a category $C$ is concretizable if it has a small separating family, meaning a family of objects $c_i, i \in I$ indexed by a set $I$ such that the product $\prod \text{Hom}(c_i, -)$ is faithful. In particular, any essentially small category is concretizable. The category of representations of a quiver has a small separating family given by the free representations on each vertex.
On the other hand, any concretizable category must be locally small. An example of an abelian category which is not locally small is the category $\text{Vect}^{\text{Ord}}$ of ordinal-indexed families of vector spaces; this is the category of representations of a "large quiver." But arguably this is cheating: it's reasonable to require that all categories should be locally small (although this comes at the price of not being able to form all functor categories).
As Martin Brandenburg says in the answer to this math.SE question, Freyd showed that the stable homotopy category embeds into a (locally small) abelian category, and since Freyd also showed that the former category is not concretizable, neither is the latter. This is a variation on Freyd's famous result that the (locally small!) homotopy category of pointed spaces is not concretizable.
Comment: Are all ordinals quivers (i.e. free categories)? Certainly ordinals $\le \omega$ are.
Comment: @Zhen: I don't mean functors from $\text{Ord}$, I literally just mean a list of vector spaces, one for each ordinal.


Update C# form RichTextBox content (in realtime) with the output of the batch file execution without waiting for the batch command execution to finish

Question: Below is my C# code in the form.
    private void callInfraCommand(string cmd)
    {
        string os_name = GetOSFriendlyName();
        var m_command = new System.Diagnostics.Process();

        // set up output redirection
        m_command.StartInfo.RedirectStandardOutput = true;
        m_command.StartInfo.RedirectStandardError = true;
        m_command.StartInfo.CreateNoWindow = true;
        m_command.StartInfo.UseShellExecute = false;
        m_command.StartInfo.CreateNoWindow = true;
        m_command.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        m_command.EnableRaisingEvents = true;

        // see below for output handler
        m_command.ErrorDataReceived += proc_DataReceived;
        m_command.OutputDataReceived += proc_DataReceived;

        if (os_name.Contains("Windows 7") || os_name.Contains("2008") || os_name.Contains("Windows 8") || os_name.Contains("2012"))
            m_command.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
        else
            m_command.StartInfo.FileName = @"C:\WINNT\system32\cmd.exe";

        m_command.StartInfo.Arguments = cmd;
        m_command.Start();

        m_command.BeginErrorReadLine();
        m_command.BeginOutputReadLine();
        m_command.WaitForExit();
    }

    private void proc_DataReceived(object sender, DataReceivedEventArgs e)
    {
        // output will be in string e.Data
        if (e.Data != null)
            this.BeginInvoke(new Action(() => deploy_result_txt.Text += (Environment.NewLine + e.Data)));
    }

I am passing the windows batch file to callInfraComamnd and it's block of code is working fine. But, my batch file is taking more than 5 minutes to finish and during that time, my form is freezing and after the batch process completes it displaying all the output of the batch file in chunk.
Ideally, i would like to see the progress (intermediate output of the batch file) in the TextBox while the batch process is running in the background.
Can anyone please advise how can I resolve it (i am using .NET 4) ?
Answer: You need to execute the content of private void callInfraCommand(string cmd) in a Task. The m_command.WaitForExit() blocks the thread calling this function (the UI thread). Since it's blocked it cannot process the marshalled code in proc_DataReceived() before m_command.WaitForExit() finished.
Option 1: don't wait for exit which would block the UI thread
    private void callInfraCommand(string cmd)
    {
        //...
    m_command.BeginErrorReadLine();
    m_command.BeginOutputReadLine();
    // m_command.WaitForExit();
}

Option 2: Call the blocking part in a separate task
private void callInfraCommand(string cmd)
{
    //...

    m_command.BeginErrorReadLine();
    m_command.BeginOutputReadLine();

    Task.Factory.StartNew(
        () =>
            {
                m_command.WaitForExit()
                // code to clean up etc.
                // if there is nothing in here, there's no need to call WaitForExit()!
            }
    );
}

UPDATE:
Option 3: Do something after the process finished which is defined elsewhere.
private void callInfraCommand(string cmd, Action afterFinishedAction)
{
    //...

    m_command.BeginErrorReadLine();
    m_command.BeginOutputReadLine();

    Task.Factory.StartNew(
        () =>
            {
                m_command.WaitForExit()

                afterFinishedAction();
            }
    );
}

Comment: Thanks. In my requirement, I do need to do some other work after command execute successfully. after using your code my form is not freezing but my code is not able to know if the process has finished in the background or not. I am suppose to do other depended work/process after the successful execution of the command execution. Is there anyway, i can check if the process has finished successfully ?
Comment: @Sam Use option 2 and put the stuff in the task. Just beware that you might need to marshal stuff if you access UI variables/controls.
Comment: Is there a way I can track the task completion status from other part of the code ??
Comment: @Sam See updated Option 3. You can now add an `Action`, e.g. a lambda function defined elsewhere and execute it afte the process finished. You may use an `Action` if the action needs data from this method (e.g. an `Action` if you need the exit code from the process).
Comment: Thanks Onur. Options you are provided are really helpful. In my case, I am having trouble handling the thread where WaitForExit() is working. I have series of batch commands which needs to be executed after the successful execution of the first command. I am trying to find a way to track the thread where I am running WaitForExit().
Answer: @Sam Then I suggest you create a task which contains the loop over each process call and leave the waitForExit.


Backing Up Transaction Logs to Tape?

Question: I'm about to put my database in Full Recovery Model and start taking transaction log backups. I am taking a full nightly backup to another server and later in the evening this file and many others are backed up to tape.
My question is this. I will take hourly (or more if necessary) t-log backups and store them on the other server as well. However, if my full backups are passing DBCC and integrity checks, do I need to put my T-Logs on tape?
If someone wants point in time recovery to yesterday at 2pm, I would need the previous full backup and the transaction logs. However, other than that case, if I know my full back ups are good, is there value in keeping the previous day's transaction log backups?
Answer: The old logs may be useful in certain scenarios, like if you discover a bad update operation occurred yesterday and you need to recover the data, you can use point in time recovery of an older full and older logs to recover to the moment when the bad data update occurred and copy out the deleted data. Basically anything that needs an image of the database prior to the last full.
Assuming you have 100% media confidence, ie. the last full backup is 100% guaranteed to be available and will restore, the log backups older than the most recent full are not needed. Whether to keep them longer or throw them away is going to be a decision driven not by restore requirements, but by history retention policy requirements and by the personal level of DBA (healthy) paranoia.
One thing to note is that log backups older than last full are useless without a prior full.
Answer: Personally, I prefer to take my data and transaction log backups to disk, and then replicate them to another set of disks (off-site in my case).  It allows you to be very flexible in your backup and restore process.
Keeping old transaction logs is useful for Point In Time database recoveries.  Depending on your business, it may be useful to restore the database back to a particular transaction from a few days ago (useful for tech support or development rollbacks).
I've used netbackup in the past to take transaction log backups direct to tape but found it incovienient to have to mess about with tapes to recover a backup (tapes are handled by a different team, in a store that is quite a long walk away from the server room).  Tapes are also unreliable, especially when you REALLY need them.
Another problem with backups to tape is that if you do have a serious failure, then there will be a serious shortage of tape drives to allow you to get your backups back.  There will be a whole host of other services and systems that will be on the list and your backups may not be at the top!
Answer:
If someone wants point in time
  recovery to yesterday at 2pm, I would
  need the previous full backup and the
  transaction logs. However, other than
  that case, if I know my full back ups
  are good, is there value in keeping
  the previous day's transaction log
  backups?

You are correct the only purpose of the olkder transaction log would be to recover the older backup for whatever reason.  The amount of time you keep the prior back depends entirely on how much confidence you have in the next backup.  Personally I assume the next backup is going to fail and have at least the last backup available.  I also don't get rid of them until I have to (eg with 15% reserve space I still have 400 GB of space so I might as well keep a backup or 2 there).
Don't forget Heisenberg's golden rule- your data doesn't exist until it exists in 2 places at all times.

Backing Up Transaction Logs to Tape?


sftp server chroot initial directory

Question: I have configured an sftp server with chroot and it works fine, the only thing that is bothering me is the initial landing directory, since the sftp user does not have write permissions in the landing folder he has to go to a folder named after his own username to upload files.
I have set: ForceCommand internal-sftp -d %u, which I found in some configuration guides and they said that it should change the landing folder, but it does not work. At the beginning I had configured a null script for the user login at passwd, but I changed it to /bin/bash in case that it might have had something to do with this. I rebooted the service, so it might not be that the cause also.
Perhaps it might be related to the version of ssh that the distribution I'm using has or perhaps sshd cannot do this at all?
Here is my configuration regarding to sshd:
CentOS release 6.6 (Final)
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 201
Subsystem sftp internal-sftp

Match group sftponly
    ChrootDirectory %h
    X11Forwarding no
    AllowTcpForwarding no
    ForceCommand internal-sftp -d %u

Answer: You can use option -d of sftp which changes the starting directory for you. See sftp-server(8) manual page:

-d start_directory
specifies an alternate starting directory for users.  The pathname may contain the following tokens that are expanded at runtime: %% is replaced by a literal '%', %h is replaced by the home directory of the user being authenticated, and %u is replaced by the username of that user.  The default is to use the user's home directory.  This option is useful in conjunction with the sshd_config(5) ChrootDirectory option.

You can do this by putting this as an argument to internal-sftp, for example:
Subsystem sftp internal-sftp -d "%h/sftp_home/"

Additionally, you might need to adjust your force-command, if you use one:
 ForceCommand internal-sftp -d "%h/sftp_home/"

This should be available on openssh since version 6.2
Comment: This didn't quite work for me on `OpenSSH_7.3p1 Ubuntu-1, OpenSSL 1.0.2g  1 Mar 2016`.  What did work was specifying `Subsystem sftp internal-sftp` (no additional arguments) and then `ForceCommand internal-sftp -d {dir_relative_to_chroot}`.
Comment: @Ryder we have upvotes for that ;)
Comment: Omg— we do? We do! @Jakuje saves the day again.
Comment: Found this today, was driving me nuts that this wasn't working until I dug into the OpenSSH release notes for v6.2: http://www.openssh.com/txt/release-6.2

Thanks!
Comment: OP states 5.3 not 6.2, is there a workaround for 5.3
Comment: @Dejan none I would be aware of. It might work if you do not use `internal-sftp`, but the real `sftp-server` together with some `cd`, but I didn't try that. OpenSSH 5.3 is already very old so it is advised to use more recent versions and systems when setting up some new infrastructure or configure the client.
Comment: If you mean update, I meant the whole system to RHEL7 ...
Comment: @Vic's solution is the only one that worked for me. I'm embarrassed to say how long I searched for this answer.
Comment: The only important difference is to use `%h/` instead of `%h`. Changing the `Subsystem sftp...` line is not necessary.

sftp server chroot initial directory


Finding $\int_0^{\pi/4}\sqrt{1+\left( \tan x\right)^2}dx$

Question: I would like to understand all the steps to find out this integral $$ \int_0^{\pi/4} \sqrt{1+\left( \tan x\right)^2} dx$$ Wolfram Alpha returns: $$ \frac12 \log(3+2 \sqrt2) = 0.881373587019543...$$ Why does it give this value? Thank you.
Comment: Is $1+\tan ^2$ familiar to you? Have you found $\int \sec$ before?
Comment: Yes it is. Yes.
Answer: An alternative approach. By substituting $x=\arctan t$, we get:
$$ I = \int_{0}^{1}\frac{dt}{\sqrt{1+t^2}}=\operatorname{ArcSinh}(1) $$
hence the value of the integral is given by the solution of:
$$ e^{x}-e^{-x} = 2 $$
or by the logarithm of the positive solution of:
$$ y-\frac{1}{y}=2, $$
hence $I=\log(1+\sqrt{2})$ as wanted.
Answer: Here are different steps.

$$ \int_0^{\pi/4} \sqrt{1+\left( \tan x\right)^2} \:dx = \ln\left(1+\sqrt{2}\right).$$

Proof.
For $0<x<\pi/4$, set $\displaystyle t:=\tan\left(\frac{x}{2}\right)$ giving $$\cos x = \frac{1-t^2}{1+t^2},\quad x=2 \arctan t, \quad dx=\frac{2}{1+t^2}dt.$$
Then
$$
\begin{align}
\int_0^{\pi/4} \sqrt{1+\left( \tan x\right)^2} \:dx & = \int_0^{\pi/4} \frac{\sqrt{\cos^2 x+\sin^2 x}}{\cos x} \:dx \\\\
& = \int_0^{\pi/4} \frac{1}{\cos x} \:dx \\\\
& = \int_0^{\tan(\pi/8)} \frac{1+t^2}{1-t^2}\frac{2 \:dt}{1+t^2} \\\\
& = \int_0^{\tan(\pi/8)} \left( \frac{1}{1-t}+\frac{1}{1+t}\right)dt \\\\
& = \ln \left( \frac{1+\tan(\pi/8)}{1-\tan(\pi/8)}\right)\\\\
&= \ln\left(1+\sqrt{2}\right)\\\\
& \approx \color{blue}{0.88137358701954 \dots}\\
\end{align}
$$ where we have used $\displaystyle \tan(\pi/8)=\sqrt{2}-1$ obtained from $\displaystyle \tan(2x)=\frac{2\tan x}{1-\tan^2 x}$ with $x=\pi/8$ knowing that $\displaystyle \tan(\pi/4)=1.$
Comment: @N3buchadnezzar I think the OP knows that since $00.$ Thanks.
Comment: Sneaky! Again hiding the drop of absolute value :p Eg $\sqrt{1+\tan^2x} = \frac{\sqrt{\cos^2x+\sin^2x}}{\sqrt{\cos^2x}} = \frac{1}{|\cos x|}$ ^^
Comment: Indeed, it is obvious. But for a novice in integration -- and not someone as skilled as yourself -- it is worth pointing out. Also $\log (1+\sqrt{2}) = \frac{1}{2}\log[(1+\sqrt{2})^2] = \frac{1}{2}\log (3 + 2\sqrt{2})$ =)
Answer: Using $1+\tan^{2}x=\sec^{2}x$ gives
$\int_0^{\frac{\pi}{4}}\sqrt{1+\tan^{2}x}\,dx=\int_0^{\frac{\pi}{4}}\sec x\,dx=\big[\ln(\sec x+\tan x)\big]_0^{\frac{\pi}{4}}=\ln(\sqrt{2}+1)-\ln 1=\ln(\sqrt{2}+1)$


How to smooth frequency spectrum of time series?

Question: I tried to reproduce Watson's spectrum plot from these set of slides (PDF p. 30, p.29 of the slides), that came from this data of housing building permits.
Watson achieves a very smooth spectrum curve in which it is very easy to tell the peak frequencies.
When I tried to run a FFT on the data, I get a really noisy spectrum curve and I wonder if there is an intermediate step that I am missing.
I ran the fourier analysis on python, using scipy package fftpack as follows:
<code>from scipy import fftpack

fs = 1 / 12 # monthly
N = data.shape[0]
spectrum = fftpack.fft(data.PERMITNSA.values)
freqs = fftpack.fftfreq(len(spectrum)) #* fs

plt.plot(freqs[:N//2], 20 * np.log10(np.abs(spectrum[:N//2])))
</code>
Could anyone help me with the missing link?
The original data is:

Below is the Watson's spectrum curve, the one I tried to reproduce:

And these are my results:
Comment: I'd assume he's used one of many [spectral density estimation](https://en.m.wikipedia.org/wiki/Spectral_density_estimation) techniques. Maybe I missed it but did not see which one he used in that document you linked. Possibly a parametric one (such as an AR model) to estimate the autocorrelation coefficients, then use the ACGF to interpolate.
Comment: Notice that Watson's peaks are evenly spaced?  The spectral resolution in his plot is extremely low, and those peaks are sample positions, between which values have been interpolated.  There are only 7 frequencies in the graph. It looks like the method he used to produce this graph would show exactly the same peaks, with different amplitudes, no matter what the data was.
Answer: The posted curve doesn't look realistic.  But there are many methods to get a smooth result with a similar amount of "curviness", using various kinds of resampling and/or plot interpolation.
One method I like is to chop the data into segments (windows, possibly overlapped) roughly 4X longer than the maximum number of "bumps" you want to see, maybe a bit longer.  Then window each segment before using a much longer (size of about the resolution of the final plot you want) zero-padded FFT.  Then average the results of the multiple FFTs of the multiple windowed segments.  This works because a zero-padded FFT is (almost) equivalent to a highest-quality Sinc interpolating low-pass filter.


Beautifulsoup: how to iterate a table

Question: I am trying to extract data from a dynamic table with the following structure:
Team 1 - Score - Team 2 - Minute first goal.
It is a table of soccer match results and there are about 10 matches per table and one table for each matchday. This is an example of the website in working with: https://www.resultados-futbol.com/premier/grupo1/jornada1
For this I am trying web scraping with BeautifulSoup in Python. Although I've made good progress, I'm running into a problem. I would like to generate a code that would iterate data by data each row of the table and I would get each data to a list so that I would have, for example:
List Team 1: Real Madrid, Barcelona
Score list: 1-0, 1-0
List Team 2: Atletico Madrid, Sevilla
First goal minutes list: 17', 64'

Once I have the lists, my intention is to make a complete dataframe with all the extracted data. However, I have the following problem: the matches that end 0-0. This implies that in the column Minute first goal there is none and it doesn't extract anything, so I can't 'fill' that value in any way in my dataframe and I get an error. To continue with the previous example, imagine that the second game has ended 0-0 and that in the 'Minutes first goal list' there is only one data (17').
In my mind the solution would be to create a loop that takes the data cell by cell and put a condition in 'Score' that if it is 0-0 to the list of Minutes first goal a value for example 'No goals' would be added.
This is the code I am using. I paste only the part in which I would like to create the loop:
page = BeautifulSoup(driver.page_source, 'html.parser') # I have to use Selenium previously cos I have to expand some buttons in the web
table = page.find('div', class_= 'contentitem').find_all('tr', class_= 'vevent')

teams1 = []
teams2 = []
scores = []

for cell in table:
    team1 = cell.find('td', class_='team1')
    for name in local:
        nteam1 = name.text
        teams1.append(nteam1)

    team2 = cell.find('td', class_='team2')
    for name in team2:
        nteam2 = name.text
        teams2.append(nteam2)

    score = cell.find('span', class_='clase')
    for name in score:
        nscore = name.text
        scores.append(nscore)

It is not clear to me how to iterate over the table to be able to store in the list the content of each cell and it is essential to include a condition "when the score cell is 0-0 create a non-goals entry in the list".
If someone could help me, I would be very grateful. Best regards
Comment: Could you edit your question to show what the html table looks like, and what does not work in your code ?
Comment: Could you improve your question and also add an expected output, please.
Answer: You are close to your goal, but can optimize your script a bit.

Do not use these different lists, just use one:
data = []

Try to get all information in one loop, there is an td that contains all the information and push a dict to your list:
for row in soup.select('tr.vevent .rstd'):
    teams = row.select_one('.summary').get_text().split(' - ')
    score = row.select_one('.clase').get_text()

    data.append({
        'team1':teams[0],
        'team2':teams[1],
         'score': score if score != '0-0' else 'No goals'
     })

Push your data into DataFrame
 pd.DataFrame(data)

Example
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd

driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')
url = 'https://www.resultados-futbol.com/premier/grupo1/jornada1'
driver.get(url)

soup = BeautifulSoup(driver.page_source, 'html.parser') # I have to use Selenium previously cos I have to expand some buttons in the web

data = []

for row in soup.select('tr.vevent .rstd'):
    teams = row.select_one('.summary').get_text().split(' - ')
    score = row.select_one('.clase').get_text()

    data.append({
        'team1':teams[0],
        'team2':teams[1],
        'score': score if score != '0-0' else 'No goals'
    })

pd.DataFrame(data)

Comment: Thanks a lot, HedgeHog. It works perfect!

Now I'm having a 'new' problem. If you see the web (https://www.resultados-futbol.com/premier/grupo1/jornada1) there are buttons to expand the events with the scorers and the minute of goals. I've seen the html and these events are in:

    tr.league-match-events

I'd like to get the first goal and the minute, only the data of the first row. But it doesn't work. I try to follow the same logical than the previous loop with select and select_one but it selects all the goals and minutes, not the info in the first row. Any suggestion?

Beautifulsoup: how to iterate a table


Get the filtered data of an UiGrid

Question: I know that we get the filtered rows for an uigrid like :
var filteredData = $scope.gridApi.core.getVisibleRows($scope.gridApi.grid);
but how can i loop into it, i need to get an array like  what we get using
$scope.gridoptions.data.
Any ideas ?
Answer: I suppose you could use forEach for that:
<code>filteredData.forEach(function(row) { ... do something here; ... }
</code>
This would loop through the visible rows, giving you the possibility to do something with each row individually.
Comment: Note that the 'row' object is in fact a representation of the row itself. To access the data that is displayed within, you would have to use row.entity...
`$scope.gridApi.core.getVisibleRows($scope.gridApi.grid).forEach(function(row) { console.log(row.entity); })`

Get the filtered data of an UiGrid


Nested loop or equivalent

Question: I am trying to make a nested loop, I start with a simple loop and I would like make the same with differents data frames;
This work fine:
  set.seed(123)
    df1= data.frame(date= 1:10,
                vartre=rnorm(10, 30, 4),
                varpre=rnorm(10, 10, 5))

var=names(df1)

for(x in var ) {

plot(df1$date,df1[,x], type="l", main=x)

}

Now, I would like make the same with differents df´s, I tried to do with a nested loop but this dont work, example:
df2= data.frame(date= 1:10,
                varkyt=rnorm(10, 100, 40),
                varkdr=rnorm(10, 50, 5))

df3= data.frame(date= 1:10,
                varwer=rnorm(10, 300, 400),
                varpou=rnorm(10, 1000, 500))

dfs=c("df1", "df2", "df3")
for(i in dfs) {
var=names(i)
    for(x in var ) {
plot(i$date,i[,x], type="l", main=paste(i,x)))
}
}

Thanks in advance
Answer: We can use lapply to loop over the list (mget -returns the list of data.frame from the string identifiers) and plot
out <- lapply(mget(dfs), function(dat)  {
        var <- names(dat)[-1]
        lapply(var, function(x) plot(dat$date, dat[,x], type = "l",
           main = x))
     })

If we need to save it in a folder
path <- "path/to/folder/"
lapply(mget(dfs), function(dat)  {
        var <- names(dat)[-1]
        lapply(var, function(x) {

        png(filename=paste(path, "grafico", x,".png"))
          plot(dat$date, dat[,x], type = "l",
           main = x)
           dev.off()
           }
           )
     })

Comment: Thank! works great! just add a parentheses at  the end
Comment: I am getting problem when I add:  png(filename=paste("graph",i,x,".png"))                                  plot(dat$date, dat[,x], type = "l",
           main = x)), do you know why?
Comment: @Rodrigo What is  the error/issue.  as there are mutiple graphs, it may be better to call `pdf` and have it in a single pdf document with each graph in one of the pages
Comment: Sorry; Error: unexpected symbol in:
"png(filename=paste("graph",dat,x,".png"))
plot"
Comment: @Rodrigo.  What is `i` in the `png`.  In the code, we don't have `i`
Comment: I fix the last error (dat) now I just get: Error: unexpected symbol in:
"
plot"
> > Error: unexpected '}' in " }"
>
Comment: @Rodrigo I think you need a `{}` i.e. `lapply(var, function(x) {png(filename = paste("graph", ....); plot(dat$date, .. })`
Comment: @Rodrigo Can you check if that is the issue
Comment: I did it but no fix the problem When I add a # :   #png(filename=paste("graph",dat,x,".png")) all work fine...
Comment: @Rodrigo I updated with the one that is working for me. Please check.  (change the `path` as your folder path

Nested loop or equivalent


Simple Ito Formula Application to Stochastic Integration

Question: I have the following definitions from my course,
Ito Process
Given an $\mathbb{F}$-Wiener Martingale $W$, we say that a process $X=\{X_t\}_{t \geq 0}$ is an Ito process if there exists an $\mathcal{F}_0$ measurable random variable $S$ and processes $g$ and $f$ such that,
$$X_t = S + \int_0^t g_s ds + \int_0^t f_s dW_s.$$
Ito's Formula
We have $X:[0,T] \times \Omega \to \mathbb{R}$ an Ito process with stochastic differential $dX_t = g_t dt + f_t dW_t$ and let  $u \in C^2$. Then $Y_t = u(t,X_t)$ is also an Ito process with stochastic differential given by Ito's Formula,
$$dY_t = (\partial_t u(t,X_t) + \partial_x u(t,X_t)g_t + \frac{1}{2} \partial_{xx}^2 u(t,X_t) f_t)dt + \partial_x u(t,X_t) f_t dW_t$$

I understand this is very basic, however, I am lacking in understanding. My question is surrounding the application of Ito's formula to examples, even in their most basic form. Consider that I am trying to model the process $X_t = W_t^2$ by Ito's formula. I let $u(t,x)=x^2$ and calculate the partial derivatives, which are of course, $\partial_x u(t,x) = 2x, \partial_{xx} u(t,x) = 2$ and $\partial_t u(t,x) = 0$. When we substitute these partial derivates into Ito's formula, we get
$$dX_t = (2x g_t + f_t)dt + 2x f_t dW_t$$
$$dX_t = (2W_t g_t + f_t)dt + 2W_t f_t dW_t$$
However, the given answer for this question is $dX_t = dt + 2W_t dW_t$. My questions are:

Why do the functions $f_t$ and $g_s$ disappear from Ito's formula? It looks like we are setting $f_t = 1$ and $g_t=0$, why is this?
Why do we make this "change of variables" from $x$ to $W_t$?
Comment: The process $W_t$ itself has $g_t=0,f_t=1$, that is the SDE that it corresponds to. And there is no change of variable really, the point is that $Y_t$ is represented as a function of $t$ and some other process $X_t$, and in this case $X_t$ is $W_t$. The letter $x$ is just introduced as a name for the second argument of $u$. In the form above as you saw, you ultimately replace $x$ with $X_t$, that is what those function evaluations mean.
Comment: Okay, I see. When will it be the case that $g_t \neq 0$ and $f_t\neq1$?
Comment: Just depends what you want to represent your process as a transformation of. It is often convenient to represent it as a transformation of standard Brownian motion but not necessarily.
Answer: You want to model a function of $W_t$. So to apply Ito you should consider the Ito process
$$dX_t=\mu(t,X_t)dt+\sigma(t,X_t)dW_t$$
where $\mu(t,X_t)=0$ and $\sigma(t,X_t)=1$, obtaining $dX_t=dW_t$. If $F(X_t)=X^2_t$
$$\frac{\partial F}{\partial t}=0,\, \frac{\partial F}{\partial w}=2w, \, \frac{\partial^2 F}{\partial w^2}=2$$
Thus, by Ito
$$dF=\frac{\partial F}{\partial t}dt+\frac{\partial F}{\partial w}dX_t+\frac{1}{2}\sigma(X_t,t)^2\frac{\partial^2 F}{\partial w^2}dt=2W_tdW_t+dt$$


Recommend book on elementary geometry

Question: I am seeking recommendations for books on elementary geometry, including Euclidean geometry and analytic geometry.
Answer: So, I'm only 15, but my mom bought me and my sisters 'Life Of Fred' math books, and I really like them. They have a geometry math book that you can check out, but I do not own it so I haven't read it to make sure that it has everything.
I read the website page (https://www.lifeoffred.uniquemath.com/lof-geometry.php#.XsaIsmhKguU) and it says it has Euclidean geometry. The life of Fred series is so good that I wouldn't doubt that it is great.
I hope this helps, but it's mostly just my opinion and I think I read that I'm not supposed to answer with my opinion. (I'm sure this question is an exception though.)
Comment: I formatted your text. It was causes by the spaces you made at the beginning of every paragraph.
Comment: *"I think I read that I'm not supposed to answer with my opinion"*. Well remembered but you're right that there are a few possible exceptions such as this (e.g., questions tagged as [soft-question] or reference requests).

Recommend book on elementary geometry


Updating a Beta prior based on observations from a product of two Independent Bernoulli variables

Question: I'm working on a problem involving Bayesian updating with a Beta prior, but the data I observe comes from a slightly complex source.
Let $X \sim \text{Bernoulli}(p)$ and $Y \sim \text{Bernoulli}(q)$, where $q$ is known. I don't directly observe $X$. Instead, I observe $Z = XY$. I would like to estimate $p$.
Here are some intuition based on some preliminary calculations:

If $Z = 1$, I believe the update on the prior $\text{Beta}(\alpha, \beta)$ should be $\text{Beta}(\alpha+1,\beta)$ since $X$ definitely equals 1.

If $Z = 0$, I deduced that the update should be $\text{Beta}(\alpha+P(X=1|Z=0),\beta+1−P(X=1|Z=0))$.

Can someone provide a detailed proof or justification for these updates? Specifically, I'm interested in the logic behind the fractional update when observing $Z = 0$ and its validity within the Bayesian framework.
Answer: Your intuition here is not quite accurate and the situation is a bit more complicated than the way you are treating it.  Nevertheless, since the parameter $q$ is known, this problem is a simple variation of the standard beta-binomial model used widely in Bayesian analysis.  Since $X \sim \text{Bernoulli}(p)$ and $Y \sim \text{Bernoulli}(q)$ you have:
$$Z = XY \sim \text{Bernoulli}(pq).$$
If we observe IID data $Z_1,...,Z_n$ from this distribution then we obtain the sufficient statistic:
$$\dot{Z}_n \equiv \sum_{i=1}^n Z_i \sim \text{Bin}(n, pq).$$
Taking the prior $p \sim \text{Beta}(\alpha, \beta)$ then gives the posterior kernel:
$$\begin{align}
\pi(p|\mathbf{z}_n, q)
&\propto \text{Bin}(\dot{z}_n | n, pq) \cdot \text{Beta}(p|\alpha, \beta) \\[6pt]
&\propto (pq)^{\dot{z}_n} (1-pq)^{n-\dot{z}_n} p^{\alpha-1} (1-p)^{\beta-1}. \\[6pt]
\end{align}$$
Consequently, the posterior density function is:
$$\pi(p|\mathbf{z}_n, q) = \frac{(pq)^{\dot{z}_n} (1-pq)^{n-\dot{z}_n} p^{\alpha-1} (1-p)^{\beta-1}}{\int_0^1 (pq)^{\dot{z}_n} (1-pq)^{n-\dot{z}_n} p^{\alpha-1} (1-p)^{\beta-1} \ dp}.$$
The scaling constant determined by the integral does not have a closed-form solution so you would need to evaluate it numerically.  Alternatively you could simulate from the posterior distribution using MCMC methods or importance sampling methods.


Group rows on some colums and use SUM

Question: I have a table defined in AX like follows:

I am trying to create an SSRS report using a query to create the DataSet and as Design - PrecisionDesign.
I got this until now:

with groupings :

I would like to visually replicate this query:
<code>SELECT Country
    , City
    , FirstName
    , SUM(AGE)
FROM TABLESSRSGROUPING
GROUP BY Country, City, FirstName
</code>
Resulting in:
Answer: You should only need to group once within the first Row Grouping.
In the Group Properties - group on [Country] then ADD [City] then ADD [Name]
Let me know if this helps
Comment: I must admit i did the same thing 2 mins ago and it worked. I was not sure about this approach but seeing your response made me feel like i am not doing smth completely wrong. Thanks!
Answer: What value do you have in the sum field in the SSRS report?
Is it
Fields!.Age.Value
or
sum(Fields!.Age.Value)
Comment: =SUM(Fields!Age.Value)
Answer: Your report is diplaying the DETAIL for all records.
The Details group has the three lines to the left, indicating that it is showing all lines and NOT grouping like you want.
Change your Details to Group on FirstName and delete your FirstName group (select DELETE GROUP ONLY not the Related Rows and Columns)
On a different note, you're adding AGEs?

Group rows on some colums and use SUM


Referenciar ao "pai" da classe

Question: Como faço para referenciar o "pai" dentro do método? Lembro que no Java era só usar this, mas e em C#?
Exemplo:
public class UPlayer : RocketPlayer
{

    public void SendMessage(object obj)
    {
        RocketPlayer pl = base;//não é possivel
    }

}

Answer: base é usado para chamar um método de uma das classes derivadas, ignorando métodos da classe actual (ou seja, "non-virtual calls"). Para referenciar o próprio objecto cujo código está a ser executado, usa-se this, tal como em java.
RocketPlayer pl = this;

Na minha opinião, a pergunta "Como faço para referenciar o "pai" dentro do metodo?" não faz muito sentido. Não existem dois objectos neste contexto (pai e filho), existe apenas um. Esse objecto é do tipo UPlayer, e esse tipo deriva do tipo RocketPlayer.
É importante fazer a distinção entre tipos (classes, interfaces, etc) e objectos (instanciações desses tipos). Neste contexto, existem 2 tipos mas apenas 1 objecto que pode ser referenciado.

Referenciar ao "pai" da classe


Why do razor compile errors not show up in VS2012 when one does a build, and can it be resolved?

Question: I notice that VS2012 fails to raise errors in Razor Views when one build the web project. If you go into the View, one sees the error highlighted. I use resharper, so it may be VS or Resharper doing this?
Just seems strage that VS does not show these errors at build time.
Any way to resolve?
Many thanks.
Answer: Razor views are not compiled with the application unless you set MvcBuildViews to true in the .csproj file.
For example:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
         DefaultTargets="Build"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
  <MvcBuildViews>true</MvcBuildViews>

Comment: No opinion.  Depends on your development process, your tool chain, etc.  If you have a lot of views this can increase your compile times.


Running Spring batch on multiple databases in parallel

Question: I've created a Spring batch application using Spring boot, and I have a Job with 9 steps. These steps are using a DataSource which I created its bean in a configuration file as follows:
@Configuration
public class DatabaseConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    @Primary
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }
}

This DataSource is using properties declared in the application.yml file:
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_01?zeroDateTimeBehavior=convertToNull
    username: xxxx
    password: ****

So far, all works as expected.
What I want to do, is that I have 4 databases parameterized in a 5th database (db_settings), which I select using an SQL query. This query will return the 4 databases with their usernames and passwords as follows:
+--------+-----------------------------------+-----------------+-----------------+
| id     | url                               | username_db     | password_db     |
+--------+-----------------------------------+-----------------+-----------------+
|    243 | jdbc:mysql://localhost:3306/db_01 | xxxx            | ****            |
|    244 | jdbc:mysql://localhost:3306/db_02 | xxxx            | ****            |
|    245 | jdbc:mysql://localhost:3306/db_03 | xxxx            | ****            |
|    247 | jdbc:mysql://localhost:3306/db_04 | xxxx            | ****            |
+--------+-----------------------------------+-----------------+-----------------+

So instead of running the steps using the database declared in 'application.yml', I want to run them on all the 4 databases.
And considering the volume processed, it is necessary to be able to launch the batch processing on these databases in parallel.
Does anyone know how to implement this?
Comment: I don't have time for a detailed answer right now, but you can combine two neat features to achieve this: the partitionned jobs from Spring Batch the partition key being the datasource id 243, 244, etc) and the AbstractRoutingDataSource from Spring JDBC (which will dynamically select the right datasource from the job parameter initialized with the partition key)
Comment: @KeatsPeeks I'm sorry but I couldn't get anything, I would appreciate if you add more details, or a code example.
Answer: Where is the bounty? :-)

Thanks KeatsPeeks, AbstractRoutingDataSource is a good starter for the solution, and here is a good tutorial on this part.
Mainly the important parts are:

define the lookup code

public class MyRoutingDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        String language = LocaleContextHolder.getLocale().getLanguage();
        System.out.println("Language obtained: "+ language);
        return language;
    }
}

register the multiple datasource

<bean id="abstractDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" />

<bean id="concreteDataSourceOne"
    parent="abstractDataSource"
    p:url="${jdbc.databaseurlOne}"/>

 <bean id="concreteDataSourceTwo"
    parent="abstractDataSource"
    p:url="${jdbc.databaseurlTwo}"/>

So after that, the problem is become to:

How to load datasource config properties when spring startup and config the corresponding dataSource using the config properties in database.
How to use multiple dataSource in spring batch
Actually when I try to google it, seems this is a most common case, google give the suggestion search words - "spring batch multiple data sources", there are a lots articles, so I choose the answer in
How to define the lookup code based on the spring batch jobs(steps)
Typically this should be a business point, You need define the lookup strategy and can be injected to the com.example.demo.datasource.CustomRoutingDataSource#determineCurrentLookupKey to routing to the dedicated data source.

Limitation
The really interesting is actually it is supports the multiple dataSource, but the db settings cannot store in the DB indeed. The reason is it will get the cycle dependencies issue:

The dependencies of some of the beans in the application context form a cycle:

   batchConfiguration (field private org.springframework.batch.core.configuration.annotation.JobBuilderFactory com.example.demo.batch.BatchConfiguration.jobs)
      ↓
   org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration (field private java.util.Collection org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.dataSources)
┌─────┐
|  routingDataSource defined in class path resource [com/example/demo/datasource/DataSourceConfiguration.class]
↑     ↓
|  targetDataSources defined in class path resource [com/example/demo/datasource/DataSourceConfiguration.class]
↑     ↓
|  myBatchConfigurer (field private java.util.Collection org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.dataSources)
└─────┘

So obviously the solution is break the dependency between dataSource and routingDataSource

Save the DB setting in properties
Or involve other approach but not in the primary dataSource

See Also
https://scattercode.co.uk/2013/11/18/spring-data-multiple-databases/
https://numberformat.wordpress.com/2013/12/27/hello-world-with-spring-batch-3-0-x-with-pure-annotations/
http://spring.io/guides/gs/batch-processing/
How to java-configure separate datasources for spring batch data and business data? Should I even do it?
Github to get the codes.


Probability - three-headed dragon and three knights

Question: I wrote a test from probability today and totally screw it.
There was a exercise about a three-headed dragon and three possible knights. Chosen knight must cut off at least two dragon heads to save princess. When he cut off zero or one head he failed. Every knight also use different weapon with different probability to cut head off.
First knight uses a sword with constant probability $\frac{1}{2}$ to cut off one head in a single attempt. He has three attempts.
Second knight uses an axe and has constant probability $\frac{1}{2}$ to hit the dragon. Also when he hits the dragon, he has $\frac{1}{2}$ change to cut off one head or $\frac{1}{2}$ to cut off two heads. He has two attempts.
Third knight uses a bow. His first attempt has hit probability $\frac{1}{3}$, second attempt has probability $\frac{1}{2}$ and third attempt has probability $\frac{2}{3}$. Every successful attempt cuts off one head. He has three attempts.
Which knight should be chosen and what chance to save the princess does each knight have?
I would appreciate step by step solution from which I can learn. Thank you.
Answer: You will need to calculate the probability of each knight cutting off at least two heads.
First Knight: Since he has three attempts and he cuts off either $1$ or $0$ heads per attempt, there are eight possible outcomes for the number of heads he cuts off in each of his three attempts.
$000, 001, 010, 011, 100, 101, 110$, and $111$.
Since he has a $50$% chance in each attempt, all eight possibilities are equally likely, and in four of them he cuts off at least two heads. Therefore his probability of success is $\frac{4}{8}=50$%
Second Knight: There are three ways for the second knight to cut off at least two heads.
Either he cuts off two heads on the first try, which has probability $25$%
Or he cuts $1$ head on the first try and at least $1$ more on try two.  This would have probability $(\frac{1}{4})(\frac{1}{2})=12.5$%
Or he misses on try one and cuts off $2$ heads on try two, which has probability $(\frac{1}{2})(\frac{1}{4})=12.5$%
Overall, his probability of success is $50$%
Third Knight: The third knight also has three ways to cut off at least two heads.
Either he cuts off a head on each of his first two attempts, which has probability $(\frac{1}{3})(\frac{1}{2})=16.67$%
Or he hits, misses, and then hits, which has probability $(\frac{1}{3})(\frac{1}{2})(\frac{2}{3})=11.11$%
Or he misses, then hits twice, which has probability $(\frac{2}{3})(\frac{1}{2})(\frac{2}{3})=22.22$%
Overall, his probability of success is $50$%
So it looks like it doesn't really matter which knight you choose because they all have a $50$% chance of succeeding.  Although Knight 2 does have the best chance of getting all three heads.
Comment: I suppose if the knights wanted to make the dragon suffer - by subjecting him to a slower death AND more time knowing the princess has been rescued - then knight three is the way to go.


VBA macro run-time too long

Question: I have written several subs which are then called from a main sub. Individual subs run very quickly, most are instantaneous (the DoFind sub takes a few seconds to run due to the large amounts of data in the table) however when I run the main sub it takes up to a minute to execute. Any ideas/tips on why this is the case?
Note, I haven't had much experience with VBA (all has been learnt in the past week). There are other macros used, but they are not shown since even the test sub takes approximately 1 minute
Sub DoFind()

    Dim i As Long

    i = 1

        Do While Sheets("Temp").Cells(i, "A").Value <> Empty

                Dim BearingArray(6) As String

                BearingArray(0) = Sheets("Temp").Cells(i, "A").Value
                BearingArray(1) = Sheets("Temp").Cells(i, "B").Value
                BearingArray(2) = Sheets("Temp").Cells(i, "C").Value
                BearingArray(3) = Sheets("Temp").Cells(i, "D").Value
                BearingArray(4) = Sheets("Temp").Cells(i, "E").Value
                BearingArray(5) = Sheets("Temp").Cells(i, "F").Value
                BearingArray(6) = Sheets("Temp").Cells(i, "G").Value

                With Sheets("Calculations")
                    .Cells(17, "K").Value = BearingArray(0)
                    .Cells(19, "O").Value = BearingArray(1)
                    .Cells(20, "O").Value = BearingArray(2)
                    .Cells(23, "O").Value = BearingArray(3)
                    .Cells(22, "O").Value = BearingArray(4)
                    .Cells(26, "O").Value = BearingArray(5)
                    .Cells(17, "L").Value = BearingArray(6)
                End With

                i = i + 1

                    If Sheets("Calculations").Cells(17, "M").Value = "PASS" Then
        Exit Do
                    Else
                    End If
        Loop
                    If Sheets("Temp").Cells(i, "A").Value = Empty Then
                        MsgBox "No available bearing."

                    End If

End Sub

Sub Create_Sheet_Temp()

    ThisWorkbook.Sheets.Add
    ActiveSheet.Name = "Temp"

' This creates a new worksheet called "Temp"

End Sub

Sub Copy_Paste()

    Dim NewTable As ListObject
    Sheets("Calculations").Activate

    Set NewTable = Sheets("Calculations").ListObjects("Full_Bearings_List")

    NewTable.Range.SpecialCells(xlCellTypeVisible).Select
    NewTable.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
    Sheets("Temp").Range("A1").PasteSpecial xlPasteAll
    Application.CutCopyMode = False

        'This sub copies all visible cells from a filtered table and pastes them to the new sheet called "Temp"

End Sub

Sub test()
    Create_Sheet_Temp
    Copy_Paste
    DoFind

End Sub

Comment: If there is not an error, this question may be better placed over at [Code Review](http://codereview.stackexchange.com/).
Comment: Yeah there is no error so will also post the same question there, Thanks
Answer: You can speed up your code by storing the worksheets in variables (ahead of the loop).
Dim TempWS as Worksheet
Dim CalcWS as Worksheet
set tempws= Sheets("Temp")
set CalcWS=Sheets("Calculations")

Also declare the array outside of the loop. Also Id recommend to use numeric column index.
Sheets("Temp").Cells(i, "G").Value

to
    TempWS.Cells(i, 7).Value
Comparing against Empty is not always the best choice, try
... <> ""

EDIT:
For the Copy try t use the destination parameter of the Copy method. Example from the help:
Worksheets("Sheet1").Range("A1:D4").Copy _
    destination:=Worksheets("Sheet2").Range("E5")

VBA macro run-time too long


MySQL tables relationship and the use of md5 hash

Question: I have a MySQL DB with 2 tables:

sample_name (stores name of a file, multiple names for same sample_hash);
sample_hash (stores the hashes of a file, will not store duplicate md5);

(all tables have an id int unsigned NOT NULL auto_increment)
My first option to relate these two tables is by creating an md5 column in both tables and relate them. However this seems to have a downside as I will be duplicating a varchar(32), which can be a waste of space with millions of records.
My second option is to calculate the file hashes first, grab the mysql_insert_id() of the sample_hash table and insert into the sample_name table. This makes sense if the hash in the sample_hash table is new, thus I have the mysql_insert_id() variable at my disposal.
But if the hash already exist in the samples_db, I don't want to store the hash again, so I will have no mysql_insert_id().
Is there an alternative other than searching the id of a given md5 in order to store it in the samples_name table in case the md5 already exist? If so, how can I do that?
Comment: Can you refactor to store the file name and hash in the same table?
Comment: I'm familiarized with refactoring. How does the process work?
Comment: I don't know exactly how you can do that, however do you already have all your fields optimized? I'm thinking of an unique index for the hash on `sample_hash`, and a simple index for the other table. Field should be of type char(32). If you don't find any solution to avoid the search, a least this way it will be a bit faster
Comment: @karamazov, does that answer your question, or do you have other concerns or reservations?
Answer: From the requirements that you describe, there is no need for the sample_hash table at all.
You can keep the hashes in the sample_name table and do all your lookups of hash values in that table.
Comment: I need to keep both filenames and hashes on separated tables because one hash can have multiple filenames. In the meanwhile i've indexed the md5 collumn, probably is the best option.


Como fazer cópia de objetos em java

Question: Tem como fazer uma cópia de objeto em Java?
MinhaClasse mc = new MinhaClasse();

MinhaClasse mc2 = mc;

Mas queria fazer alterações em mc2 sem afetar mc.
Comment: Tentou utlizar a classe Cloneable do Java? http://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html
Comment: MinhaClasse mc = new MinhaClasse();
MinhaClasse mc2 = new MinhaClasse();
já não resolveria
Comment: não resolveria por que eu queria que mc2 tivesse os mesmo valores de mc, sem que eu tivesse que preencher os campos novamente manualmente
Answer: Basta sobrescrever o método Object#clone, mas a sua classe precisa implementar a interface Cloneable:

Por convenção, as classes que implementam esta interface devem substituir Object.clone (que é protegida) por um método público.

Utilizando o exemplo de MinhaClasse utilizado na pergunta, esta ficaria assim:
public class MinhaClasse implements Cloneable {

    // Atributos e métodos

    @Override
    public MinhaClasse clone() throws CloneNotSupportedException {
        return (MinhaClasse) super.clone();
    }
}

Desse modo bastaria chamar o método clone desta forma:
MinhaClasse meuObjeto = new MinhaClasse();
MinhaClasse meuOutroObjeto = meuObjeto.clone();

Answer: O método mais correto para criar cópias, duplicar ou clonar objetos é implementando o método clone() e a interface Clonable. Isto permite definir exatamente os valores que serão copiados e até clonar objeto contidos no objeto principal, se for o caso.
Entretanto, dependendo do contexto, existem outras alternativas que podem ser viáveis. Por exemplo, se a ideia é apenas copiar um Java Bean com atributos simples, getters e setters, então é possível o método cloneBean() da biblioteca Apache Commons BeansUtils. Existe também o método copyProperties() que copia os atributos de um objeto para outro.
Outra opção, se a classe for serializável, é executar a serialização da mesma seguida da desserialização. Pronto, você tem uma cópia exata.
Claro que não é recomendável utilizar os dois métodos mencionados acima para um caso tão simples, mas eles podem ser úteis para o desenvolvimento de frameworks ou funcionalidades bem específicas.
Um dos cenários onde já usei isso foi em um sistema que tinha, literalmente, centenas de Java Beans em constante mudança. Não era viável criar e manter atualizado o método clone() em todas as classes.
Comment: @bruno A linguagem possui um padrão muito claro e definido para criar cópias, o método `clone`. Só porque é possível fazer de outra forma não significa que não exista um padrão.
Comment: @bruno Na verdade, do ponto de vista de Padrões de Projetos, o construtor não é uma boa prática porque você fica muito dependente da implementação. Um método *factory* seria mais interessante, mas do ponto de vista de abstração e polimorfismo, o `clone` é o mais genérico. Dependendo da aplicação das cópias, o clone permite aplicar o padrão de projeto [Prototype](http://en.wikipedia.org/wiki/Prototype_pattern), o que não é possível com um contrutor.
Comment: Lamento discordar, mas o método clone não é "o método mais correcto" para criar cópias. Tudo aquilo que dizes ser vantagem do método clone é possível com o construtor de cópia e sem a necessidade de implementar uma interface . Não vou votar negativamente porque penso que a sugestão de usar o cloneBean é interessante. :)
Answer: Sim, basta definires o método Constructor
class MinhaClasse {
  private String str;

  public MinhaClasse (MinhaClasse outro) {
    this.str= outro.str; // tens acesso directo
  }
}

Todos os objects possuem um método clone que por defeito permite a copia de objectos. As boas práticas ditam que deves definir o teu método de cópia.
Após a definição deste método é simples copiar os objectos. Podes simplesmente fazer o seguinte:
MinhaClasse mC = new MinhaClasse();
....(fazer algo com o objecto)
mc.Update();

MinhaClasse mC2(mc);
....(fazer algo com o objecto)
mC2.Update()  <- esta alteração apenas irá afectar mC2!

Em resposta ao teu comentário. Caso a tua classe MinhaClasse tenha como membros outras classes que não de tipo primitivo (int, string, ...) então essas classes também deverão ter definidos construtores de cópia. Por exemplo.
class ClasseA {

   private ClasseB b;
   private String str;

   ClasseA (ClasseA outro) {
      this.b(outro.b) (aqui tens que garantir que a ClasseB possui um constructor de copia ou método =)
      this.str= outro.str; // tens acesso directo
      }
}

EDIT:
A todos aqueles que estão a sugerir implementar a interface Cloneable e definir o método clone() eu sugiro a leitura dos seguintes artigos. Principalmente a leitura do Effective Java:

"Copy Constructor versus Cloning"
"Effective Java Effective cloning"

Deixo aqui apenas mais uma citação do Josh Bloch

"The Cloneable interface was intended as a mixin interface for objects
  to advertise that they permit cloning. Unfortunately it fails to serve
  this purpose ... This is a highly atypical use of interfaces and not
  one to be emulated ... In order for implementing the interface to have
  any effect on a class, it and all of its superclasses must obey a
  fairly complex, unenforceable and largely undocumented protocol"
Comment: Não é mais fácil dar um `new` em um objeto e settar apenas as propriedades que você precisa a partir do objeto original? Ou filtrar na criação do json?
Comment: O livro Effective Java é muito bom, mas só porque o autor cita alguns problemas e desvantagens do uso de uma determinada funcionalidade da linguagem, isso não significa absolutamente que ela não deve ou não possa ser usado em  diversos cenários mais simples, como é o caso de Java Beans simples. Outros capítulos do livro fazem o mesmo, entrando em discussões bem interessantes e profundas como no caso das diferentes possíveis implementações do padrão Singleton. O importante é entender que cada abordagem tem suas vantagens e desvantagens.
Comment: Um dos grandes problemas de clonar com construtor é o alto acoplamento com a implementação. E se forem feitas implementações polimórficas, os mesmos exatos problemas citados pelo autor podem ocorrer.
Comment: O que fazer quando a classe referencia outras classes dentro dela? As referencias ainda estão apontando para os mesmos objetos. Eu vou ter que programar o clone recursivo?
Comment: Sim, deverá fazer isso recursivamente e será um pouco trabalhoso. Você já parou pra analisar com mais calma o seu negócio pra verificar se uma cópia de um objeto é realmente necessária? Não existem outras alternativas no seu design pra evitar ter que trabalhar com cópias?
Comment: No meu projeto, vou ter enviar um objeto via json, mas tem coisa nesse objeto que eu não queria mandar. Por isso pensei em copiar o objeto, deletar o que eu não queria enviar e fazer o json dessa copia. Vou pensar em outra maneira de fazer isso, mas acho que vou ter que copiar os dados que eu não quero enviar, deletar da classe e colocá-los de volta após a criação do json
Comment: @utluiz, antes de mais obrigado pelo comentário. É importante quando podemos debater este género de questões aqui e podemos participar nesta troca de informação. No comentário que eu fiz à tua resposta, eu referi isso mesmo, que por exemplo o cloneBeans é uma boa alternativa para shallow-cloning. No entanto, mantenho a minha opinião. A interface cloneable tem falhas no seu desenho, basta procurar por essa internet fora que não faltam artigos a indicar o mesmo e a sugerir alternativas. Para mim o problema é apenas o dizer que o melhor método para copiar objectos é o clone(), apenas isso.

Como fazer cópia de objetos em java


Need help in understanding proof "approach" and meaning for "1st order Theory of dense linear orders w/o endpoints is PSPACE complete"

Question: So in my class we are giving a proof for 1st order Theory of dense linear orders w/o endpoints is PSPACE complete.
The proof that it is in PSPACE is basically to reduce TQBF.
Let $\phi = \exists x_1 \forall x_2... \exists x_m \ \psi(x_1, x_2, ...x_m)$
So we provide mapping $x_i \mapsto (x_i \geq x_0)$ and $ x' \mapsto (x_i \geq x_o)'$
Let $\phi' = \exists x_1 \exists x_1 \forall x_2... \exists x_m \ \psi'(x_0, x_1, x_2, ...x_m)$
Not sure how we proved it with this mapping.
But what I want to ask here is to how reason about what we need to proof here. I guess it is the first I need to understand before diving into the actual formal proof.
I understand that in order to proof completeness we need to show that the problem is

is in PSPACE
hard for PSPACE

As I understand PSPACE problem is a problem that can be solved by deterministic TM in order of polynomial space.
So there is a thing that bugs me. What it mean to solve a "problem" by TM where problem is 1st order Theory? So 1st order theory is a set of different logic formulas. What does it mean 1st order Theory to be in PSPACE? Does it mean that we have TM and on input x we can decide whether this formula is in our Theory or it is not in our Theory? If yes, what is the procedure to decide it?
I am confused regarding it A LOT.
Thank you for any help!
Answer: The theory $T_{dlo}$ of dense linear orders without endpoints is complete: for every sentence $S$ of the language $\{<\}$, either $T_{dlo}\vdash S$ or $T_{dlo}\vdash \neg S$. In fact, $T_{dlo}$ admits elimination of quantifiers: for every sentence $\varphi$ of the theory there is an open formula $\psi$ such that $T_{dlo}\vdash \varphi \leftrightarrow \psi$.
In their book "The Computational Complexity of Logical Theories" (1979), Ferrante & Rackoff present a related result and many others on the same theme. There they present their algorithm for producing $psi$ from $varphi$ in the theory of $\mathbb{Q}$ as a field. The algorithm is also described in the latter half of this pdf of slides. (Caution: in that pdf, negation symbols render as empty rectangles, for me.) It proceeds by eliminating existential quantifiers and negations. Where their algorithms introduce a new term $(a+b)/2$, an analogous algorithm for $T_{dlo}$ would introduce a new constant $c$ and conjoin $a < c \wedge c < b$ to the formula being constructed.
So there is an effective procedure for determining whether sentences in the language of dense linear orderings without endpoints is a theorem or not.
The statement you're asking about says exactly the two things you mention:

(deciding $T_{dlo}$ is in PSPACE) There is a Turing machine $M$ which decides theoremhood for sentences of the language, such that the space complexity of $M$ is polynomial as a function of the "size" of input sentences, and that this is a lower bound, in the sense that
(deciding $T_{dlo}$ is PSPACE-hard) There is a reduction of a PSPACE-complete problem (TQBF) the algorithm of 1.


How to format rows to color group by like values in column 1

Question: I have a worksheet that has information like this:
a
a
b
c
c
c

How do I format it so that all of the rows that have a value of a in the first column are one color, then all the rows that have a value of b in the first column are a different color, etc. ?
Edit not from OP to add clarification from comment:
Everything is already sorted alphabetically, and will stay that way, and I want multiple colors.
Comment: it doesn't matter to me what color they are. I am just trying to visually separate the groups so that I can enter that data into another program more easily. I have about 10 different worksheets. One worksheet for each state and then a list of companies in each state
Comment: By using conditional formatting. Try to read the docs, it's well explained.
Comment: I am a novice at Excell. I guess I am trying to find something that will do it automatically? I can go and select the rows I want and just fill in a random color but I am trying to cut out time, and it seems that conditional formatting for each value will take substantially more time.
Comment: But it'd take even more time by doing it manually, and in any case, you have to define what colour will be assigned to what value, right?
Comment: possible duplicate of [Alternating coloring groups of rows in Excel](http://stackoverflow.com/questions/27020/alternating-coloring-groups-of-rows-in-excel) **(look at the highest voted answer which does not use VBA)**
Answer: Create a helper column with a formula like this;
=MOD(IF(A3=A2,0,1)+B2,2)
In this example column A is the column of sorted values to be grouped by, and column B is the helper column.  The formula is entered on row 3.  Set the first row of the helper column to the value 0 and the others to the formula.  This will result in alternating values in the helper column for each group, ie;
a 0
a 0
b 1
c 0
c 0
c 0
d 1
d 1
e 0

You can then set conditional formatting based on the column value.  If the value is 1 then highlight the row; if it is 0 do not highlight it.  Or use alternating colors or whatever.  You can reference any of the articles on the web that describe how to conditional format the entire row based on the value in the column.
IF(A3=A2,0,1) compares the current row (3) and prior row (2) returning a 1 or 0.
MOD( [...] +B2,2) accomplishes the alternation between 0 and 1 when the grouping column value changes.
Answer: I think you need a helper column, say B seeded with 1 in row1, and =IF(A1=A2,B1,B1+1) in B2 and copied down to suit. Then formulae of the kind below should suit for conditional formatting:
Comment: +1 I had a similar need and while your answer isn't perfect it did give me the idea to use `ISODD()` to highlight grouped rows.  Thanks!
Comment: to avoid the helper column one would need to use a unique color (and rule) for each value. I now notice this was suggested in the comments and Kari (I did realize my mistake with pronoun and tried to "ninja-edit" but was too slow). didn't seem to like that approach. If the values will be kept sorted (admittedly not very difficult to do) then this should work splendidly (and then helper column can be hidden). Due to this I must admit my downvote was very premature and if you were to add to your answer somehow, I would be able to change my vote (it's locked down until an edit) as I like it now.
Comment: helper column is not necessary here and from context we can't be sure the values will always be sorted. Plus it sounds like they want multiple colors to tell teh groups apart at a glance
Comment: yes, everything is already sorted alphabetically, and will stay that way, and yes I want multiple colors


can I have a button next to the select option? if can't, can I put the button outside and connect with the option in the select?

Question: What I want is when you select and there are multiple options showing with two buttons. When the button is clicked, I want to take the option value,( which might be id) and do some functions.
Is it possible?
If I put buttons outside next to each option, how can I connect with the option in the multiple select?
Comment: Can I use jQuery ? or pure JS ?
Answer: This is an example using jQuery
<code><select id="choice" multiple="multiple">
  <option value="one">one</option>
  <option value="two">two</option>
  <option value="three">three</option>
</select>

<button type="button">one</button>
<button type="button">two</button>
<button type="button">three</button>

<script>
$('button').click(function() {
  var val = $(this).text(); // one, two, three
  $("#choice option[value='" + val + "']").prop("selected", true);
});
</script>

</code>
Online Verification


Field 'data_log' doesn't have a default value - Não insere dados no banco

Question: Não está inserindo dados só usuário no banco, então usei o comando:
print_r($inserir->errorInfo());

E apareceu este erro:

Array ( [0] => HY000 [1] => 1364 [2] => Field 'data_log' doesn't have
  a default value )

No banco existe o data_log, ou seja a data de criação daquele cadastro, não tem nenhuma forma de ignorar este erro e salvar no banco?
Aliás, salvar mas não criar problemas futuros...
public function inserir($tabela, $dados){

    $pegarCampos = array_keys($dados);
    $contarCampos = count($pegarCampos);
    $pegarValores = array_values($dados);
    $contarValores = count($pegarValores);

    $sql = "INSERT INTO $tabela (";

    if($contarCampos == $contarValores){
        foreach($pegarCampos as $campo){
            $sql .= $campo.', ';
        }
        $sql = substr_replace($sql, ")", -2, 1);
        $sql .= "VALUES (";

        for($i = 0; $i < $contarValores; $i++){
            $sql .= "?, ";
            $i;
        }

        $sql = substr_replace($sql, ")", -2, 1);
    }else{
        return false;
    }

    try{
        $inserir = self::conn()->prepare($sql);
        if($inserir->execute($pegarValores)){
            return true;
        }else{
            return false;
        }
    }catch(PDOException $e){
        print_r($inserir->errorInfo());
    }
}

Answer: Isso acontece devido o mysql-strict que está configurado no seu servidor, desta forma, o campo data_log não pode ficar vazio quando você está inserindo os dados.
Pode desativar o mysql strict desta forma:
set @@global.sql_mode='';

Ou em sua query, inserir algum valor em data_log


In what sense is the term "pigeon" used in tennis

Question: I have encountered the word pigeon in connection with tennis players, for example, have a look here, here or here. It seems to be a slang term - at least I did not find it in any news article.
From the context it is clear that it has something to do with one player having bad H2H against another player. Could somebody give a clearer explanation when this word is used? Etymology of this phrase could also bit interesting. Has this originated from some common idiom in English?
Comment: From your first link, I found this: http://tt.tennis-warehouse.com/index.php?threads/how-would-you-define-this-term-pigeon.507161/
Answer: A player that is easy to beat, an easy prey. If you beat a player too many times, he/she becomes your "pigeon".
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=define+pigeon
NORTH AMERICAN
informal
a gullible person, especially someone swindled in gambling or the victim of a confidence trick.
This term has been commonly used as as a slang as far as the 20's in American English.


Infinite Riemann sum of $x^3$

Question: Write
$$\int_{1}^{3} x^3 dx$$ as a riemann sum.
Here is what I thought:
$$\Delta(x) = \frac{2}{n}$$
$$f(x) = (\Delta(x)k)^3 = \left(\frac{2k}{n}\right)^3$$
$$I = \int_{1}^{3} x^3dx = \lim_{n \to \infty} \sum_{k=1}^{n} \frac{2}{n}\cdot \left(\frac{2k}{n}\right)^3$$
But the sum evaluates to $4$ instead of $20$ which is the actual value...?
Comment: Maybe you should do $(1+\Delta(x)k)^3$, the 1 since you want to start at 1.
Answer: You forgot 1:
$$I = \int_{1}^{3} x^3dx = \lim_{n \to \infty} \sum_{k=1}^{n} \frac{2}{n}\cdot \left(1+\frac{2k}{n}\right)^3$$

Infinite Riemann sum of $x^3$


How do I properly add settings to an iOS app with a Cordova project?

Question: In a Cordova build I am trying to add settings for my iOS application. I have the settings already defined in a Settings.bundle/Root.plist file. All I want is the Settings.bundle copied over to the XCode project along with its children.
I'm trying to do this with a resource-file statement:
<platform name="ios">
    <resource-file src="res/ios/Settings.bundle" />
    ...
</platform>
When I do my build, the folder is added 3 times to my project instead of only one time, and none of the files inside that folder is copied over. I see in the log output that there are 3 CpResource statements executed for this resource.
CpResource Anamnese\ mobil/Resources/res/ios/Settings.bundle build/emulator/Anamnese\ mobil.app/Settings.bundle
CpResource Anamnese\ mobil/Resources/res/ios/Settings.bundle build/emulator/Anamnese\ mobil.app/Settings.bundle
CpResource Anamnese\ mobil/Resources/res/ios/Settings.bundle build/emulator/Anamnese\ mobil.app/Settings.bundle

This is consistent with what I'm seeing in the resulting XCode project: The folder is there 3 times, but it has no contents. I played around with the location of the Settings.bundle and the number of file inside to see whether there is a relation to the number of times this is executed, but to no avail.
So, my question is, how do I properly add settings to an iOS app with Cordova?
Or, if I stay on this level of analysis: How do I properly copy folder structures into an iOS project?
Answer: I didn't find why resource-file added the Settings.bundle three times. But I figured out another way to integrate it into the app: I had to declare an own cordova plug-in that listed this Settings.bundle.
David Souther wrote a detailed instruction here: Cordova Phonegap IOS App Settings.Bundle Possible?
To install me.apla.cordova.app-preferences I had to also include this line into my cordova.xml
<plugin name="cordova-plugin-app-preferences" spec="https://github.com/apla/me.apla.cordova.app-preferences" />


Show that $\mathfrak{sp}_{2n}(\mathbb{C})$ is a sub lie-algebra of $\mathfrak{gl}(\mathbb{C}^{2n})$

Question: I want to show that $\mathfrak{sp}_{2n}(\mathbb{C})$ is a sub lie-algebra of $\mathfrak{gl}(\mathbb{C}^{2n})$. Where $$\mathfrak{sp}_{2n}(\mathbb{C})=\{X \in End(\mathbb{C}^{2n})| \Omega(Xv,w)+\Omega(v,Xw)=0, \text{for all} \ v,w \in \mathbb{C}^{2n}\},$$ where $\Omega(x,y)$ is given by $x^TMy$ with $M=
\begin{pmatrix}
0 & I_n\\-I_n & 0\end{pmatrix} \in M_{2n}(\mathbb{C}).
$  Furthermore, I need to know what form does $X \in \mathfrak{sp}_{2n}(\mathbb{C})$ explicitly have in regards to a block matrix.
To show that  $\mathfrak{sp}_{2n}(\mathbb{C})$ is a sub lie-algebra, I tried to show that $[X,Y]=XY-YX \in  \mathfrak{sp}_{2n}(\mathbb{C})$, So $$\Omega((XY-YX)+\Omega(v,(XY-YX)w)=\Omega(XYv-YXv,W)+\Omega(v,XYw-YXw)=$$
$$=\Omega(XYv,w)-\Omega(YXv,w)+\Omega(v,XYw)-\Omega(v,YXw).$$ However, I do not know how to proceed and to show that it is actually zero. Additionally, I am not sure how to derive a form for an arbitrarily $X\in  \mathfrak{sp}_{2n}(\mathbb{C})$. Thanks for your help!!
Comment: First show that $X \in \mathfrak{sp}_{2n}(\mathbb{C})$ iff
$$
    X^T M + MX = 0
$$
Answer: This is more transparent if you make the following observations:

$X\in{\mathfrak g}{\mathfrak l}_{2n}$ belongs to ${\mathfrak s}{\mathfrak p}_{2n}$ iff $X^\dagger = -X$, where $(-)^\dagger$ means the transpose with respect to $\Omega$, i.e. $\Omega(X,-)=\Omega(-,X^\dagger)$. (If you replace $\Omega$ with the standard scalar product, this is the ordinary transpose)
The transpose has the property that $(XY)^\dagger=Y^\dagger X^\dagger$.

From that, you can calculate that for $X^\dagger = -X$ and $Y^\dagger = -Y$, also $[X,Y]^\dagger = -[X,Y]$.
Comment: @Deavor: Do you have any questions?
Comment: Thanks for your asking. Somehow I did not get a notification. I did it as Matthew stated in the comments.
Answer: Hint: If $X, Y \in \mathfrak{sp}_{2n}(\mathbb{C})$, then $\Omega(XYv,w) = -\Omega(Yv, Xw) = \Omega(v, YXw)$.


Custom Error Handler

Question: I have searched the web and stitched together an error handling solution that doesn't work mainly because I don't understand fully how the exception pipeline works. I used different guides, but I didn't get any of theme to work for me. What I want the error handler to do is this. I have a class called workplanRepo where all my queries are executed. I have covered all queries with a try and catch block. What I want is when an error occurs is for an exception to be thrown that allows me to customize a specific message for each query and the default exception message. I then want to be able to retrieve the messages in the error view that the exception handler has redirected the user to. I would also like a default handler that catches all other errors. but don't necessarily have the custom message part. If anybody could explain or show me how I can achieve this. I would be very grateful!. This is one of the query methods:
<code>try {
    newItem["Author"] = _user.Id;
    newItem["Title"] = _user.Title;

    newItem.Update();
    clientContext.ExecuteQuery();
    }
    catch (Exception e) {
      throw new HttpException("Oops, there must have been an error: " + e.Message);
    }
</code>
Comment: Take a look at this article https://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine#ExceptionhandlinginASP.NETMVC(6methodsexplained)
Comment: Which also influenced my answer here https://stackoverflow.com/questions/38247900/custom-error-pages-with-layout/38249656#38249656
Answer: In ASP.NET MVC 5, we can catch error inside Global.asax.cs's Application_Error event instead of using try catch block in every query. From then redirect to custom error page.
In addition, we can also use logging framework like Log4Net and NLog.
For example,
<code>    protected void Application_Error(object sender, EventArgs e)
    {
        var exception = Server.GetLastError();

        LogException(exception);

        if (exception is HttpAntiForgeryException)
        {
            Response.Clear();
            Server.ClearError();
            Response.TrySkipIisCustomErrors = true;

            // Call target Controller and pass the routeData.
            IController controller = EngineContext.Current.Locator.GetInstance<CommonController>();

            var routeData = new RouteData();
            routeData.Values.Add("controller", "Common");
            routeData.Values.Add("action", "AntiForgery");

            var requestContext = new RequestContext(new HttpContextWrapper(Context), routeData);
            controller.Execute(requestContext);
        }
        else
        {
            // Process 404 HTTP errors
            var httpException = exception as HttpException;
            if (httpException != null && httpException.GetHttpCode() == 404)
            {
                Response.Clear();
                Server.ClearError();
                Response.TrySkipIisCustomErrors = true;

                // Call target Controller and pass the routeData.
                IController controller = EngineContext.Current.Locator.GetInstance<CommonController>();

                var routeData = new RouteData();
                routeData.Values.Add("controller", "Common");
                routeData.Values.Add("action", "PageNotFound");

                var requestContext = new RequestContext(new HttpContextWrapper(Context), routeData);
                controller.Execute(requestContext);
            }
        }
    }

    private void LogException(Exception ex)
    {
        if (ex == null)
            return;

        // Ignore 404 HTTP errors
        var httpException = ex as HttpException;
        if (httpException != null &&
            httpException.GetHttpCode() == 404)
            return;

        try
        {
            // Log error message
        }
        catch (Exception)
        {
            // Don't throw new exception if occurs
        }
    }
</code>
You can view sample project which use Log4Net at GitHub.
Comment: Thank you very much!!! it worked. Do you know how to check a viewbag for content and if not null show a modal. I know how to use the viewbag in a razorview but I never seem to be able to fuse razorview/C# with javascript
Comment: Could you create a new question and elaborate a little bit more? It will be helpful if you provide a sample code on where you get stuck.
Comment: I tried to create a question, but there is a 90min timer until I can create a new question. I`ll do it tomorrow

Custom Error Handler


How would I monitor if a new entity had been saved?

Question: I am creating a module that I want to run when an entity is saved. I have used rules in the past but the overhead on the module means that I cannot use it. Is there another way that I can do this?
Thanks
Answer: If you need it to run for all entities, then your module can implement hook_entity_insert() for new entities and hook_entity_update() for changes to existing ones.
If you just need to target specific ones (node, user, etc), then most of the modules that implement entities also have insert and update hooks.
Comment: @Andrew the docs pages have got examples that should help
Comment: Dang beaten to the punch ;-p +1 for the better descriptions
Comment: Is there a possibility to give a small example of how it would be implemented?
Comment: I think I got my head around it after digging around in the docs. Thanks
Answer: hook_entity_insert() sounds like what you're after.

Act on entities when inserted.


What is the difference between multivariate analysis and econometrics?

Question: I am writing my thesis about how different factors influence stock excess returns after an M&A announcement. I haven't taken a multivariate analysis class and I have only a very basic idea of econometrics. I am not sure which one of those two will be what I need to learn to build my model.
In general, I will have one dependent variable (excess returns within specific period of time) and many explanatory variables, such as size of the acquirer, institutional ownership, different aspects of corporate governance etc. I want to see what influence, if any, each of those variables have on the dependent variable. What should I learn to be able to build my model?
Answer: To Expand: Econometrics includes Multivariate Analysis as a tool (a mathematical one). At the same time it may include many other things, such as economic "fundamental" models.
Econometrics is also a certain spin on (applied) statistics, just as biostatistics (one could say biometrics) or statistics in medicine, information theory or whatever field you can imagine. The unique problems faced in any field shape the tools needed for statistical analysis.
For example Econometrics is a very frequentist, non Bayesian field (at least how it is taught) - or at least one that doesn't teach two distinct approaches. The reason for that is that the data for most problems* lends itself to classic, frequentist analysis (lots of data points available, targets are constant mechanics). It is also a pretty standard approach in econometrics to learn OLS and then the General Method of Moments.
Panel Data models (Fixed/Random Effect) are, until needed, more of a fringe thing that comes up as a sidenote in most classes.
The data is generally considered continous. Logit, Probit and models which target nominal or ordinal variables are also not at the heart of what you learn in most Econometrics courses. Of course they are taught eventually and are important for economic fields which stray more into experiments or social areas but usually dependent variables are in money units.
Many medical people will learn things like ANOVA very througoutly. Econometricians consider this to be a spin on linear regression and maybe never learn it as a distinct thing.
Before those things come up, students will study time series analysis very througoutly, with Hamilton (1994) being the premier book in that area.
Even if you study econometrics to a graduate level, many methods on this site will seem foreign to you at first. It is a applied take on statistics that goes deep where it needs to and omits things that are not important in the field of economics.
In your case I would say that learning Econometrics is the right thing to do. It includes the stuff you need to analyze problems such as the one you posted.
My recommendation would include three books on three different levels.
The first one is Introduction to Econometrics by Stock and Watson. It is written for economics undergrads without further interest in statistics - just the basic stuff without going deep in the math. On the other hand it brings you - method wise - up to topics like cointegrated analysis.
Next up are Econometrics by either Hayashi or Green. These are the standard econometric graduate textbooks. If things are not proven or analyzed completely, they at least refer to the mathematical texts that do. Those give you a pretty good understanding of the usual econometric problem.
Last up is Time Series Analysis by Hamilton. It pretty much includes everything you could know at the point when it was published (1994), though it is more on a late graduate or phd level at least for (non quant-) economists.

*One could very well argue about this though
Comment: Very good overview! Although Bayesian approach is getting more and more interest in econometrics, especially in empirical macroeconomics and finance, though it is not usually taught yet in undergrad courses.
Comment: I come from a 100% frequentist background in econometrics and am currently trying to get a grip on the Bayesian approach myself. I read on this site that many consider it intuitive. I disagree. It seems nobody can really define what it is and what is doing differently. You need some grounding in the terminology of information science to even be able to read the wikipedia article. It goes so far that it is so unclear defined that I literally don't know if I am doing Bayesian stuff already (as I clearly learned some of it in basic stats). There should be a book for people like me. I feel so dumb
Comment: Example: I just read in a paper (Jaynes) that in one experiment the researcher was doing a hypothesis test over the means of two variables (ie. which one is better) with normal distribution. He made some mistakes but what Jaynes picks out is that he tests over the whole interval (including B>A, which is not supported by data)  instead of restricting the hypothesis to just A=B and A>B beforehand (so that B>A is not a possibility).
Comment: This seems so amazingly arbitrary given the distribution of the variables is known. I mean there is a reason we pick this kind of interval which is we are unsure that the data reflects the truth. And given a known distribution, I don't understand how we can just cut out half of the distribution. I doubt Jaynes is in error but I don't get it. At the present, for me, you either come from a science that already does Bayes and are familiar with the terminology and procedure (mostly experimental sciences), or it is really unintuitive
Comment: I mean I can see the argument that we the hypothesis test leaves out some information ie. that the data would never support B>A, but given that the data itself is only one experiment or - given the distribution, we can not assume it is accurate, this is unsure information and calls to question the whole validity of his approach. Sorry for the derail ;)
Answer: Econometrics is a specialized branch of applied statistics.  Multivariate analysis is a branch of mathematics that has a lot of applications to statistics.  For a great econometrics intro (at the beginning PhD level), I recommend Mostly Harmless Econometrics.
It doesn't cover everything by any means, but if you're starting from scratch with no stats background but with decent math skills, it'll get you started.


DataGridView com colunas de diferentes tabelas de um DataSet

Question: Importei 16 tabelas do meu MSSQL em um DataSet e fiz todos os relacionamentos necessários.
Estou precisando gerar um relatório em um DataGridView (ou outra ferramenta) com colunas específicas de diversas tabelas.
Como posso fazer isso?
Comment: Pode colocar o código que tem, mesmo que esteja incompleto ou com erro.
Comment: Bom, pra falar a verdade, ainda não tenho código algum.. rs
Queria o ponto de partida mesmo.
Answer: Finalmente consegui.
Para gerar os relatório eu usei o Crystal Report e para o problema de login eu adicionei a seguinte linha no meu App.Config:
<startup useLegacyV2RuntimeActivationPolicy="true">

Me desculpem a falta de informações e detalhes, mas estou numa baita correria!
Obrigado a quem respondeu!
Answer: Você pode utilizar ReportViewer para gerar o relatório
http://paulohdsousa.blogspot.com.br/2012/01/relatorios-com-reportviewer-e-linq-c.html
http://www.devmedia.com.br/gerando-relatorios-atraves-do-reportviewer/5371
https://code.msdn.microsoft.com/windowsdesktop/Gerando-relatrios-com-815c3396
http://www.macoratti.net/pageview.aspx?catid=9
Comment: Vou tentar utilizar. Obrigado pelas referências!
Comment: Acabei utilizando o Crystal Reports pra gerar o relatório e consegui exatamente o que precisava à partir de um XSD que criei, mas na hora de executar o programa ele pede senha, mas o arquivo não está protegido. O que faço?


In how many different ways can I sort balls of two different colors

Question: Let's say, I have 4 yellow and 5 blue balls. How do I calculate in how many different orders I  can place them? And what if I also have 3 red balls?
Comment: maybe a title more clear could be "In how many different ways can I sort balls of two different colors"
Comment: @Justin L., That's probably a fair assumption, yeah.
Comment: So, you're asking for how many unique ways you can sort them?  And swapping two red balls in a certain arrangement doesn't count as a unique new sorting.
Answer: This is a standard problem involving the combinations of sets, though perhaps not very obvious intuitively.
Firstly consider the number of ways you can rearrange the entire set of balls, counting each ball as indepndent (effectively ignoring colours for now). This is simply $(4 + 5)! = 9!$, since the 1st ball can be any of the $9$, the 2nd can be any of the remaining $8$, and so on.
Then we calculate how many different ways the yellow balls can be arranged within themselves, since for the purpose of this problem they are considered equivalent. The number of combinations is of course $4!$; similarly, for the blue balls the number is $5!$.
Hence, overall we find:
$$\text{total arrangements} = \frac{\text{arrangements of all balls}}{\text{arrangements of yellow balls} \times \text{arrangements of blue balls}}$$
Therefore in our case we have:
$$\text{total arrangements} = \frac{9!}{5! \times 4!} = 126$$
I'm sure you can see how this can be easily extended if we also have 3 red balls too. (Hint: the total changes and we have another multiple of identical arrangements to account for.)
Answer: The case of two colors is simple: if you have m yellow balls and n blue ones you only need to choose m positions among (m+n) possibilities, that is (m+n)!/(m!·n!). The other balls' positions are automatically set up.
Answer: For some reason I find it easier to think in terms of letters of a word being rearranged, and your problem is equivalent to asking how many permutations there are of the word YYYYBBBBB.
The formula for counting permutations of words with repeated letters (whose reasoning has been described by Noldorin) gives us the correct answer of 9!/(4!5!) = 126.