Clearing cache breaks site

Question: I'm trying to load a production site in a dev environment using the site's codebase (core 8.3.7, contrib and custom modules and vendor folder) and the current database.
After struggling a bit with it, I managed to import the DB (it's PGSQL with a lot of extensions), and the site was up. I looked into the status report page and besides a few update warnings, there are no errors.
First time I try to clear cache, the site breaks and starts complaining about missing modules (one of them made sense, the other is the basic_auth module, which is in core). I manually cleaned up the missing one from the DB, but I'm getting a log of PluginNotFoundExceptions, which to me, says I must have done something wrong. I've tried importing the DB again and I traced the caching issues down to router cache and views cache. All of the other caches are cleaned with no issues, but these two throw the above exceptions.
I've tried to install the codebase again via composer, respecting the .lock file with composer update --lock, and it says there is nothing to install/update.
I'm a bit lost, and the only way I can make the site work (even if momentarily) is by importing the DB again, but it breaks after cache clearing and/or configurations being exported, either via drush or via backend.
AFAICT, even if the codebase was in fact missing something (ie, someone installed something in production not managed by composer), it would show up in the contrib folder and by importing the DB it should be fine, right?
Does anyone have some suggestions on where to start looking for the problem?
Comment: Did you install Drupal after importing the codebase and before importing the database?
Comment: Nope. I just copied the codebase and imported the DB right after it. should I have done anything else in between? I will now try emptying all cache tables in the DB right after import to check if it works.
Comment: Emptying the cache tables also does not work. The errors are still there. If I `drush cr` I get a PluginNotFoundException about a `list` plugin, related to the `group` module. Although, when I try accessing the site and the `drush ws`, I get the same exception but regarding a `language` plugin.
Comment: You need to actually install Drupal before importing your database into it. I hope this helps, cheers.
Comment: Can't install. The install script stops at the "Check requirements" stage, telling me to check the messages and try again, but no messages show up.
Answer: Are you sure there are no differences in the codebase? This would explain the issue. The plugins/routes are cached in the database until you clear the cache. Then they are rediscovered from the codebase and the missing plugins or routes become visible. It's not necessarily that the code is missing physically, it can also be a problem with autoloading.
If there is a problem with syncing the codebase by git/composer, use another tool, for example rsync. Make sure to sync the complete codebase, also the files in the root folder.
Comment: Thank you for your feedback. I just `scp`'ed the whole root folder contents to my local root folder. After that, I dropped the DB and created it again, using all the extensions being used by PGSQL in the production server. If, before importing the DB, I delete `settings.php` and try to install, I get a "list" plugin not found exception.
Comment: Right now, the only differences I can think of relate to the stack. The production server is using CentOS and I'm using Ubuntu 16.04 locally. When I tried `composer update --lock` locally I always get a "nothing to install or update" message and it then creates the autoloader again.
Comment: Delete all files and scp again (or use rsync --delete), because not only missing files can cause problems, also leftover files from older versions.
Comment: That was exactly my first procedure for exactly those reasons, sorry I wasn't clear on that aspect. :)
Answer: Had a very similar issue with a missing "list" plugin on D8.4.2.  I discovered that my issue was caused by an OSX update to 10.13.1 which seriously messed with my vagrant/virtual box/composer/drupalvm local environment. My files were not syncing between host and guest machines apparently caused by some issues with NFS.  One indication of this issue is that I had missing modules on the module list page.
Here is a link to where I found my solution: https://github.com/geerlingguy/drupal-vm/issues/1547
Ultimately, the fix was upgrading to 10.13.2 beta, then I deleted core and contrib modules manually and ran composer update to replace. I had no issues with the beta version of OSX.
Hope this helps.

Clearing cache breaks site


Need SQL query to show the data in the below format

Question: I have columns like id, name as below
id name
1  A
1  B
1  C
1  D
1  E
1  F
2  A
2  B
2  C

I want to show data like
ID A B C D E F G
1  Y Y Y Y Y Y N
2  Y Y Y N N N N

Where :
Y = YES - EXISTS
N = NO- DOES NOT EXIST
Comment: What is your DBMS ? MySQL, Oracle, ... ?
Comment: I think you can't do it directly with sql.
Take a look at this example maybe it helps :
select
 case MyFlag
   when 1 then 'YES'
   when 0 then 'NO'
   else 'OOPS'
 end
Answer: SELECT
      id,
      MAX( CASE when name = 'A' THEN 'Y' ELSE 'N' END ) A,
      MAX( CASE when name = 'B' THEN 'Y' ELSE 'N' END ) B,
      MAX( CASE when name = 'C' THEN 'Y' ELSE 'N' END ) C,
      MAX( CASE when name = 'D' THEN 'Y' ELSE 'N' END ) D,
      MAX( CASE when name = 'E' THEN 'Y' ELSE 'N' END ) E,
      MAX( CASE when name = 'F' THEN 'Y' ELSE 'N' END ) F,
      MAX( CASE when name = 'G' THEN 'Y' ELSE 'N' END ) G
   from
      yourTable yt
   group by id

Comment: @Sravan, you are welcome... also, as a newbie to the site, check out HELP -> TOUR for site eitquette


If a 4x4 matrix A is diagonalizable, is A+4I diagonalizable as well?

Question: The matrix $I$ is the $4 \times 4$ identity matrix.
If this is true, how do I show that? If it's false, how would I find a counterexample?
Answer: Hint:
$$
P(D + 4I)P^{-1} = PDP^{-1} + 4I
$$


SendKeys.SendWait permission denied error when RDP is closed

Question: I'm trying to launch a program and I have to enter login and password into it.
To simulate keystrokes I'm using the next code:
System.Windows.Forms.SendKeys.SendWait(login + "\t" + password + "\n\r");

Unfortuantely, I am to do all these actions on a server with Windows Server 2008 R2 x64 OS and I have to use RDP. When RDP connection is active, everything is OK, but if the RDP window is closed or minimized, SendWait invokes an exception with message 'permission denied'.
How can I emulate keystrokes when RDP is disconnected?
Thanks.
Comment: That sounds fair, there is no desktop anymore to send the keys to.
Answer: You could use the method described in this by David Johnson
Create a batch file with the following commands and save it to the desktop of the workstation you want to keep unlocked: I have named my file Logoff.bat
START C:\Windows\System32\tscon.exe 0 /dest:console
START C:\Windows\System32\tscon.exe 1 /dest:console
START C:\Windows\System32\tscon.exe 2 /dest:console
START C:\Windows\System32\tscon.exe 2 /dest:console
START C:\Windows\System32\tscon.exe 3 /dest:console
START C:\Windows\System32\tscon.exe 4 /dest:console
START C:\Windows\System32\tscon.exe 5 /dest:console

Tscon.exe comes standard with your windows installation and is specifically created to leave a Previously-Locked Console Unlocked. See this link: http://support.microsoft.com/kb/302801
The next time you log in to the workstation with remote desktop, do not log out the normal way, but run you batch file - in my case "Logoff.bat" This will terminate your Remote Desktop connection and log you out, but will then continue in an unlocked state for any scripts to run as if someone is actually logged in.


Can Zermelo's theorem be extended to a game which always has a winner?

Question: Zermelo's theorem states that in a finite game, with two players, $a$ and $b$, where each player takes turns, the following is true:

Player $a$ has a winning strategy,
Player $b$ has a winning strategy,
Both players have an at-least-drawing strategy.

Now consider the following game: Either player $a$ wins or player $b$ wins but they cannot draw, so the game continues indefinitely until one of them wins.
Does Zermelo's theorem hold?
My initial thought and an obvious solution is: No, because the Zermelo's theorem specifically states that the game must be finite. However, I'm struggling to see why this doesn't work like so:

Player $a$ has a winning strategy,
Player $b$ has a winning strategy,
Both players have an at-least-not-winning strategy (i.e. game continues forever).

Can someone provide a proof for why this extension of Zermelo's theorem does not work in this new game?
Comment: You want at-least-not-losing in 3.
Comment: I think those three things should be stated as "at least one of the following is true."  The way it is stated sounds like all are claimed to be true.
Comment: What if the game deterministically has each player scoring 0 points per round, and the game continues until one player accumulates 10 more points than the other.  So the game never ends and nobody ever wins.
Comment: First, Zermelo's theorem applies to games with finitely many positions, the game need not be finite. Second, so who is going to win if the game is going on forever?
Answer: Something similar, but not quite identical, to what you want is true.
It is not possible to guarantee strategies which ensure that the game continues forever, if neither has a winning strategy. For example, consider the really sad game whose only moves for either player are "pass" or "resign." Neither player has a winning strategy - all they can do is refrain from resigning - but neither player can ensure that the game goes on forever, either (since each player could resign at any given moment).
However, what we can guarantee is that at least one player has a non-losing strategy (i.e. when they play according to that strategy, either they win or the game continues forever). This follows from the Gale-Stewart theorem; this is a significant strengthening of Zermelo's theorem, and represented the first real step into the land of infinite games.
Call your game $G$. Consider the related game $G^*$ where players $a$ and $b$ play the game you've described, and if it goes on forever then player $b$ is said to win. This is an example of an open game, and the Gale-Stewart theorem says that every open game is determined. A winning strategy for $a$ in $G^*$ is also a winning strategy for $a$ in $G$; meanwhile, a winning strategy for $b$ in $G^*$ is a non-losing strategy for $b$ in $G$. So we see that one player or the other has a non-losing strategy in your game $G$, as desired.
So in fact we have the following "lopsided" determinacy principle:

In a (perfect-information, $2$-player) no-draws-possible but not-necessarily-terminating game $G$, either player $a$ has a winning strategy or player $b$ has a non-losing strategy.

There are essentially two proofs of the Gale-Stewart theorem. One is a "ranking" argument, where we assign nodes in the "game tree" ordinals (or "$\infty$") in a recursive way. The other "magical" one is the following: looking at the game tree, color a node red if player $a$ has a winning strategy from that node and blue otherwise. Now show that if the root of the game tree (= the beginning of the game) gets colored blue, then $b$ has a non-losing strategy. HINT: think about what color the nodes directly above a given blue node can get, based on the parity of that node (even length or odd length).
There are much, much stronger determinacy principles out there - see in particular Borel determinacy, due to Martin. The general study of determinacy principles is a major topic in set theory and descriptive set theory, and has surprising connections to large cardinals and "regularity" properties (like measurability) of sets of real numbers. See this essay of Larson.


Raid array Devices are not removed

Question: cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [linear] [raid10]
md11 : active raid10 sde1[3] sdj1[4](F)
      286743936 blocks 64K chunks 2 near-copies [4/1] [___U] [1:1:3:0]
      [=>...................]  resync =  8.0% (23210368/286743936) finish=67874.1min speed=64K/sec

umount /dev/md11
umount: /dev/md11: not mounted

mdadm --stop /dev/md11
mdadm: fail to stop array /dev/md11: Device or resource busy

mdadm --manage --set-faulty /dev/md7 /dev/sdh1
mdadm: set /dev/sdh1 faulty in /dev/md7

mdadm -r /dev/md7 /dev/sdh1
mdadm: hot remove failed for /dev/sdh1: Device or resource busy

cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [linear] [raid10]
md11 : active raid10 sde1[3] sdj1[4](F)
      286743936 blocks 64K chunks 2 near-copies [4/1] [___U] [1:1:3:0]
      [=>...................]  resync =  8.0% (23210368/286743936) finish=67874.1min speed=64K/sec

Answer: I'm no expert in this, but isnt the RAID rebuilding?
Comment: i know is not rebuilding , how to slove this
Comment: No, it is rebuilding. "resync =  8.0%"
Comment: is not modified this is stop , i am absorve from 3 days it shows same "resync =8.0%"
Comment: can u try this create raid 10 with 4 raid devices, after that mount to one directory, in directory add one file and some texts and save it. after that remove two raid devices one by one. after that edit the contents of file of already mounted. and try remove for other raid devices. ( i follow this way i got problem )

Raid array Devices are not removed


Pasting text from GoogleDocs into D7 Body field causes imagestyle error?

Question: Super strange - I'm copying and pasting text from a GoogleDoc into a D7 article post. When I do this and publish my article, it drops onto the main page into a slider that I've created via a View. This works seamlessly usually, however when I paste the text from the googledoc, it actually distorts my entire main page - seemingly stripping the image styles from my other article posts in the slider, thus, ruining my slider? Any idea why this is and how I can fix it?
Comment: It's probably producing dodgy HTML for whatever reason  - check the markup against a validator to be sure
Comment: @Clive Totally the case I'm sure - how do I check it against a validator? And is there anyway to allow for copy/pasting from a google doc?
Comment: validator.w3.org is a good one, I'm sure there are plenty more a search for "find broken HTML" or similar would show up. I'm afraid I haven't tried copying from a gdoc before but there did always used to be a "paste from word" option if that's a viable option
Answer: You may have better luck exporting the Google Doc to "RTF" format first then copy + pasting that format into the article body.
You could also try the "Paste without formatting" option, such as Ctrl+Alt+Shift+V or Ctrl+Shift+V but would need to re-add the formatting (headings etc..) with that approach.
There may also be some WYSIWYG toolbar options to paste with various formatting options (paste from word, paste as plain text etc..)


How do you compute the value of a multivariable limit?

Question: I've been wondering this as I've tried to solve problems by parameterizing variables to f.e x = $\lambda(t)t$ and y = $\mu(t)t$ and reducing by dividing common t factors. I've looked online for methods to compute multivariable limits and I have found no one who shows how to do it and almost all of them just "assume" a limit and use an epsilon-delta proof. Also some use x = $rcos(\theta)$ and y = $rsin(\theta)$, but since $sin^2+cos^2 = 1$ doesn't this not work?
As an example take $\lim_{(x,y)\to(0,0)}\frac{x^3y}{x^6+y^2}$. I then find $\frac{t^4\lambda^3(t)\mu(t)}{t^2(\mu^2(t)+t^4\lambda^6(t))}$ which reduces to $\frac{t^2\lambda^3(t)\mu(t)}{\mu^2(t)+t^4\lambda^6(t)}$ and thus the limit is $\frac{0}{\mu^2(t)+0} = 0$ as t approaches $0$. This limit doesn't exist however as it is path dependent.
Thus my question is: Is there a way to compute multivariable limits without "guessing" them beforehand, and if so how is it done and what is/isn't a correct method?
Thank you in advance!
Answer: I don't think there's a particularly generic approach that will always work. There are a good number of tricks. I'll discuss the $(x, y) \to (0, 0)$ case for simplicity.

Fix $m$, substitute $y=mx$, and let $x \to 0$. If the result depends on $m$, the limit does not exist. This is often a quick-and-dirty way to see if a limit "obviously" doesn't exist, but it cannot prove the limit does exist.
Example: $\lim_{(x, y) \to 0} \frac{xy}{x^2 + y^2}$ does not exist since $\lim_{x \to 0} \frac{x(mx)}{x^2 + (mx)^2} = \frac{m}{1+m^2}$ depends on $m$.

Polar: substitute $x=r\cos\theta, y=r\sin\theta$ and let $r \to 0$. If the result is the same no matter what $\theta$ is doing, the limit exists and is that value. Otherwise the limit does not exist. This approach will often give insight even if it doesn't immediately work out.
Example: $\lim_{(x, y) \to 0} \frac{x^2 y}{x^2 + y^2} = 0$ since $\lim_{r \to 0} \frac{r^3\cos^2\theta\sin\theta}{r^2\cos^2\theta+r^2\sin^2\theta} =
lim_{r \to 0} r (\cos^2\theta\sin\theta) = 0$.

Series expansions. L'Hopital's Rule is fundamentally an example of this philosophy, so I won't write out an example. While L'Hopital's Rule is a single-variable phenomenon, you could still try a multivariable Taylor expansion. I haven't personally seen that in practice.

Inspired substitutions. This requires "insight", whatever that means. Often the idea is something like, "well, if $x$ were really really small compared to $y$, but not too small, then this term would dominate, and the quotient would be basically 1, so..."
Example: $\lim_{(x, y) \to 0} \frac{x^3 y}{x^6 + y^2}$ does not exist since when $y=mx^3$, we have $\lim_{x \to 0} \frac{x^3 (mx^3)}{x^6 + (mx^3)^2} = \lim_{x \to 0} \frac{m}{1+m^2}$ depends on $m$.
Comment: Thanks for your answer. I see the rule is basically: there is no fixed way to find multivariable limits.
Comment: @PimLaeven Sure. In complete generality, we could pick a function $f(x, y)$ which is piecewise-defined on different paths, and we could tune the paths to behave in any way we wished, so looking at behavior of individual paths seems entirely unavoidable. I do wonder if there's a general method to do $\lim_{x, y} \frac{p(x, y)}{q(x, y)}$ where $p, q$ are polynomials with $p(0, 0) = q(0, 0) = 0$. Apparently this is treated in https://www.sciencedirect.com/science/article/pii/S0747717112001204 which looks fascinating, but I don't have time to read it.
Answer: I don't believe there is a rule that fits all multi variable limits. Generally, one looks for continuity at the point and then failing that will apply various different techniques until something sticks (such as polar coordinates, trying many different paths then generalising, and Taylor series).
This is a good thread detailing several ways to approach such problems, hopefully it helps a bit!
Is there a step by step checklist to check if a multivariable limit exists and find its value?
If you're looking to figure out what the limit is to aid proving it, I would try several paths and if all paths tend to the same point then I would assume that to be the result I try to prove. 3D graphing calculators can help with this!!
Comment: Thanks a lot for your answer, gives some good insight and I will definitely check out the thread.


Android como otimizar uma grande cadeia de ifs

Question: Gostaria de saber como otimizar uma grande cadeia de ifs de forma que o código fique mais eficiente e mais legível.
Minha aplicação Android usa a versão 2.3.3 (API 10), nesta aplicação tenho uma activity responsável por exibir as informações da conta do usuário, onde o mesmo pode editar essas informações e atualizar. Essa activity contém 8 campos ao lado de campo tem 1 botão "Editar" e um botão "OK" escondido, se o botão "Editar" for clicado então a aplicação esconde o "Editar" e exibe o "OK" no mesmo lugar. Dessa forma tenho ao total 16 botões, 8 "Editar" e 8 "OK". Que deixaram meu método onClick(View v) com 16 ifs.
Como posso melhorar isso e deixar mais eficiente e mais legível? Sei que posso usar o switch que seria quase a mesma coisa.
O código abaixo tem o método com apenas 2 dos 16 botões para simplificar, já que os outros botões seguem a mesma lógica.
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId() == R.id.btneditar1) {
        camponome.setSelectAllOnFocus(true);
        camponome.setFocusable(true);
        camponome.setFocusableInTouchMode(true);
        camponome.requestFocus();
        imm.showSoftInput(camponome, InputMethodManager.SHOW_FORCED);
        btneditar1.setVisibility(View.GONE);
        btnok1.setVisibility(View.VISIBLE);
    }

    if(v.getId() == R.id.btnok1) {
        CampoValido = ValidaCampo.validateNotNull(camponome, "Campos sem dados são inválidos!");
        if(CampoValido) {
            CampoValido = ValidaCampo.validateNotEqual(camponome, bnome);
            if(CampoValido) {
                final String nome = camponome.getText().toString().trim();
                atualizar.execute(usuario, nome, null, null, null, null, null, null);
                txtnomebd.setText(nome);
            }
            camponome.setFocusable(false);
            camponome.setFocusableInTouchMode(false);
            camponome.requestFocus();
            btnok1.setVisibility(View.GONE);
            btneditar1.setVisibility(View.VISIBLE);
        }
    }

    if(v.getId() == R.id.btneditar2) {
        camposobrenome.setSelectAllOnFocus(true);
        camposobrenome.setFocusable(true);
        camposobrenome.setFocusableInTouchMode(true);
        camposobrenome.requestFocus();
        imm.showSoftInput(camposobrenome, InputMethodManager.SHOW_FORCED);
        btneditar2.setVisibility(View.GONE);
        btnok2.setVisibility(View.VISIBLE);
    }

    if(v.getId() == R.id.btnok2) {
        CampoValido = ValidaCampo.validateNotNull(camposobrenome, "Campos sem dados são inválidos!");
        if(CampoValido) {
            CampoValido = ValidaCampo.validateNotEqual(camposobrenome, bsobrenome);
            if(CampoValido) {
                final String sobrenome = camposobrenome.getText().toString().trim();
                atualizar.execute(usuario, null, sobrenome, null, null, null, null, null);
                txtsobrenomebd.setText(sobrenome);
            }
            camposobrenome.setFocusable(false);
            camposobrenome.setFocusableInTouchMode(false);
            camposobrenome.requestFocus();
            btnok2.setVisibility(View.GONE);
            btneditar2.setVisibility(View.VISIBLE);
        }
    }

}

Comment: @GustavoAlmeidaCavalcante: Você está sobrepondo o método onClick da Activity? O que mais costumo ver é algo do tipo `findViewById(id).setOnClickListener(new View.OnClickListener() { ... seu código ...} );` , que eu considero mais legível. De qualquer forma, seria um grande avanço separar esses corpos dos ifs em suas devidas funções privadas adequadamente nomeadas. Quanto ao desempenho, a não ser que você tenha notado alguma lentidão nessa parte do código, não se preocupe com isso precipitadamente.
Comment: @GustavoAlmeidaCavalcante considere utilizar switch ou utilize if....if else. Do jeito que está toda vez que o usuário clicar em um botão os 16 ifs serão consumidos.
Comment: @GustavoAlmeidaCavalcante Eu prefiro para a depuração, mas esse ponto é mesmo questão de gosto. Mas os outros dois conselhos eu sugiro que considere.
Comment: Verificar esses 16 ifs é praticamente nada para os processadores atuais. Foca no que o @Pablo falou.
Comment: Tente usar [switch...case](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) para todos os ifs do código. Agora tem muito conteúdo ai que está repetido em várias funções, otimizar isso seria criar uma nova lógica pra você, e nem conhecemos seu programa.
Comment: @Diego Felipe Como disse na pergunta o switch é quase a mesma coisa não vai otimizar nada.
Comment: O que você quer dizer com "otimizar"? O grande problema do código não são nem os ifs, e sim o número de funções repetidas dentro de algumas condições. E como já disse, otimizar isso seria quase o mesmo que recriar sua lógica. Parece que dá pra usar ArrayList de objetos também.
Comment: @Diego Felipe Sim eu tive que repetir as funções, pois cada uma além de afetar um campo diferente, ela afeta um botão diferente. Mas é uma boa ideia fazer um único método onde passo como parâmetro o campo e o botão.
Comment: Tenho uma idéia de como resolver isso, mas não sei se é otimizada, vou postar aqui nos comentários e você da uma olhada
Comment: @Diego Felipe ok.
Comment: @Pablo Se eu fosse colocar o Listener em cada botão dessa maneira, iria aumentar o tamanho do código significativamente. Eu não sei se ficaria mais legível, mas pelo fato de aumentar muito o tamanho do código eu prefiro centralizar o Listener de todos os botões em um método.
Comment: @Arubu Você esta certo nesse ponto, do jeito que esta ele consome os 16 ifs. Se eu for utilizar mesmo os ifs farei isso.
Comment: @Pablo Ao pensar na sua ideia de colocar o Listener em cada botão com seu método da forma que você disse, pode até ser que aumente o tamanho do código, mas acho que dessa forma ele vai consumir menos recursos de processamento já que ele não vai verificar qual botão foi apertado e sim vai executar diretamente o que está no método do botão. No entanto meu onCreate() vai ficar maior e quando a activity for iniciada ele vai consumir mais recursos nesse momento. Bom é só o que eu acho, não tenho certeza.
Comment: @GustavoAlmeidaCavalcante Sim, provavelmente isso acontece. Mas a diferença provavelmente é tão pequena que, para todos os efeitos e propósitos, é a mesma coisa. Sugiro que você pare de se preocupar com desempenho dessa forma a não ser que você realmente meça alguma diferença. Existem ferramentas chamdas _profilers_ que servem para encontrar onde estão os "gargalos" da sua aplicação. Use-os nessas situações e, enquanto não for necessário, preocupe-se mais com a legibilidade que, assim, você ganha mais tempo no médio e longo prazos.
Answer: Ao refletir sobre os comentários e fazer algumas pesquisas. Para esse caso em específico, a única forma de otimizar os ifs seria colocar um Listener em cada botão no método onCreate() ficando assim:
findViewById(id).setOnClickListener(new View.OnClickListener() { ... seu código ...} );

Porém ao fazer isso eu sobrecarrego método onCreate(), ou seja, ao otimizar uma coisa sobrecarrego outra. Não sei se isso seria melhor, mas com a ajuda dos comentários cheguei a conclusão de que os ifs nesse caso não eram o problema e sim o fato de repetir funções. Eu estava preocupado com ifs e esqueci de usar a orientação a objeto. Então a única otimização feita foi fazer um único método para a ação de todos os botões que usam esse método, passando como parâmetro o que diferencia na ação de cada botão ficou assim:
public void onClick(View v) {
    // TODO Auto-generated method stub
    ControlaBanco bd = new ControlaBanco(getBaseContext());
    Cursor dados = bd.pegaUsuario();
    bnome = dados.getString(2);
    bsobrenome = dados.getString(3);
    busuario= dados.getString(4);
    bpaisregiao = dados.getString(6);
    bdatanasc = dados.getString(7);
    bsexo = dados.getString(8);
    bemail = dados.getString(9);

    boolean CampoValido = false;
    TarefaAtualizar atualizar = new TarefaAtualizar(this);
    final String usuario = busuario;

    if(v.getId() == R.id.btneditar1) {
        AcaoEditarCampo(camponome, btneditar2, btnok2);
    }

    if(v.getId() == R.id.btnok1) {
        CampoValido = AcaoOkCampo(camponome, bnome, btnok2, btneditar2);
        if(CampoValido) {
            final String nome = camponome.getText().toString().trim();
            atualizar.execute(usuario, nome, null, null, null, null, null, null);
            txtnomebd.setText(nome);
        }
    }

    if(v.getId() == R.id.btneditar2) {
        AcaoEditarCampo(camposobrenome, btneditar3, btnok3);
    }

    if(v.getId() == R.id.btnok2) {
        CampoValido = AcaoOkCampo(camposobrenome, bsobrenome, btnok3, btneditar3);
        if(CampoValido) {
            final String sobrenome = camposobrenome.getText().toString().trim();
            atualizar.execute(usuario, null, sobrenome, null, null, null, null, null);
            txtsobrenomebd.setText(sobrenome);
        }
    }

}

private void AcaoEditarCampo(EditText Campo, Button BotaoEditar, Button BotaoOk) {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    Campo.setSelectAllOnFocus(true);
    Campo.setFocusable(true);
    Campo.setFocusableInTouchMode(true);
    Campo.requestFocus();
    imm.showSoftInput(Campo, InputMethodManager.SHOW_FORCED);
    BotaoEditar.setVisibility(View.GONE);
    BotaoOk.setVisibility(View.VISIBLE);
}

private boolean AcaoOkCampo(EditText Campo, String bcampo, Button BotaoOk, Button BotaoEditar) {
    boolean CampoValido = ValidaCampo.validateNotNull(Campo, "Campos sem dados são inválidos!");
    if(CampoValido) {
        if(Campo.getInputType() == (InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS + 1)) {
            CampoValido = ValidaCampo.validateEmail(Campo);
            if(!CampoValido) {
                return CampoValido;
            }
        }
        if(CampoValido) {
            CampoValido = ValidaCampo.validateNotEqual(Campo, bcampo);
        }
        Campo.setFocusable(false);
        Campo.setFocusableInTouchMode(false);
        Campo.requestFocus();
        BotaoOk.setVisibility(View.GONE);
        BotaoEditar.setVisibility(View.VISIBLE);
    }

    return CampoValido;
}

Obrigado a todos que comentaram e ajudaram na minha resposta, vou deixar sem marcar essa resposta como certa por 2 dias para o caso de alguém ter uma resposta melhor.


Covariance matrix with diagonal elements only

Question: Let's imagine an arbitrary data set in $ D=3 $, is it possible that the covariance matrix consists of diagonal elements only?
I'd say such a data set can't exist. Or would all data points be incident with three axes?
Comment: It could exist a diagonal covariance matrix if variable are uncorrelated.
Comment: Take the eight points $(0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0), (1,1,1)$ as your and find the covariance matrix
Comment: Try finding the covariances with my suggested eight points (remember to subtract the means).  All three will be $0$.  Then try to understand why
Comment: @Henry The data points (0,1,1), (1,0,1), (1,1,0) and (1,1,1) would result in a S with non-diagonal elements, or not?
Comment: @AbdoulHaki Sure, I just wonder about the possibility of such a data set. Therefore the question: Only when all the data points would be exactly positioned on the axes, or not?
Answer: For instance, if we try to estimate linear regression model, we then check an assumption of an absence of autocorrelation (particular, in time series). We use, at first, covariance matrix between residuals with zero non diagonal elements. Then we use Newey West matrix with consistent estimates and compare this matrix to the previous (also, you could use tests to detect).
So, according to the theory, yes, this matrix exists, but in practice, we always get nonzero values (maybe, small, e.g. 1e-5) on non diagonal elements.


What is an elegant way of visualising two time series with many data points?

Question: I need to draw two time-series plots on a graph in R. The problem I am facing is as follows. Each plot has 200 data points, and therefore, the graph that I have produced looks clumsy on my two-column pdf file. I am trying to use different pch values, but still the graph doesn't look good to me. Could someone help me to visualize this data in an elegant way, please?  Please see the graph below. The most important requirement is that the graph should be clear on printing.
The R Code that I have used is as follows:
plot(temp11, type="b", col="blue", ylim=c(0.5,1), lwd=0.8, lty=4, pch=1, xlab="Time", ylab="price", cex.lab=1.2,cex.axis=1.2)
> lines(temp22, type="b", col="red", lwd=1.5,lty=1, pch=18)
> legend("top",legend=c("temp11","temp22"),lty=c(4,1),pch=c(1,18), col=c("blue", "red"),text.col=c("blue", "red"))

Comment: maybe losing the plotting characters altogether and increasing the line width (argument: lwd. takes positive real values) would made it reasonable. How does this example  x=ts(rnorm(100)); y=ts(rnorm(100)); plot(x, col=2, ylim=c(-4,4), lwd=4); lines(y, col=4, lwd=4) look? It would need to be suped-up a bit but I think that basic template makes both time series visible.
Comment: Have you thought about two *rows* instead of two *columns*?  Even if you decide to keep them both on the same axes, if you'd make your height smaller than your width then it'd stretch the series out to address that bunching up shown above.
Comment: As @macro suggested, maybe lose the points altogether, or maybe just make them really small. You could use cex = .3, or pch = '.'. If you make them small,I would suggest filled circles instead of open circles.
Given the overlap on the right side of the graph, though, it might be good to lose the lines and keep the dots, perhaps with a small amount of jitter to avoid overlap
Comment: @Marco: Thanks. On a computer screen your idea is perfectly fine. But the graph should be elagant on printing. It would be great if you share your ideas further on this.
Comment: @Jay: I am not getting your idea. Could you please give more information? Thank you.
Answer: Partial transparency ("alpha") might help you here, e.g.:
> temp11=runif(100)
> temp22=runif(100)
> plot(temp11, type="p", col=rgb(0.2, 0.2, 1, 0.6), pch=19, xlab="Time", ylab="price")
> lines(temp11, lwd=3, col=rgb(0.2, 0.2, 1, 0.3))
> points(temp22, pch=19, col=rgb(1, 0.2, 0.2, 0.6))
> lines(temp22, lwd=3, col=rgb(1, 0.2, 0.2, 0.3))

Here is an example of the image:

It doesn't look that great with runif data, but on your data I think it would work a bit better.  Alpha support is device-dependent, but PDF does support it.
Comment: Welcome to the site. With your couple of upvotes from this post you should have enough reputation to post as many images and links in questions/answers as you would like.
Answer: Here's what I was trying to say in the comment, with a little bit more detail (though this might not be the answer to your question).  First, get some data.
x <- arima.sim(200, model = list(ar = 0.6)) + 3
y <- arima.sim(200, model = list(ar = -0.7)) - 3

A plot with two columns would look something like this:
par(mfrow = c(1,2))
plot(x)
plot(y)

with a graph like so:

while a plot with two rows would look something like this:
par(mfrow = c(2,1))
plot(x)
plot(y)

with a graph like so:

And if you'd like to keep both series on the same axes then you could do like this:
par(mfrow = c(1,1))
ts.plot(x, y)

which would look something like this:

I was trying to communicate that if you make the plot wider rather than taller then it helps to see what's happening inside the lengthy time series better.  You can manually resize the graphics device to get the aspect you like, or if you use RStudio, when you export the image you can set whatever dimensions desired.


Zookeeper security concern

Question: Below is a well know problem about zookeeper safeguard. I might have limited knowledge or Zookeeper or may be I am trap in classical example of xy problem. So I am open to all kind of suggestion please do mention any thought.
If I want to run Zoookeeper quorum at 3 server having static IP. I have done all the configuration and
<code>My quorum are distributed over 3 server which static ip , lets say

server.1=xx.xx.x1:2888:3888
server.2=xx.xx.x2:2889:3889
server.3=xx.xx.x3:2890:3890

My concern is how should I protect it to other unauthorized zkClient to connect above quorum.
</code>
One way that I can think of, don't open the port for the client but then how will application
( I am using this for solr) solr connect ?
other problem is how to safeguard quorum interconnection. I observed a weird behavior that I can point
a fourth zookeeper from my local to the above quorum (i have to know only ip and port which is not tough
to find) and it will be absorbed as a part of quorum and then I can use my local zkClient to connect my local
zoookeeper and have access to quorum which we don't want.
I want to define quorum in a way that foreign zookeeper server is not able to become part of it.
Comment: Is it possible for you to keep zookeeper on a private network not visible to the outside world?
Comment: Sir, that is my problem. I have static ip only may be I am not understanding clearly how to create private network.
Answer: Check out the zookeeper programmer guide:
Particularly the bit about ACLs:
http://zookeeper.apache.org/doc/r3.2.2/zookeeperProgrammers.html#sc_ZooKeeperAccessControl

ZooKeeeper has the following built in schemes:
world has a single id, anyone, that represents anyone. auth doesn't
  use any id, represents any authenticated user. digest uses a
  username:password string to generate MD5 hash which is then used as an
  ACL ID identity. Authentication is done by sending the
  username:password in clear text. When used in the ACL the expression
  will be the username:base64 encoded SHA1 password digest. ip uses the
  client host IP as an ACL ID identity. The ACL expression is of the
  form addr/bits where the most significant bits of addr are matched
  against the most significant bits of the client host IP.

Haven't used, but here's a guide to using:
https://ihong5.wordpress.com/2014/07/24/apache-zookeeper-setting-acl-in-zookeeper-client/
I'll try it out tomorrow!
Here's a guide to using this with Curator:
http://michaelmorello.blogspot.de/2012/12/zookeeper-netflix-curator-and-acls.html

Zookeeper security concern


Como realizar update em registros marcados com checkbox em um while

Question: Não estou conseguindo efetuar o UPDATE dos campos que estão apenas selecionados pelo CHECKBOX, o que eu consegui desenvolver esta logo abaixo:
BD.SQL
CREATE DATABASE IF NOT EXISTS `seq_acessos` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `seq_acessos`;

CREATE TABLE `teste2` (
  `id` int(11) NOT NULL,
  `teste1` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `teste2` varchar(20) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `teste2` (`id`, `teste1`, `teste2`) VALUES
(5, '02', '22'),
(6, '03', '33'),
(7, '04', '44');

ALTER TABLE `teste2`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `teste2`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;

INDEX.PHP
error_reporting ( E_ALL );
ini_set( 'display_errors', TRUE );

    $host = "localhost";
    $usuario = "root";
    $senha = "";
    $bd = "teste";

    $mysqli = new mysqli($host, $usuario, $senha, $bd);

    if($mysqli->connect_errno){
        echo "Falha na Conexão: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
    }

$sql_code = "SELECT * FROM teste2";
$sql_query = $mysqli->query($sql_code) or die($mysqli->error);
?>

<form action="" method="post">
<?php while($linha = $sql_query->fetch_assoc()){ ?>
    <input type="checkbox" name="teste[cb][]" value="<?php echo $linha['id']; ?>">
    <input type="text" name="teste[teste1][]" value="<?php echo $linha['teste1']; ?>">
    <input type="text" name="teste[teste2][]" value="<?php echo $linha['teste2']; ?>">
<?php } ?>

    <input type="submit" value="OK">
</form>
<?php

if(isset($_POST)){
    $field      = $_POST;
    $count      = count($field[teste]);
    for($i = 0; $i < $count; $i++){

$up .= "UPDATE table SET teste1 = {$field['teste']['teste1'][$i]}, teste2 = {$field['teste']['teste2'][$i]} WHERE id = $linha[id] \n";

    }

    echo "
";
    print_r($up);
    print_r($_POST);

}

Answer:  <?php
    $linha = [];

    $linha[1] = array("id" => 1, 'field' => "valor", 'field2' => "valor2");
    $linha[2] = array("id" => 2, 'field' => "valor", 'field2' => "valor2");
    $linha[3] = array("id" => 3, 'field' => "valor", 'field2' => "valor2");
    $linha[4] = array("id" => 4, 'field' => "valor", 'field2' => "valor2");
    $linha[5] = array("id" => 5, 'field' => "valor", 'field2' => "valor2");

    echo '<form action="" method="post">';
    echo '
';
    for ($i=1; $i <= count($linha); $i++) {

        echo '
<input type="checkbox" name="teste[ex'.$i.'][id]"      value="'.$linha[$i]["id"].'">

<input type="text"  name="teste[ex'.$i.'][field]" value="'.$linha[$i]["field"].'">

<input type="text"  name="teste[ex'.$i.'][field2]" value="'.$linha[$i]["field2"].'">
';
    }
    echo '

       <input type="submit" value="OK">
    </form>';

    if (isset($_POST['teste'])) {
       $data = $_POST['teste'];
       if ($data != null) {
            foreach ($data as $key => $value) {
                if (isset($value['id']) && !empty($value['id'])) {
                    $query = "UPDATE table SET field =".$value['field'].",field2 =".$value['field2']." WHERE id= ".$value['id'];
                    $query = rtrim($query, ", ");
                    echo "
$query";
                }
            }
        }
    }
    ?>

Comment: Lucas, obrigado pela atenção, mas não consegui testar sua idéia, mesmo acrescentando o fechamento do FOR, a $query não esta sendo exibida.
Comment: @CristianoIglesias, tente novamente refiz os ajustes.
Comment: Só para constar que ficou exatamente como eu queria, obrigado!
Answer: Pelo que entendi você quer pegar os valores que estão nos inputs do tipo text de acordo com o checkbox marcado. Para isso você tem que ver se o checkbox correspondente foi marcado e pegar o valor dele que será usado como referência para pegar os dos text. Segue um exemplo:
<?php
  if(isset($_POST["submit"])){

    $checkbox = $_POST["teste"]['cb'];

    if(is_array($checkbox) && !empty($checkbox)){

        $up = '';

        foreach($checkbox as $key=>$value){

            $teste1 = $_POST['teste']["teste1"][$key];
            $teste2 = $_POST['teste']["teste2"][$key];

            $up .= "UPDATE table SET teste1 = '".$teste1."', teste2 = '".$teste2."' WHERE id = '".$value."'
";
        }

        echo $up;
    }
  }
?>

<?php
error_reporting ( E_ALL );
ini_set( 'display_errors', TRUE );

    $host = "localhost";
    $usuario = "root";
    $senha = "";
    $bd = "teste";

    $mysqli = new mysqli($host, $usuario, $senha, $bd);

    if($mysqli->connect_errno){
        echo "Falha na Conexão: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
    }

$sql_code = "SELECT * FROM teste2";
$sql_query = $mysqli->query($sql_code) or die($mysqli->error);
?>
<form action="" method="post">

    <?php while($linha = $sql_query->fetch_assoc()){ ?>
    <input type="checkbox" name="teste[cb][]" value="<?php echo $linha['id'];?>">
    <input type="text" name="teste[teste1][]" value="<?php echo $linha['teste1'];?>">
    <input type="text" name="teste[teste2][]" value="<?php echo $linha['teste2'];?>">

    <?php } ?>

    <input type="submit" name="submit">
</form>

Comment: Sim, preciso alterar somente as linhas que estiverem marcadas com o checkbox, no exemplo acima o BD possui 3 registros:
Efetuei um teste com a alteração que você fez e agora esta gerando o UPDATE somente dos checkbox marcados, perfeito, mas não esta identificando os valores corretos dos imputs.
Efetuei um teste alterando o 1 e o 3 registro e o resultado foi este:
UPDATE table SET teste1 = '02', teste2 = '22' WHERE id = '2'
UPDATE table SET teste1 = '03', teste2 = '33' WHERE id = '4'
Comment: Vou tentar detalhar um pouco mais:    Se eu marcar os três checkbox é criado corretamente os UPDATES:  UPDATE table SET teste1 = '02', teste2 = '22' WHERE id = '2'  UPDATE table SET teste1 = '03', teste2 = '33' WHERE id = '3'  UPDATE table SET teste1 = '04', teste2 = '44' WHERE id = '4'    Mas como mensionei anteriormente, se eu efetuar somente as alterações do 1 e o 3 campos, o resultado é este:  UPDATE table SET teste1 = '02', teste2 = '22' WHERE id = '2'  UPDATE table SET teste1 = '03', teste2 = '33' WHERE id = '4'    O UPDATE é criado com os valores incorretos.


Confusion about the proof of fence theorem for strong fences

Question: In the book of Differential equations: A dynamical system approach by Hubbard and West, at page 27, it is given that:

A strong fence for the diff. equations $x' = f(t,x)$ is nonporous.

And in the first line of the proof, it says that

The hypothesis $\alpha(t_0) \leq u(t_0)$ means that ...

,where $\alpha(t)$ is a lower fence for the given diff. equation.
However, a fence is related with the slope of the function x, i.e $x'$, not with the actual value of x. In other words, $\alpha'(t) \leq x'(t)$ for all t in I, where I is some interval, so we can not conclude that $\alpha(t_0) \leq u(t_0)$.
So, what am I missing ?
Edit:
A link to the book is added, so for more detail, you can check it out.
Answer: The problem was due to the misunderstanding of the definition of nonporous fence.

The definition says that If there exist a $t_0 \in I$ such that
  $\alpha (t_0) \leq u(t_0)$ for a given solution of the diff. equation,
  then $\alpha (t) \leq u(t)$ for all $t > t_0$ in I.

Therefore, let first assume that $\alpha(t)$ is a strong fence for the ODE, and let $u(t)$ be a solution to the ODE.If $t_0$ exists, we have either;
$\alpha(t_0)  = u(t_0)$, or
$\alpha (t_0) < u(t_0)$. (If t_0 is not exists, we have nothing to prove.)
In the former case, we have
$$\alpha(t_0) < f(t_0, \alpha(t_0) = f(t_0, u(t_0)) = u(t_0),$$
right after $t_0$, $\alpha(t) < u(t)$, and since by definition
$\alpha'(t) < u'(t)$ for all t in I, we have
$$\alpha(t) < u(t)\quad \forall t > t_0.$$
In the latter case, as similar argument follows.


Can compact generators detect zero morphisms?

Question: Let $T$ be a triangulated category admitting arbitrary coproducts with a set of compact generators $\mathcal{G}$. From the definition of compact generators we have that for any object $X$ of $T$ $[\Sigma^nZ,X]=0$ for all $n \in \mathbb{Z}$ and all $Z \in \mathcal{G}$ then $X=0$, equivalently $\mathcal{G}$ detects isomorphisms i.e. if a map $f \colon X \rightarrow Y$ has the property that $[\Sigma^nZ,f]$ is an isomorphism $\forall n \in \mathbb{Z}$ and $\forall  \in \mathcal{G}$ then $f$ is an isomorphism.
But what if we have $[\Sigma^nZ,f]=0$? Can we deduce that $f$ is the zero map? I never found this claim anywhere and I could not prove it. I tried to show that the full subcategory of $T$ generated by $\{ A \in T : [A,f]=0 \}$ is a localizing subcategory but I have difficulties proving that is triangulated. Basically I get in a situation similar to the 5 lemma but where the two vertical maps on the left and on the right are zero and I cannot conclude the central one is zero from the exactness of the long horizontal sequences.
Thus I am induced to think that the claim is false: can you provide an explicit counterexample?
Also what if I add more structures or properties to $T$? For example I could consider a closed tensor triangulated category such that the elements of $\mathcal{G}$ are strongly dualizable or such that every cohomology functor is representable.
Answer: If you allow yourself to choose the set $\mathcal{G}$ of compact generators then there are very simple examples. For example, take $T=D(\text{Mod }R)$ for some ring $R$, and $\mathcal{G}=\{\Sigma^nR\mid n\in\mathbb{Z}\}$, and let $f:M\to\Sigma N$ be a map representing a nonzero element of $\text{Ext}^1_R(M,N)$.
But even if you take $\mathcal{G}$ to be the class of all compact objects, then there are examples even in quite normal triangulated categories, that have been quite extensively studied under the name "phantom maps".
Comment: I guessed that the argument was related to phantom maps. I read about it only the lecture notes by Lurie on chromatic homotopy theory. Are there recommended references for this topic? I found the paper "Phantom maps and homology theories" by Christensen and Strickland, also "Axiomatic stable homotopy theory" has some sections dedicated to to phantom maps. Is there any other text you would recommend?
Comment: @N.B. There are some papers by Gnacadja, who did a PhD on phantom maps in stable module categories of finite group algebras, that are a lot less general than those you mention, but might be useful if you want to see concrete examples in a relatively simple setting, and have any kind of background in representation theory.
Comment: Thanks for the answer. Can I ask you if the following intuition is correct? In the paper I cited and also in your example we see a correlation between phantom maps and Ext terms. Unravelling your example: an element $f \in Ext^1(M,N)$ stands for a Yoneda class of extensions $0 \rightarrow N \rightarrow F \rightarrow M \rightarrow 0$ which in the derived category $D(R)$ "corresponds" to an exact triangle $\Sigma^-1 M \rightarrow N \rightarrow F \rightarrow M$ where the first map $\Sigma^-1 M \rightarrow N$ is phantom. I said "corresponds" because the phantom map is not uniquely determined.
Comment: But the point is that the phantom map stands for the impossibility of the triangulated structure of $D(R)$ to pass to an abelian structure where we encode information in short exact sequences. I do not know if there is a precise formalisation of this fact: maybe there is an encolement of triangulated categories where $D(R)$ is the central term and on the right we have some sort of category with morphisms the phantom maps?
Comment: @N.B. I'm rather busy right now, but I'll think about what you've asked when I get some time. By "encolement" do you mean "recollement"?
Comment: Yes, recollement. I did not work intensively with this concept so I am not very familiar with it but it should be a sequence of triangulated categories and exact functors which describes the central category as extension (in some sense) of the two terms at left and right. So given a nice stable homotopy category $S$ is it reasonable to extect to have a recollement $P \rightarrow S \rightarrow H$? Where $P$ is a category with same objects as $S$ but the morphisms are the phantom maps and $H$ is a category of homology theories?


Converting time format to numeric with R

Question: In most cases, we convert numeric time to POSIXct format using R. However, if we want to compare two time points, then we would prefer the numeric time format. For example, I have a date format like "2001-03-13 10:31:00",
  begin <- "2001-03-13 10:31:00"

Using R, I want to covert this into a numeric (e.g., the Julian time), perhaps something like the passing seconds between 1970-01-01 00:00:00 and 2001-03-13 10:31:00.
Do you have any suggestions?

The Julian calendar began in 45 BC (709 AUC) as a reform of the Roman calendar by Julius Caesar. It was chosen after consultation with the astronomer Sosigenes of Alexandria and was probably designed to approximate the tropical year (known at least since Hipparchus). see http://en.wikipedia.org/wiki/Julian_calendar
Comment: Generally speaking, `as.numeric` is a good first place to turn if you want to convert something to, well, a number. Have you tried that?
Comment: I didn't downvote but I suspect it was done because of careless mismatch between your input and output. It seems possible you hoped to just remove the punctuation in a character type vector and you wanted: 20010313103100 possibly as a numeric value but at the moemnt it's very unclear.
Comment: Your reply is helpful. I will revise the question.
Comment: good reminding. thank u.
Answer: If you just want to remove ":" , " ", and "-" from a character vector then this will suffice:
end <- gsub("[: -]", "" , begin, perl=TRUE)
#> end
#[1] "20010313103100"

You should read the section about 1/4 of the way down in ?regex about character classes. Since the "-" is special in that context as a range operator, it needs to be placed first or last.
After your edit then the answer is clearly what @joran wrote, except that you would need first to convert to a DateTime class:
 as.numeric(as.POSIXct(begin))
#[1] 984497460

The other point to make is that comparison operators do work for Date and DateTime classed variables, so the conversion may not be necessary at all. This compares 'begin' to a time one second later and correctly reports that begin is earlier:
as.POSIXct(begin) < as.POSIXct(begin) +1
 #[1] TRUE

Comment: or more generally `gsub("[^[:digit:]]", "", begin)` will remove anything other than number.
Answer: Based on the revised question this should do what you want:
begin <- "2001-03-13 10:31:00"
as.numeric(as.POSIXct(begin))

The result is a unix timestamp, the number of seconds since epoch, assuming the timestamp is in the local time zone.
Answer: Maybe this could also work:
library(lubridate)
...
df <- '24:00:00'

as.numeric(hms(df))

hms() will convert your data from one time format into another, this will let you convert it into seconds. See full documentation.
I tried this because i had trouble with data which was in that format but over 24 hours.
Answer: The example from ?as.POSIX help gives
as.POSIXct(strptime(begin, "%Y-%m-%d %H:%M:%S"))

so for you it would be
as.numeric(as.POSIXct(strptime(begin, "%Y-%m-%d %H:%M:%S")))


Linear Model or Logistic Model: Can Someone Recommend a Book?

Question: I have a huge data set that looks roughly like this:
x = [x1, x2, x3, ..., x800]
x is how much percent the real project cost is different from the planned project cost

y1 = [y1.1, y1.2, y1.3, ..., y1.800]
y1 is the estimated project cost in dollar (or another currency)

y2 = [y2.1, y2.2, y2.3, ..., y2.800]
y2 is the real projet duration

y3 = [y3.1, y3.2, y3.3, ..., y3.800]
y3 is the project manager (in coded form like "mk" and "op")

....

y50 = [y50.1, y50.2, y50.3, ..., y50.800]
y50 is the estimated project duration

Now the aim is to predict the x (cost difference in percent) in dependency of other y variables. It is a priori not clear which variables influence it. And some of the y are not interdependent like for example the difference in percent x, the real costs and the estimated costs since x is calculated from the real and estimated costs.
I am now wondering how is the best practice way to analyse the data or how to build a model (linear or maybe logistic). The binary output for the logistic model could be difference bigger than 10 %.
Since I have a lot of data sets (over 800) almost everything could get significant on a e. g. 5 % level. And It's not easy to handle/control the power if you do not have a simple t-test.
What would you recommend? Is there a good book for "normal people" about that kind of statistics?
Comment: Unless you're looking for a run-of-the-mill Statistics textbook, it will depend a lot on the software that you plan on using. You might really like [this article](http://www.aeaweb.org/articles.php?doi=10.1257/jep.28.2.3)--it's geared towards a general audience (though still substantial). In addition, you can practice with the code/data the author provided on his website (and described throughout).
Comment: Also, you probably don't want to use the logistic regression here since the restriction that results be between zero and 1 is most likely an unreasonable assumption on your part. As an aside, your data will be more readable if you follow the convention of using **x** to signify your predictors and **y** to be the response.
Comment: But again, that article will cover some of the big issues that you'll have to consider (such as incorporating **cross-validation** into your analysis as well as **regularization**), giving you a better idea of where to go from there.
Comment: Thanks for the comments. And yes - I should switch x and y ;)
Answer: I highly recommend these two books:

Linear Models with R, by Julian J. Faraway
Extending the Linear Model with R, by Julian J. Faraway

Both of these books discuss how to do applied statistics in R (a free statistical software that can be downloaded). Linear Models with R discusses modelling continuous variables where Extending the Linear Models with R will discuss modeling other types of variables (count, binary).
Comment: I will have a look at this books. I also know R by the way. Thanks!


How to get href from tag  (html) by BS4?

Question: Hie there!
I can't get href from tag  in BS4. That is my code:
import requests
from bs4 import BeautifulSoup

URL = 'https://auto.ria.com/newauto/marka-jeep/'
HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)                        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', 'accept':'*/*'}

def get_html(url, params=None):
    r = requests.get(url, headers=HEADERS, params=params)
    return r

def get_content(html):
    soup = BeautifulSoup(html, 'html.parser')
    items = soup.find_all(class_='proposition_area')
    cars=[]
    for item in items:
        cars.append({
            'title': item.find('h3', class_='proposition_name').get_text(strip=True),
            'link': item.find('a', class_='proposition_link').getAttribute("href")
        })
    print(cars)

def parse():
    html = get_html(URL)
    if html.status_code == 200:
        get_content(html.text)
    else:
        print('error')
parse()

output:
'link': item.find('a', class_='proposition_link').getAttribute("href")

Error:
AttributeError: 'NoneType' object has no attribute 'getAttribute'

Comment: `item.find('a', class_='proposition_link')` is returning `None`
Comment: That means the element you're looking for isn't in the HTML.
Comment: yes, and I can't fix it
Comment: Correct, if it's _not there_, you can't fix it.
Comment: Are you looking at the HTML in your browser, or are you looking at it in `curl` or your Python interpreter? One very common problem we see here is people trusting their browsers to be showing exactly what the remote server returns.
Comment: ...however, very often there's Javascript code or DOM fixups changing what the browser shows, so it's not an accurate representation of what your software needs to be written to look for.
Comment: ? I don't understand. I just want to get href from tag . Is that html?
Comment: The tag is HTML, but the question is if the HTML is there in what the server gives you, or if JavaScript code being run in your web browser _creates_ the `` tag.
Comment: If it's JavaScript running in the browser that creates the tag, then the tag doesn't exist without a web browser in use, so bs4 (and anything else that isn't a web browser with javascript support) can't see it.
Comment: Anyhow, it's easy to test that. _Print your HTML in the Python code_ and inspect it, and see if it really contains the elements you're trying to find. (In this case, `print(html.text)` will do the trick).
Comment: thank you! And how I need to get the link from it?
Comment: ...so, having done that test above, it looks like the HTML that the server returns _does_ include the relevant item, so you aren't facing the specific issue I mentioned.
Comment: ...rather, the issue is simply that none of the `proposition_area`s in that page contain a `proposition_link`. They _do_ have a span with `class=link`; maybe that's what you wanted?
Comment: Anyhow, if they have a `proposition_link` in the web browser, it's JavaScript doing that. You don't have JavaScript without a web browser; you can't rely on anything it does.
Answer: You have two problems:

You are doing items = soup.find_all(class_='proposition_area') and later looping over that class to search for the class proposition_area with:
for item in items:
     cars.append({
         'title': item.find('h3', class_='proposition_name').get_text(strip=True),
         'link': item.find('a', class_='proposition_link').get("href")
     })

the item.find('a', class_='proposition_link') is not within the class proposition_area which you are looping for, so, instead do:
items = soup.find_all(class_='proposition')

Don't use the .getAttribute() method, instead, use .get().

Here's a fully working example:
import requests
from bs4 import BeautifulSoup

URL = 'https://auto.ria.com/newauto/marka-jeep/'
HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', 'accept':'*/*'}

def get_html(url, params=None):
    r = requests.get(url, headers=HEADERS, params=params)
    return r

def get_content(html):
    soup = BeautifulSoup(html, 'html.parser')
    items = soup.find_all(class_='proposition')
    cars=[]
    for item in items:
        cars.append({
            'title': item.find('h3', class_='proposition_name').get_text(strip=True),
            'link': item.find('a', class_='proposition_link').get("href")
        })
    print(cars)

def parse():
    html = get_html(URL)
    if html.status_code == 200:
        get_content(html.text)
    else:
        print('error')
parse()

Output:
[{'title': 'Jeep Gladiator 2021', 'link': '/newauto/auto-jeep-gladiator-1862595.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1859603.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1863650.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1842428.html'}, {'title': 'Jeep Renegade 2021', 'link': '/newauto/auto-jeep-renegade-1838198.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1853604.html'}, {'title': 'Jeep Wrangler 2021', 'link': '/newauto/auto-jeep-wrangler-1838190.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1811781.html'}, {'title': 'Jeep Wrangler 2021', 'link': '/newauto/auto-jeep-wrangler-1857232.html'}, {'title': 'Jeep Wrangler 2021', 'link': '/newauto/auto-jeep-wrangler-1860925.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1836192.html'}, {'title': 'Jeep Renegade 2021', 'link': '/newauto/auto-jeep-renegade-1857781.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1838297.html'}, {'title': 'Jeep Wrangler 2021', 'link': '/newauto/auto-jeep-wrangler-1860927.html'}, {'title': 'Jeep Wrangler 2021', 'link': '/newauto/auto-jeep-wrangler-1860588.html'}, {'title': 'Jeep Gladiator 2021', 'link': '/newauto/auto-jeep-gladiator-1856629.html'}, {'title': 'Jeep Renegade 2021', 'link': '/newauto/auto-jeep-renegade-1857246.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1857805.html'}, {'title': 'Jeep Grand Cherokee 2021', 'link': '/newauto/auto-jeep-grand-cherokee-1829808.html'}, {'title': 'Jeep Wrangler 2021', 'link': '/newauto/auto-jeep-wrangler-1862123.html'}]

How to get href from tag  (html) by BS4?


Form validation doesn't working after ajax call

Question: Form Validation not working correctly after form is loaded with an ajax call. How can i fix this?

$(document).ready(function() {
  // $('#add_product').submit(function(e){
  $(document).on('submit', '#add_product', function(e) {
    e.preventDefault();
    $.post('script/test.php', {
        naam: $('#naam').val()
      })
      .done(function(response) {
        bootbox.alert(response);
        parent.fadeOut('slow');
      })
      .fail(function() {
        bootbox.alert('error');
      })
  });
});

<form id="add_product" method="post">

        Productnaam*
        <input maxlength="250" id="naam" type="text" required="required" class="form-control" name="naam" />

      <button class="btn btn-success completeBtn btn-lg" name="submit" type="submit" style="width: 100%;">
                         Toevoegen
                    </button>

</form>
Comment: did you try 'add_product' instead of '#add_product'?
Comment: @readyfreddy Tnx, but that doesn't seems to work.
Answer: As api.jquery states,

The .submit() method is a shortcut for .on( "submit", handler ) in the first variation, and .trigger( "submit" ) in the third.

So the behaviour of both the constructs should be the same.
Also, you are delegating your submit event from #add_product to the document which is low in performance cause it now has to listen for all submit events in the document.
And maybe because of the event delegation, you might have had an e.stopPropagation somewhere in your code which would stop the natural bubbling up of your event to the document, hence no execution of the handler.
It's much better to restrict the scope a little than listen on the document, if possible.
Try the following:
$(document).ready(function(){
        $('#add_product').on('submit', function(e) {
        e.preventDefault();
        $.post('script/test.php',
            {naam: $('#naam').val()})
            .done(function(response){
                bootbox.alert(response);
                parent.fadeOut('slow');
            })
            .fail(function(){
                bootbox.alert('error');
            })
    });
});

Comment: Tnx, but if i use $('#add_product').on('submit', function(e) he cant log the click and reloads the page.
Comment: I see even without the jquery it doesn't check the required fields.
Comment: It seems like the validation is not working corectly after form is loaded with an ajax call
Answer: try to add 'return false' in your handler:
$(document).on('submit','#add_product',function(e){
        e.preventDefault();
        $.post('script/test.php',
            {naam: $('#naam').val()})
            .done(function(response){
                bootbox.alert(response);
                parent.fadeOut('slow');
            })
            .fail(function(){
                bootbox.alert('error');
            })
        return false;
    });

Comment: It seems like the validation is not working corectly after form is loaded with an ajax call
Answer: Try the following:

$(document).ready(function() {
    $('#add_product').on('submit', function(e) {
        e.preventDefault();
        $.post('script/test.php', { naam: $('#naam').val() })
            .done(function(response) {
                bootbox.alert(response);
                parent.fadeOut('slow');
            })
            .fail(function() {
                bootbox.alert('error');
            })
    });
});

<form id="add_product" method="post">

                            Productnaam*
                            <input maxlength="250" id="naam" type="text" required="required"
                                   class="form-control" name="naam"/>

                    <button class="btn btn-success completeBtn btn-lg" name="submit"
                            type="submit" style="width: 100%;">
                         Toevoegen
                    </button>

                </form>
Comment: It seems like the validation is not working corectly after form is loaded with an ajax call


Where is the changelog?

Question: I got a project which need to go from config v1.30.0 to v2.0.1 if I do the upgrade. I searched the repository for a Changelog either on the README or as a separated file, also searched in the releases part on github, checked the Wiki and read the "Future Compatibility" and the "Ample Warning", however none of that assures me that things will not break after the update. Also no reference to the work "break" on a git log over the repo.
Is there another place I should look to find the changes, I'd really like the package to have a better way of notifying changes so one can safely upgrade.
Another question: could this be considered a feature request, so I can file an issue on the repo?
Answer: This package comes with a History.md file, at the root of the sources, that acts as a changelog.
Here it is: https://github.com/lorenwest/node-config/blob/master/History.md

Where is the changelog?


Using TableValuedParameter with Dapper Conversion Fails for string to smalldatetime

Question: I get this error when converting a datatable to a TableValuedParameter using dapper. Am I missing something in my conversion to datatable, does DBNull.Value work with nullable datetimes? Is there a mismapped property I'm not seeing in my code?

System.Private.CoreLib: Exception while executing function: MyProject. Core .Net SqlClient Data Provider: Conversion failed when converting character string to smalldatetime data type.

I create the List of data to the datatable
List<PersonData> personDatas = maps.Select( mappedData =>
    new PersonData
    {
        PersonId = mappedData.PersonId,
        StartDate = mappedData.StartDate,
        EndDate = mappedData.EndDate,
        CurrentDate = DateTime.Now,
        SomeString = mapped.SomeStringData
    })
    .ToList()
    .ToDataTable();

Method for ToDataTable can be found here
PersonData is defined as
public class PersonData
{
    public int PersonId { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public string SomeString { get; set; }
    public DateTime? CurrentDate { get; set; }
}

Then I execute the query with Dapper like this
using (var cn = new SqlConnection(connectionString))
{
    cn.Open();
    //This is where the exception happens
    cn.Execute(RepositorySql.MergeSql(), new
    {
        UserDefinedTable = dataTable.AsTableValuedParameter("[dbo].[PersonData]")
    });
    cn.Close();
}

the Sql is
MERGE dbo.tblPersonData AS target
USING @UserDefinedTable AS source
ON target.PersonID = source.PersonID
    WHEN MATCHED
    THEN UPDATE SET
                    StartDate = source.StartDate,
                    EndDate = source.EndDate,
                    --UpdatedDate instead of InsertedDate
                    UpdatedDate = source.CurrentDate,
                    SomeString = source.SomeString
    WHEN NOT MATCHED BY TARGET
    THEN
        --Inserted date instead of UpdatedDate
        INSERT(PersonID, StartDate, EndDate, InsertedDate, SomeString)
        VALUES
            (source.PersonId, source.StartDate, source.EndDate, source.CurrentDate, source.SomeString)

The user Defined Table is
CREATE TYPE [dbo].[PersonData] AS TABLE(
   PersonId int NOT NULL
   ,SomeString varchar(50) null
   ,StartDate smalldatetime null
   ,EndDate smalldatetime null
   ,CurrentDate smalldatetime null)

Answer: The problem was because the serialization of the object was putting the columns in different order than the User Defined Table so they where trying to save dates into teh string column. I changed the object to this and the error was resolved, notice the order of properties has changed:
public class PersonData
{
    public int PersonId { get; set; }
    public string SomeString { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public DateTime? CurrentDate { get; set; }
}


jQuery not working on Windows Server 2008, IIS 7

Question: I am deploying an ASP.NET MVC to Windows Server 2008. I have installed ASP.NET MVC 1, but jQuery is not working on any of the pages. Anyone had this problem?
Comment: Can you be a bit more specific?
Comment: I'm having the same problem... specifically on Windows Server 2008/IIS 7 and the jQuery Datepicker. Did you manage to find a solution?
Answer: I suspect a problem with the path being used to load the scripts.  Is your web site at the root or in a subdirectory on the server?  See this question and see if the answers there don't help.
jQuery library location wants to be referenced differently on server
Comment: This does not seem to be the problem
Comment: All my scripts are accessible(can download them) from the page's source view and some of the code in my script file is working fine by ajax call aren't, any clue?


Analyzing BSOD dump file from Windows 2008 R2 SP1

Question: I have a problem with a virtual Windows Server 2008 R2 SP1 server running withing VMWare. The server is running Citrix and also has Symantec Endpoint protection installed. It randomly crashes and goes to BSOD.
Investigating the event log didn't yield any useful information about the cause of the crash. I ran windows debug and generated the report shown below. Apparently it points to a failed driver. The problem is I can't pinpoint what driver is causing it. I'm wondering if anyone can offer some help.
<code>----------
## Bugcheck Analysis   ##
SYSTEM_SERVICE_EXCEPTION (3b)
An exception happened while executing a system service routine.
Arguments:
Arg1: 00000000c0000005, Exception code that caused the bugcheck
Arg2: fffff9600008744d, Address of the instruction which caused the bugcheck
Arg3: fffff88007ba3de0, Address of the context record for the exception that caused the bugcheck
Arg4: 0000000000000000, zero.

Debugging Details:
------------------
Page 125923 not present in the dump file. Type ".hh dbgerr004" for details

EXCEPTION_CODE: (NTSTATUS) 0xc0000005 - The instruction at 0x%08lx referenced memory at 0x%08lx. The memory could not be %s.

FAULTING_IP:
win32k!xxxInternalInvalidate+7d
fffff960`0008744d f6473208        test    byte ptr [rdi+32h],8

CONTEXT:  fffff88007ba3de0 -- (.cxr 0xfffff88007ba3de0)
rax=0000000000000000 rbx=0000000000010485 rcx=0000000000000000
rdx=0000000000000b02 rsi=0000000000000000 rdi=0000000000000000
rip=fffff9600008744d rsp=fffff88007ba47c0 rbp=0000000000000000
 r8=0000000000010485  r9=0000000000000000 r10=fffff900000004c0
r11=fffff900c26eac30 r12=0000000000000000 r13=0000000000000001
r14=0000000000000000 r15=0000000000000000
iopl=0         nv up ei pl zr na po nc
cs=0010  ss=0018  ds=002b  es=002b  fs=0053  gs=002b             efl=00010246
win32k!xxxInternalInvalidate+0x7d:
fffff960`0008744d f6473208        test    byte ptr [rdi+32h],8 ds:002b:00000000`00000032=??
Resetting default scope

DEFAULT_BUCKET_ID:  WIN7_DRIVER_FAULT

BUGCHECK_STR:  0x3B

PROCESS_NAME:  csrss.exe

CURRENT_IRQL:  0

LAST_CONTROL_TRANSFER:  from fffff960001351a2 to fffff9600008744d

STACK_TEXT:
fffff880`07ba47c0 fffff960`001351a2 : 00000000`00000000 00000000`00000040 fffffa80`0678d330 00000000`00000000 : win32k!xxxInternalInvalidate+0x7d
fffff880`07ba4840 fffff960`001352a2 : fffffa80`0678d330 00000000`00000000 fffff880`07ba4ca0 fffffa80`06109ab0 : win32k!xxxInternalUserChangeDisplaySettings+0x486
fffff880`07ba4900 fffff960`001330e3 : 00000000`00000000 00000000`00000000 fffff900`c0f9ead0 fffff900`00000040 : win32k!xxxUserChangeDisplaySettings+0x92
fffff880`07ba49f0 fffff960`00115cba : 00000000`00000001 00000000`00aff960 00000000`00000000 ffffffff`ffffffff : win32k!xxxRemoteReconnect+0x6d7
fffff880`07ba4bf0 fffff800`016d9ed3 : fffffa80`06f66b00 fffff880`07ba4ca0 00000000`00000000 00000000`00000000 : win32k!NtUserCallOneParam+0x4e
fffff880`07ba4c20 000007fe`fd1b2aea : 00000000`00000000 00000000`00000000 00000000`00000000 00000000`00000000 : nt!KiSystemServiceCopyEnd+0x13
00000000`00aff918 00000000`00000000 : 00000000`00000000 00000000`00000000 00000000`00000000 00000000`00000000 : 0x000007fe`fd1b2aea

FOLLOWUP_IP:
win32k!xxxInternalInvalidate+7d
fffff960`0008744d f6473208        test    byte ptr [rdi+32h],8

SYMBOL_STACK_INDEX:  0

SYMBOL_NAME:  win32k!xxxInternalInvalidate+7d

FOLLOWUP_NAME:  MachineOwner

MODULE_NAME: win32k

IMAGE_NAME:  win32k.sys

DEBUG_FLR_IMAGE_TIMESTAMP:  50e64bda

STACK_COMMAND:  .cxr 0xfffff88007ba3de0 ; kb

FAILURE_BUCKET_ID:  X64_0x3B_win32k!xxxInternalInvalidate+7d

BUCKET_ID:  X64_0x3B_win32k!xxxInternalInvalidate+7d

Followup: MachineOwner
---------
</code>
Comment: Do you meant that the host is crashing or that the guest is crashing?  Also, since you only list one operating system (Server 2008 R2), is that the OS of the host, the guest, or both?  Also, what version of VMWare are you using?
Comment: Use [BlueScreenView](http://www.nirsoft.net/utils/blue_screen_view.html) to analyse the dump file.
Comment: Looks like it's related to changing a display setting, I'd guess the display driver first...
Comment: I tried to replicate that by changing resolution on the server couple of times with different settings, unfortunately or not but it didn't cause the blue screen.
Comment: Display driver problems on a Windows VM? - haven't seen that before. What version of Symantec EP? Are VMWare tools installed?
Answer: This looks like the issue described in the following KB article:
https://support.microsoft.com/en-us/kb/2359223
"0x0000003B" Stop error occurs in Windows Server 2008 R2 and in Windows 7 when an application or a service performs a GUI-related operation
The reason I think it is the issue described in the hotfix is because it specifically calls out the win32k.sys driver. Also, the CSRSS.exe process which also shows up in the dump is used in inter-process communication between user-mode GUI operations and the kernel which is also indicative of the issue described in the hotfix (reference: https://en.wikipedia.org/wiki/Client/Server_Runtime_Subsystem).
I would recommend applying the hotfix in the KB article and monitoring to see if the stop errors continue.


PostgresSQL upgrade from 9.6 to 10 on Windows fails when using pg_upgrade (fe_sendauth: no password supplied)

Question: Part of the work moving from Windows 2012 to 2019 involves upgrading the current install of Postgres from 9.6 to 10. I'm trying to use pg_upgrade and after some struggle with file permissions I have managed to start the process with the command
pg_upgrade.exe -b "c:/Program Files/PostgreSQL/9.6/bin" -B "d:/PostgreSQL/10/bin" -d "D:/PostgreSQL/9.6/data" -D "D:/PostgreSQL/10/data" --old-port=5432 --new-port=5433 -U postgres -v --check

It starts fine, but after a while it fails like this
"d:/PostgreSQL/10/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "D:/PostgreSQL/10/data" -o "-p 5433 -b -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c vacuum_defer_cleanup_age=0 " start >> "pg_upgrade_server_start.log" 2>&1

connection to database failed: fe_sendauth: no password supplied
could not connect to target postmaster started with the command:
"d:/PostgreSQL/10/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "D:/PostgreSQL/10/data" -o "-p 5433 -b -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c vacuum_defer_cleanup_age=0 " start

But if I try to run the command by itself, it starts without any issues. I have updated the pg_hba.conf-file so I can issue psql -U postgres -host 5433 without being prompted for password. Other than that I cannot find anything of value in the pg_upgrade*.log files.
Answer: on windows there are two things necessary to do before starting pg_upgrade

enable "passwordless" authentication by editing pg_hba.conf
replacing md5 or whatever other method by trust in both ipv4 & v6 sections

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
#host    all             all             127.0.0.1/32            md5
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
#host    all             all             ::1/128                 md5
host    all             all             ::1/128                 trust

command must be run under the same user as the database superuser  (postgres)
create a local user postgres and give him admin rights.
don't forget to also grant full control right to postgres\data folder in both   databases
then run cmd.exe from this user (as administrator)

at last , run the command from a location where postgres can read/write log files
run the most recent version of pg_upgrade.
you only need these parameters

d:\PostgreSQL\10\bin\pg_upgrade.exe -b "c:\Program Files\PostgreSQL\9.6\bin" -B
"d:\PostgreSQL\10\bin" -d "D:\PostgreSQL\9.6\data" -D
"D:\PostgreSQL\10\data"

edit: to improve security, postgres can run without admin privileges but require run as batch job & service privilege.
steps :

stop postgresql service
create local or domain user
add user to backup operator group (or log on as batch job right)
edit local security policy : security setting->user rights asssignment ->Log on as a service (add your user here)
edit service logon user and replace system by your dedicated user
change postgres home directory's access right to allow rw of your user.
start postgresql service


Masters and grandmasters play in chess tournament. Everyone won half their games against masters, prove that number of players is a perfect square.

Question: I got stuck at solving one problem in combinatorics recently. It goes as following:
Masters and grandmasters play in chess tournament, such that total number of players is $N$. Prove that $N$ is a perfect square if every plater won exactly half of their games against masters. (Every player faced every other player).
My reasoning goes like this:
Let $P$ be number of grandmasters.
Let $Q$ be number of masters.
The total number of wins masters got against masters is equal to the number of pairs of masters that faced each other - $Q\choose 2$
The total number of wins grandmasters got against masters is equal to the total number of their wins - the total number of their wins against grandmasters. This is $K - {P\choose2}$, but I got stuck at figuring out K. I've tried doing $P * (N - 1)$, but that obviously fails, as that includes wins of masters over grandmasters.
How would you approach calculating $K$, or suggesting another approach to the problem?
UPDATE: Fixed the type in question (Q and P are now assigned proprely)
Comment: When I first read the question, I thought that for each player X, half of X's games against masters were wins for X. But that doesn't seem to work since some players played an odd number of games against masters. So I guess that of each player's total games, half were wins against masters. (E.g., each player played 10 games in the tournament and of those 10 games, 5 were W's agains masters.) Is that right, or am I still missing the correct interpretation?
Comment: EDIT: My bad. The question states that player wins half of their points against masters (indicating half of their wins, not the total games).
Answer: Since half of anyone's wins have been in front of a master, the total number of master's losses is half of all games played so there have totally been $\frac{\binom{N}{2}}{2}$ master's losses. On the other hand exactly half of master's wins have been in front of another master so there have totally been $2\times\binom{M}{2}$ master wins. Hence total number of games that all masters have participated in is :$$\frac{N(N-1)}{4}+M(M-1)=M(N-1)$$
$$\iff N^2-N+4M^2-4M=4MN-4M$$
$$\iff (2M-N)^2=4M^2-4MN+N^2=N$$
So $N$ is a perfect square and we're done.
Answer: Let M and G be the number of Masters and grandmasters, respectively.
Number of plays between masters only - $\binom M2$
Number of plays between grandmasters only - $\binom G2$
Number of plays between a grandmaster and a master - MG
Now, for any master, for every win against a master, it must have another against a non-master(grandmaster). Similarly, for any grandmaster, any win against a grandmaster must have another against a master.
Hence,
$$
 MG = \binom M2 + \binom G2
$$
$$2*MG = M(M-1) + G(G-1)$$
$$ M + G = M^2 + G^2 - 2MG$$
$$ M + G = (M-G)^2 $$
where M+G equals the total number of players.


Group by and count foreign key

Question: I have a data model where I store certain Events that happen. Each Event is linked to an EventType. The data model roughly looks like this:
class EventType(models.Model):
    name = ...

class Event(models.Model):
    date = ...
    event_type = models.ForeignKey(EventType)

What I would like to know is how often each event time appeared. I tried it like this:
Event.objects.values('event_type', count=Count('event_type'))

But the result looks like this:
<QuerySet [{'count': 1, 'event_type': 71}, {'count': 1, 'event_type': 2}, {'count': 1, 'event_type': 71}, {'count': 1, 'event_type': 71}, ...

So the entries did not get grouped. How can I make it such that they are grouped?
Answer: You can use following query
Event.objects.all().values('event_type').annotate(count=Count('event_type'))

Group by and count foreign key


Is there an API for sharing an attachment in RingCentral Glip?

Question: I want to build an app that can monitor one Glip team for posts including screenshots and then post that message to another Glip team. I can read a post, download an attachment and repost / upload it, but is there a way to simply share an existing attachment without reposting it. This can be done in the app UI but I didn't see a share API in the Glip API Reference.
Here's the Glip API Reference which includes endpoints for creating, reading, updating and deleting posts but not sharing:

https://developers.ringcentral.com/api-reference/team-messaging

The icon for sharing is the 6th from the left in the app screenshot below.

Is there a way to do this in Glip without downloading and re-uploading the file?
Comment: Below are the useful links.
https://medium.com/ringcentral-developers/announcing-enhanced-posting-with-glip-attachments-and-the-glip-file-upload-api-25e0ac5c72ce

https://forums.developers.ringcentral.com/idea/772/new-glip-features-post-attachments-and-the-file-up.html
Answer: To share an attachment via the Glip API, create a new post with an existing attachment.
Create Post API
The Create Post API takes an optional attachments array which references existing attachments. Both the id and type properties are required. Both properties are present in the post API response.
POST /restapi/v1.0/glip/chats/{chatId}/posts

{
    "text": "Please check out this file",
    "attachments": [
        {
            "id":"123456789",
            "type":"File"
        }
    ]
}

Ref: https://developers.ringcentral.com/api-reference/Posts/createGlipPost

Example Get Posts API
The following is an example of a post showing the attachments array with the id and type properties. The attachment URL is an AWS Presigned Object URL as shown below.
GET /restapi/v1.0/glip/chats/{chatId}/posts

{
    "records": [
        {
            "id": "11111111",
            "creatorId": "22222222",
            "creationTime": "2019-08-26T21:41:56.648Z",
            "lastModifiedTime": "2019-08-26T21:41:56.648Z",
            "type": "TextMessage",
            "chatId": "33333333",
            "mentions": [],
            "attachments": [
                {
                    "id": "123456789",
                    "name": "sharedfile.png",
                    "contentUri": "https://glip-vault-1.s3.amazonaws.com/web/customer_files/44444444/testimage.png?Expires=55555555&AWSAccessKeyId=myAccessKeyId&Signature=myAWSPresignedObjectUrlSignature",
                    "type": "File"
                }
            ],
            "text": "Check this out!"
        }
    },
    "navigation": {}
}

Ref: https://developers.ringcentral.com/api-reference/Posts/readGlipPosts

Sharing Permissions
Attachments can only be shared by the original poster or within the same chat. If a different user wants to share an attachment in a different team, it will be necessary to download and repost the file, generating a new attachment id.
If a different user attempts to share an attachment in a different chat, a 403 Forbidden error will be encountered:
403 Forbidden

{
    "errors": [
        {
            "errorCode": "PST-011",
            "message": "The requester must be attachment creator or attachment must belong to the requested chat."
        }
    ]
}


Running a Linux executable via Process.Start gives: Exec format error

Question: I have a small C#/.NET6.0 commandline application that should run a certain linux command via System.Diagnostics.Process.
var processStartInfo = new ProcessStartInfo
{
    FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Compressonator/compressonatorcli_linux_x86_64_4.2.5150/compressonatorcli"),
    ArgumentList = { "-fd", compression, "-noprogress", fileName, outputFilename },
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false
};

var process = Process.Start(processStartInfo);
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.WriteLine(process.StandardError.ReadToEnd());
process.WaitForExit();

However this fails with the following exception on an Ubuntu 64bit running in a VirtualMachine:
System.ComponentModel.Win32Exception (8): An error occurred trying to start process '/media/sf_imageconverter/net6.0/Compressonator/compressonatorcli_linux_x86_64_4.2.5150/compressonatorcli' with working directory '/media/sf_imageconverter/net6.0'. Exec format error
   at System.Diagnostics.Process.ForkAndExecProcess(ProcessStartInfo startInfo, String resolvedFilename, String[] argv, String[] envp, String cwd, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

The strange thing is, that when running the process directly from a terminal on the same machine it works fine:
$/media/sf_imageconverter/net6.0/Compressonator/compressonatorcli_linux_x86_64_4.2.5150/compressonatorcli

The process is https://github.com/GPUOpen-Tools/compressonator/releases/tag/V4.2.5185
My .NET6.0 app is set to compile to AnyCPU but I have also tried the other options without success.
Comment: what happens when you set UseShellExecute = true ?
Comment: @DaiBok This seems to work, but then I can not capture the Standard Output and Standard Error streams, which are important my case.
Comment: This seems to be a good comprehention: https://developers.redhat.com/blog/2019/10/29/the-net-process-class-on-linux#
Answer: This error means that you are not launching an executable but a script. If you open the script in a text editor, you can see that the second line contains this code
#!/bin/bash

This is called a shebang and specifies what executable is used to run this script. If your system does not have /bin/bash you will get this error message.
Either make sure that bash is available or change the line to the shell you are using to execute the script directly.
Comment: Thank you! That solved the issue. I did not realize that it was a script because it didnt have a .sh or similar file extension.


Run command from different server using ssh2_exec() - php

Question: There are 2 servers (server A and server B). The server A will send a command and server B execute it.
I had try the below code running from server A to instruct server B do a job and it work successful:
<code>$connection = ssh2_connect('server.com', 22);
ssh2_auth_password($connection, 'username', 'password');

ssh2_exec($connection, 'mv /var/www/test.php /root');

var_dump($connection);
</code>
from the above code, it will move test.php to the root folder. The problem happen if I try running the .sh file using the same code, it seems not running at all. Below is my example code to run .sh file
<code>$connection = ssh2_connect('serverB.com', 22);
ssh2_auth_password($connection, 'username', 'password');

ssh2_exec($connection, './var/www/bbb_clean.sh');

var_dump($connection);
</code>
If I run the bbb_clean.sh manually from the server B, it works . Please help me.
Answer: I would try to use phpseclib, a pure PHP SSH implementation, and enable logging to see what's going on.  eg.
<code><?php
include('Net/SSH2.php');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->getLog();
?>
</code>


Difference to connect to LDAP by BindDn or by username

Question: We have an application which can only connect to a LDAP (AD) by using the BindDn.
When I try to connect from the server which host the application using a simple LDAP client (LDAPAdmin) it connects sucessfully.
Recently the sysadmin change the AD (probably to a new machine and new version) but we cannot connect to it using the BindDn anymore, we did not change anything on the settings (BindDn string) except the ldap url pointing to the new server.
EDIT:
Case 1 - connect with user/password
url     = ldap://myserver.mydomain.org
BaseDn  = OU=department,DC=mydomain,DC=org
user    = myuser
passw   = password

Case 2 - connect with bindDn
url     = ldap://myserver.mydomain.org
BaseDn  = OU=department,DC=mydomain,DC=org
bindDn  = CN=myuser,OU=Special Accounts,DC=mydomain,DC=org
bindpwd = password

The connection using case 1 works with the ldapAdmin as the Case 2 does not work from our app (same user and password in both cases).
UPDATE:
Error message is here
Is there a special setting on the LDAP server side to enable to connect using BindDn?
UPDATE:
Apparently the former server was a Windows 2003 Server and the new one is Windows 2008 Server.
Comment: You will need to talk to the AD admin to learn what he changed and work with him to enable your app again.
Comment: Yes, this is the case and we are investigating together... but I do not have all technical details on the AD infrastructure.
Comment: Have you verified connectivity to the LDAP server on TCP port 389 from the server trying the bindDN?  What is the LDAP or application error being generated when you try to connect?
Comment: I put the error screenshot, the error 52e means the credentials are not correct. After a while the account get blocked and we get a 775 instead 52e but that is a known issue.
Answer: LDAP admin will be Binding to database using specific credentials, you need to locate them and bind using them, but for core changes you need to find specific binding account details anyway your bind parameters will link to specific access control lists ACLs, so admin will have more privs than a user who can read the database.  OK.
Comment: I just do not understand your answer...


Tentei fazer um algoritmo que gere uma matriz aleatoria e exiba o menor elemento mais a sua posição

Question: Esse é o enunciado do exercico:
Desenvolver um algoritmo que gere uma matriz (7,4) contendo valores inteiros aleatórios. Após, encontrar o menor valor contido na matriz e sua posição.
Isso é o que consegui esboçar por enquanto:
from numpy import *

M1=random.randint(100, size=(28))

a=array(M1).reshape(7,4)

print("\nM1\n",a)

menor_valor=a.min()

pos=M1.index(a)

print("\nO menor valor da matriz é: ",menor_valor)

Comment: Por favor, esclareça seu problema ou forneça detalhes adicionais a fim de destacar exatamente o que você precisa. Da forma como está escrito atualmente é difícil dizer com exatidão o que você está perguntando.
Comment: Qual sua dúvida ?
Comment: Como faço para que o algoritmo exiba a posição do menor elemento?
Answer: Sem numpy eu consegui dessa forma:
Inicia a variável minimum em 0, procura o menor elemento de cada linha da matriz e os compara. Se o menor elemento da linha atual for menor que o menor elemento da linha anterior, o valor na variável minimum será substituído.
minimum, aux, position = 0, 0, (0,0)

for i in range (nRows):
    aux = minimum
    for j in range (nCol):
        # Procura o valor mínimo no vetor i que compõem a matriz
        minimum = (min(matriz[i]))
        # Operações de comparação
        if aux < minimum:
            minimum = aux
        elif aux > minimum:
            aux = minimum
            #Armazena a posição do menor elemento até o momento
            position = (i, matriz[i].index(minimum))
        else:
            continue

print(minimum)
print(position)

Answer: Não testei, mas pesquise sobre numpy.where()
Da uma olhada no final desse site: https://www.delftstack.com/pt/howto/python/python-find-index-of-value-in-array/
Deve resolver seu problema.
Comment: Sua resposta pode ser melhorada adicionando informações fundamentais. Basta [edit] para adicionar mais detalhes, como citações ou documentação para que outras pessoas possam confirmar que sua resposta está correta. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).
Comment: Deu certinho, amigo! Muito obrigado.
Comment: Cliquei na seta pra cima, pra dizer que a resposta foi util, mas contabilizou como -1. Desculpe, sou novato aqui e estou aprendendo a usa este ambiente
Comment: Tranquilo. É que como eu fiz uma resposta sem muita explicação o site está dizendo para eu adicionar mais informações, acho que por isso ele deu negativo. Também sou novato.


How's my proof, are there any mistakes I have made?

Question: Compute $\int \sin(x) \left( \frac{1}{\cos(x) + \sin(x)} + \frac{1}{\cos(x) - \sin(x)} \right)\,dx. $
First, simplify the two fractions into one using a common denominator
\begin{align*}
\int\sin(x)& \left(\frac1{\cos(x)+\sin(x)}+\frac1{\cos(x)-\sin(x)}\right)dx\\
   &= \int\sin(x)\left(\frac{\cos(x)-\sin(x)}{\cos^2(x)-\sin^2(x)}
                     +\frac{\cos(x)+\sin(x)}{\cos^2(x)-\sin^2(x)}\right)dx \\
&= \int\frac{2\sin(x)\cos(x)}{\cos(2x)}dx \\
&= \int\frac{\sin(2x)}{\cos(2x)}dx \\
\end{align*}
Let $u = 2x$. Taking the derivative, $\frac{du}{dx} = 2,$ or $dx = \frac{du}{2}.$
Plugging in $u$ for $2x,$ and $\frac{du}{2}$ for $dx,$ we get $\frac{1}{2}\int \frac{\sin(u)}{\cos(u)}\,du.$
Taking the integral of $\tan(u)$, as solved in problem 10(assume this is true), we get $-\log|\cos(u)|+C.$
Therefore, $\frac{1}{2}\int \frac{\sin(u)}{\cos(u)}\,du = -\frac{1}{2}\log|\cos(u)|+C.$
Finally, subbing $2x$ for $u$, we get $\boxed{-\frac{1}{2}\log|\cos(2x)|+C}$ where $C$ is a constant
Comment: looks correct to me
Comment: Your solution is correct.
Answer: Your solution is correct.
Alternatively you could've used the substitution $u=\cos(2x)$.