why output tex3D data are different from initial data?

Question: I make this program to practice cudaMemcpy3D() and Texture Memory.
Here comes the questions,when I print out tex3D data,it is not same as initial data.The value I get is ncrss times the initial value, and there are ncrss interval numbers  which equal to 0 between each other. If I set nsubs to 2 or other bigger one, the time should be ncrss*nsubs and interval will be ncrss*nsubs.
Can you piont out where I made the mistakes. I think it probably is make_cudaPitchedPtr at line 61, or make_cudaExtent at line 56. And also  may related with the way of array storaged.
So I come here for your help,appreciate for your comments and advices.
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<cuda_runtime.h>
  4 #include<helper_functions.h>
  5 #include<helper_cuda.h>
  6 #ifndef MIN
  7 #define  MIN(A,B)  ((A) < (B) ?  (A) : (B))
  8 #endif
  9 #ifndef MAX
 10 #define  MAX(A,B)  ((A) > (B) ?  (A) : (B))
 11 #endif
 12
 13 texture<float,cudaTextureType3D,cudaReadModeElementType> vel_tex;
 14
 15 __global__ void  mckernel(int ntab)
 16 {
 17         const int biy=blockIdx.y;//sub
 18         const int bix=blockIdx.x;//crs
 19         const int tid=threadIdx.x;
 20
 21         float test;
 22         test=tex3D(vel_tex,biy,bix,tid);
 23         printf("test=%f,bix=%d,tid=%d\n",test,bix,tid);
 24
 25 }
 26
 27 int main()
 28 {
 29         int n=10;//208
 30         int ntab=10;
 31         int submin=1;
 32         int crsmin=1;
 33         int submax=1;
 34         int crsmax=2;
 35         int subinc=1;
 36         int crsinc=1;
 37
 38         int ncrss,nsubs;
 39         ncrss=(crsmax-crsmin)/crsinc + 1;
 40         nsubs=(submax-submin)/subinc + 1;
 41         dim3 BlockPerGrid(ncrss,nsubs,1);
 42         dim3 ThreadPerBlock(n,1,1);
 43
 44         float vel[nsubs][ncrss][ntab];
 45         int i,j,k;
 46         for(i=0;i<nsubs;i++)
 47                 for(j=0;j<ncrss;j++)
 48                         for(k=0;k<ntab;k++)
 49                                 vel[i][j][k]=k;
 50         for(i=0;i<nsubs;i++)
 51                 for(j=0;j<ncrss;j++)
 52                         for(k=0;k<ntab;k++)
 53                                 printf("vel[%d][%d][%d]=%f\n",i,j,k,vel[i][j][k]);
 54
 55         cudaChannelFormatDesc velchannelDesc=cudaCreateChannelDesc<float>();
 56         cudaExtent velExtent=make_cudaExtent(nsubs,ncrss,ntab);
 57         cudaArray *d_vel;
 58         cudaMalloc3DArray(&d_vel,&velchannelDesc,velExtent);
 59
 60         cudaMemcpy3DParms velParms = {0};
 61         velParms.srcPtr=make_cudaPitchedPtr((void*)vel,sizeof(float)*nsubs,nsubs,ncrss);
 62         velParms.dstArray=d_vel;
 63         velParms.extent=velExtent;
 64         velParms.kind=cudaMemcpyHostToDevice;
 65         cudaMemcpy3D(&velParms);
 66
 67         cudaBindTextureToArray(vel_tex,d_vel);
 68
 69         printf("kernel start\n");
 70         cudaDeviceSynchronize();
 71         mckernel<<<BlockPerGrid,ThreadPerBlock>>>(ntab);
 72         printf("kernel end\n");
 73
 74         cudaUnbindTexture(vel_tex);
 75         cudaFreeArray(d_vel);
 76         cudaDeviceReset();
 77         return 0 ;
 78 }

Here comes the printf data,nsubs=1 and ncrss=2;
  1 vel[0][0][0]=0.000000
  2 vel[0][0][1]=1.000000
  3 vel[0][0][2]=2.000000
  4 vel[0][0][3]=3.000000
  5 vel[0][0][4]=4.000000
  6 vel[0][0][5]=5.000000
  7 vel[0][0][6]=6.000000
  8 vel[0][0][7]=7.000000
  9 vel[0][0][8]=8.000000
 10 vel[0][0][9]=9.000000
 11 vel[0][1][0]=0.000000
 12 vel[0][1][1]=1.000000
 13 vel[0][1][2]=2.000000
 14 vel[0][1][3]=3.000000
 15 vel[0][1][4]=4.000000
 16 vel[0][1][5]=5.000000
 17 vel[0][1][6]=6.000000
 18 vel[0][1][7]=7.000000
 19 vel[0][1][8]=8.000000
 20 vel[0][1][9]=9.000000
 21 kernel start
 22 kernel end
 23 test=1.000000,bix=1,tid=0
 24 test=3.000000,bix=1,tid=1
 25 test=5.000000,bix=1,tid=2
 26 test=7.000000,bix=1,tid=3
 27 test=9.000000,bix=1,tid=4
 28 test=1.000000,bix=1,tid=5
 29 test=3.000000,bix=1,tid=6
 30 test=5.000000,bix=1,tid=7
 31 test=7.000000,bix=1,tid=8
 32 test=9.000000,bix=1,tid=9
 33 test=0.000000,bix=0,tid=0
 34 test=2.000000,bix=0,tid=1
 35 test=4.000000,bix=0,tid=2
 36 test=6.000000,bix=0,tid=3
 37 test=8.000000,bix=0,tid=4
 38 test=0.000000,bix=0,tid=5
 39 test=2.000000,bix=0,tid=6
 40 test=4.000000,bix=0,tid=7
 41 test=6.000000,bix=0,tid=8
 42 test=8.000000,bix=0,tid=9

Comment: similar question is [here](http://stackoverflow.com/questions/25591045/cuda-3d-texture-interpolation)
Comment: make_cudaExtent(A,B,C),elements in this,which is the fast dimension?
Comment: i think array in host is vel[A][B][C],now in kernel is vel[C][B][A],how to correct it?by cudapitchedptr?
Answer: After a night thinking ,I find out the problem.
the cuda array load as M[fast][mid][low] while c array is M[low][mid][fast].
so dim3(),cudaExtent(),pitchedPtr()should be same to [low][mid][fast] or at least should be same as each other.


Adaptar código JS para IE8 e IE9

Question: Como posso fazer esse código abaixo funcionar no IE8 e IE9, porque ele funciona apenas no 10
$("#input_file").change( function(event) {
    var tmppath = URL.createObjectURL(event.target.files[0]);

    $("img").attr('src',tmppath);

});

Método que não funciona
URL.createObjectURL

Qualquer coisa já ajuda(bibliotecas, scripts e etc)
Comment: Olá, qual método ou objeto que não funciona no IE8 e 9 ?
Comment: @PauloHDSousa Editei!
Comment: @SilvioAndorinha já testaste isto: http://stackoverflow.com/a/11277737/2256325 ?
Comment: @Sergio, não tinha visto.. mas mesmo assim infelizmente ainda não resolve 100% do problema porque no IE 9 ainda não funciona
Comment: Acho que nos IEs anteriores ao 10 não tem jeito mesmo.
Comment: Para que serve esse método?
Comment: Pois como o @bfavaretto referiu isso não vai dar. O que queres fazer? fazer preview de imagens? Algo como isto: http://stackoverflow.com/q/4459379/2256325 ?
Comment: @SilvioAndorinha você quer apenas fazer um "preview" da imagem?
Comment: Eu não cheguei a trabalhar com o IE 10, portanto desconheço esse método. Imagino que terá que fazer uma função para verificar qual a versão do IE do cliente e se for versão anterior ao IE 10, trabalhar de outra forma. Dependendo do seu objetivo, exista uma forma que se aplique às versões anteriores e atual.
Comment: cara eu nem sei o que usar para isso funcionar. o IE é um atraso de vida. Não serve para absolutamente nada. Na verdade serve só para dar problemas
Comment: Respeito sua opinião, porém realmente existem situações em que o cliente exige que o sistema funcione (às vezes até exclusivamente) no IE, inclusive empresas multinacionais.
Comment: Só como complemento estou em um projeto para uma grande empresa que para homologá-lo precisa rodar no IE8. Então, infelizmente, o gosto pessoal não serve como parâmetro nem norteador.
Comment: Achei isso no Stack Overflow Inglês.
http://stackoverflow.com/questions/14206127/createobjecturl-does-not-work-in-ie10
Olhe se pode te ajudar.
Comment: Esta questão está resolvida?
Comment: @MiguelAngelo não.
Comment: É possível instalar algo no computador? Tipo um servidor... ou então o Flash no browser?
Answer: Biblioteca mOxie
Achei uma biblioteca capaz de fazer o que você está pedindo.
Trata-se de um polyfill listado no GitHub do modernizr chamado mOxie.
A biblioteca é fantástica, exceto para se iniciar o desenvolvimento. Tive de compilar ela, para depois começar a testar... ai corrigi uns erros de compilação, e uns erros no JavaScript gerado, ai finalmente funcionou.
Usei as seguintes partes do mOxie:

FileInput: componente que permite selecionar um arquivo e utilizar os arquivo que forem selecionados usando um FileReader
FileReader: permite ler um arquivo
Image: permite manipular imagens, tal como redimensioná-la

Pela dificuldade que tive na compilação, resolvi criar um repositório no GitHub com a versão já compilada:

mOxie-Compiled dentro da pasta \v1.2.1\dist

Exemplo de uso
Coloquei na pasta \v1.2.1\samples, o exemplo de como fazer o que você falou:

image-preview-before-upload.htm
<!DOCTYPE html>

    <form id="form1">

            Browse...

    </form>

Compatibilidade
Funciona no IE8 e IE9. Também no Chrome, FireFox e Opera.
Tive problema com o Safari, IE6 e IE7.
Talvez o melhor seja usar uma abordagem seletiva entre a minha resposta, o seu próprio código (deve funcionar no Safari), e a resposta do @CarlosMartins (que usa AlphaImageLoader, pelo que pesquisei funciona no IE6 e IE7). Você poderia usar comentários condicionais para atingir a compatibilidade total se quiser.
Dependências
Para funcionar no IE8 e IE9 o mOxie requer ou Flash ou SilverLight. Parece que eles possuem planos de suportar o uso de Java também, mas por enquanto não tem.
Answer: A última vez que fiz eu acabei ocultando quando fosse o ie8
no seu caso faria mais ou menos assim:

function createObjectURL ( file ) {
    if ( window.webkitURL ) {
        return window.webkitURL.createObjectURL( file );
    } else if ( window.URL && window.URL.createObjectURL ) {
        return window.URL.createObjectURL( file );
    } else {
        return null;
    }
}

$("#input_file").change( function(event) {
    var tmppath = createObjectURL(event.target.files[0]);
 if(tmppath)
  $("img").attr('src',tmppath);
 else
  $("img").hide()
});
Answer: A solução para o teu problema penso que está aqui. Testei no IE7+ e funciona.

    <form id="form1" runat="server">
        <input type='file' id="imgInp" onchange="readURL(this)" />
    </form>

Adaptar código JS para IE8 e IE9


What is a credibility value in meta-analyses?

Question: I came across the term credibility value in reference to meta-analytic analyses and am not familiar with it. It is similar to a credibility interval?
Here is the context:
"Because there is more variation in business units pooled across companies than there is within the average company across business units, estimated correlations are larger when true validity estimates are calculated for business units across companies. For instance, consider the estimates relative to the customer satisfaction–loyalty criterion. Without the between company range-restriction correction, the true validity value of overall satisfaction was .23 (True Validity A) with a 90% credibility value (CV) of .17. This is the relation expected within a typical company. With the between-company range-restriction correction (True Validity B), the true validity value of overall satisfaction was .28 with a 90% CV of .21. This is the relation expected in business units pooled across companies (i.e., in the total population of business units)" Harter et al. (2002).
Comment: The quote you give seems a mess. What does it mean by the true value do you think? When it calls it a validity value is it an estimate of a validity coefficient? If the credibility value it estimates is indeed a credible interval then it is an interval with upper limit infinity (or whatever the maximum possible value is) as @user106879 suggests in his/her answer.
Answer: I could assume that this meta-analysis is conducted with a Bayesian approach.
A credibility value is sort of a confidence interval with a probability distribution. You can read more here: Wikipedia
Focusing on the second paragraph you can see different methods to choose a credibility value for the interval:

Credible intervals are not unique on a posterior distribution. Methods for defining a suitable credible interval include:
  Choosing the narrowest interval, which for a unimodal distribution will involve choosing those values of highest probability density including the mode. This is sometimes called the highest posterior density interval.
  Choosing the interval where the probability of being below the interval is as likely as being above it. This interval will include the median. This is sometimes called the equal-tailed interval.
  Assuming that the mean exists, choosing the interval for which the mean is the central point.
  It is possible to frame the choice of a credible interval within decision theory and, in that context, an optimal interval will always be a highest probability density set.
Comment: That makes sense. Too bad the article does not do a good job of explaining it. Thanks!
Answer: I found a definition in this article - CUMULATIVE RESEARCH KNOWLEDGE AND
SOCIAL POLICY FORMULATION: The Critical Role
of Meta-Analysis which says that

This value is called the 90% credibility value; 90% of
  all values in the distribution of true validities lie above this value.
Comment: I like this as a simple way of explaining the idea. Thanks!


Java Invalid keystore format

Question: I have a private key with PKCS#8 encoding format as .pem (and .cert) file.
If i try to call following method:
 private KeyStore getKeyStore() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException{
     String ks = keystores.get(service);
     KeyStore keyStore = KeyStore.getInstance("JKS");
     keyStore.load(new Base64InputStream(new ByteArrayInputStream(ks.getBytes())), "secret".toCharArray());
     return keyStore;
 }

I get an error on line 4 "keystore.load...":
Caused by: java.io.IOException: Invalid keystore format
at sun.security.provider.JavaKeyStore.engineLoad(Unknown Source) ~[na:1.8.0_211]
at sun.security.provider.JavaKeyStore$JKS.engineLoad(Unknown Source) ~[na:1.8.0_211]
at sun.security.provider.KeyStoreDelegator.engineLoad(Unknown Source) ~[na:1.8.0_211]
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(Unknown Source) ~[na:1.8.0_211]
at java.security.KeyStore.load(Unknown Source) ~[na:1.8.0_211]
at java_security_KeyStore$load$0.call(Unknown Source) ~[na:na]

How can i fix this problem? I read that java does not support PKCS#8 encoding format...
If i try to import key manually like this:
keytool -import -alias *alias* -keystore cacerts -file *cert.pem*

I get this error:
keytool error: java.lang.Exception: Input not an X.509 certificate

I tried to convert pkcs8 to x509 in openssl (with openvpn in windwos)... unsuccessful.
It works with the old key. If i replaced the old key with the new private key, then it fails..
How can i fix this error?
Comment: I am not sure alias can contain star `*`. Try to import with different alias name
Comment: @RomeoNinov its only a placeholder.
Answer: It is possible that your Java versions is the problem.

Try this version openjdk version "11.0.17" or a new one.
Answer: My mistake: i try to add the content (as string) of the private key.
The correct way:
At first create a PKCS12 file (replace content of * * with the path to your files.) with openssl (i used openvpn on windows):
pkcs12 -inkey *key* -in *cert* -export -out myKeys.pkcs12

Use only the certificates (in my case .der files).
Import your keys into a keystore (.jks-file):
keytool -importkeystore -srckeystore myKeys.pkcs12 -srcstoretype pkcs12 -destkeystore *keystoreName*

For initialising a String with the "content" of .jks file i implement following code:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

import java.io.File;

public class ClassName{

    public static void main(String[] args) {
        byte[] bytes = FileUtils.readFileToByteArray(new File("Path to .jks file"));
        String bdata = new String(Base64.encodeBase64(bytes, true));
        System.out.println(bdata);
    }
}

I take the printed lines and initialise the variable "ks" (see my code in the question) with it.
Answer: yeh I spend lot of hours before able to finalize that its my JDK issue. I have JDK 17 creating the jks file while container had ZULU 8 (pretty old) . so I downgraded my JDK to match ZULU and i worked . hope this helps someone

Java Invalid keystore format


Rodar Python e C# juntos

Question: Como posso no C# importar, ou seja, executar algum script em outra linguagem (Python)?
Exemplo:
Se escrever "R" em um programa em C#, executa um arquivo se chamado ApertouR.py.
Comment: Alguma das respostas resolveu sua dúvida? Acha que pode aceitar uma delas? Veja o [tour] como fazer isso, se ainda não o fez. Você ajudaria a comunidade identificando qual foi a melhor solução para você. Pode aceitar apenas uma delas. Mas pode votar em qualquer pergunta ou resposta que achar útil no site todo.
Answer: Tem o Python.NET e dever ser o mais adequado. Em alguns casos pode usar o IronPython. Desta forma as duas linguagens podem funcionar na mesma plataforma e coexistirem.
Tem outras formas, mas acho muito gambiarra. Uma delas é chamar o interpretador externamente como uma aplicação normal. Muita coisa pode sair errado nisto. É uma opção válida, mas não é das mais confiáveis.
Seria bom pensar se precisa mesmo disto, se não tem uma solução melhor. Não podemos ajudar muito mais porque a pergunta não diz o cenário onde será usado. Pode ser que nem precise do Python.
Coloquei no GitHub para referência futura.
Answer: Não tem como importar, ambas linguagens não tem relação alguma, agora ser quer disparar a execução de um Python através de um script c# então a história é diferente, é possivel
using System.Diagnostics;

class Exemplo
{
    static void Main()
    {
        Process.Start("python", @"c:\pasta\arquivo.py");
    }
}

Se python não estiver instalado globalmente (nas variáveis de ambiente), então faça assim:
Process.Start(@"c:\pasta_da_instalacao_do_python\python.exe", @"c:\pasta\arquivo.py");

Se quiser pegar o output/resposta faça conforme
Process process = new Process();
process.StartInfo.FileName = "python";
process.StartInfo.Arguments = @"c:\pasta\arquivo.py";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true; //Pega saída de erros
process.StartInfo.RedirectStandardOutput = true; //Pega a saída
process.Start();

StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();

Console.WriteLine(output);

process.WaitForExit();
process.Close();

Fonte:

ProcessStartInfo.StandardOutput
ProcessStartInfo.RedirectStandardError
Answer: Você pode fazer uma chamada de sistema
Process.start(@"python C:\ApertouR.py")

Mas não sei que tipo de aplicação é esta.

Rodar Python e C# juntos


Defining priors fom past years' data

Question: I'm trying to define a prior from published research.  Lets say several studies have collected data and then estimated some statistic $\hat{\theta}$, as well as the statistic's uncertainty $\hat{\mathrm{Var}}\left(\hat{\theta}\right)$.  For the sake of the argument, lets say there were 3 published studies, so
$$
\hat{\theta}_1 \mbox{ and } \hat{\mathrm{Var}}\left(\hat{\theta}_1\right)\\
\hat{\theta}_2 \mbox{ and } \hat{\mathrm{Var}}\left(\hat{\theta}_2\right)\\
\hat{\theta}_3 \mbox{ and } \hat{\mathrm{Var}}\left(\hat{\theta}_3\right)
$$
I want to define a prior distribution that takes into account this information, and lets say I know that this distribution is normally distributed a priori; $\theta \sim N(\mu,\sigma^2)$.
Answer: No, this is not how you should be finding the mean and variance. The prior precision should be the sum of the individual study precisions and the prior mean should be the precision weighted average of the individual study means.
Suppose that your published studies truly had normal posteriors and that your stated estimates and variances are actually posterior means and variances, i.e.
$$
p_i(\theta|y_i) = N(\hat{\theta}_i, \hat{\tau}_i^2)
$$
where $\hat{\tau}_i^2 = \hat{Var}(\hat{\theta}_i)$ for $i=1,\ldots,n$ where $n=3$ in your example. Then your prior should be the combination of each of these individual posteriors. If each of your individual studies are independent of each other and each had a uniform prior for $\theta$, i.e. $p_i(\theta)\propto 1$, and you had a normal prior for $\theta$, say $\theta\sim N(m,C)$, then
$$ \begin{array}{rl}
p(\theta|y_1,\ldots,y_n) &\propto \left[\prod_{i=1}^n p(y_i|\theta) \phantom{p_i(\theta)}\,\,\right] p(\theta) \\
&\propto \left[\prod_{i=1}^n p_i(\theta|y_i)p_i(\theta)\right] p(\theta)  \\
&\propto \left[\prod_{i=1}^n p_i(\theta|y_i)\phantom{p_i(\theta)}\,\,\right] p(\theta)  \\
&\propto N(\theta|\mu,\sigma^2)
\end{array} $$
where
$$
\sigma^2 = \left[ \frac{1}{C} + \sum_{i=1}^n \frac{1}{\hat{\tau}_i^2} \right]^{-1}
$$
and
$$
\mu = \sigma^2 \sum_{i=1}^n \left[ \frac{m}{C} + \frac{\hat{\theta}_i}{\hat{\tau}_i^2}\right].
$$
Letting $C\to \infty$ recovers the precision weighted average of means and posterior precision being the sum of the individual study precisions.
If the posteriors for the previous studies are not well approximated by normal distributions, then the answer will be more complicated. But this should be better than the equal-weighted average of means and simple average of variances.
Note that

as you have more data or have a study that provides an accurate estimate of the parameter, then the sum of the precisions will accurately reflect this increased certainty whereas a simple average will not and
the precision weighted average of the mean will not be poor when a study has a bizarre estimate but huge uncertainty while a simple average of the means will.
Comment: Thanks @jaradniemi!  If I understand correctly, you're suggesting a Bayesian meta-analytic approach to define the posterior of $\theta$ across multiple studies, which could then be used as a prior.

Defining priors fom past years' data


NodeJS: function vs. anonymous function

Question: Working on the stream-adventure NodeJS tutorial, but am struggling with the duplexer redux exercise.
This is what I've tried to use, but it doesn't work:
var duplexer = require('duplexer2');
var through = require('through2').obj;

module.exports = function (counter) {
  var counts = {};
  var input = through(write, end);
  return duplexer(input, counter);

  var write = function (row, _, next) {
    counts[row.country] = (counts[row.country] || 0) + 1;
    next();
  }

  var end = function (done) {
    counter.setCounts(counts);
    done();
  }
};

This is the proposed solution, which works:
var duplexer = require('duplexer2');
var through = require('through2').obj;

module.exports = function (counter) {
  var counts = {};
  var input = through(write, end);
  return duplexer(input, counter);

  function write (row, _, next) {
    counts[row.country] = (counts[row.country] || 0) + 1;
    next();
  }

  function end (done) {
    counter.setCounts(counts);
    done();
  }
};

Can someone help me understand the difference between using the anonymous function saved into a variable versus just naming a function?
Comment: The function gets hoisted to the top of the scope (hence before the return), the variable assignment stays in place (hence after the return and not evaluated).
Answer: The difference is hoisting. When you use a function declaration statement (i.e. what's being used in the proposed solution), its definition is "hoisted" to the top of the scope (read: function) that contains it, and therefore even if you try to reference it before the code that defines it, it will be available.
You are using a variable assignment to define your functions. This means that write will not have a value until the var write = statement executes. You are trying to use it before that, when write still has the value undefined.
What this means is that you can get your code to work simply by moving the location where you define your functions:
module.exports = function (counter) {
  var counts = {};
  var write = function (row, _, next) {
    counts[row.country] = (counts[row.country] || 0) + 1;
    next();
  };
  var end = function (done) {
    counter.setCounts(counts);
    done();
  };
  var input = through(write, end);
  return duplexer(input, counter);
};

n.b. Do not confuse function declaration statements with named function expressions. named function expressions are unhoisted, just like anonymous functions:
var boom = getNumber();
var getNumber = function getNumber() { return 3; };

Comment: Agreed. All variables belong at the top of a function body.
Comment: Not to necro the post, but typically most minifiers and even JS engines now days will automatically move all variables to the top of the function as an optimization step, though I typically still place all of mine at the top.

NodeJS: function vs. anonymous function


Spring boot rest api - Can I get response without creating any java class(DTO's or entity) for the response object?

Question: I'm implementing spring boot rest api application, in which I invoke stored procedure (which returns back refcursor). I need to show that response as JSON object with key, value pair without creating any DTO's or entity for that response. Is it possible to achieve the above requirement?
I get response(Cursor) from stored procedure.
@Override
public List getOrder(String orderNo) {
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("xx.xxx.get_order_details"); query.registerStoredProcedureParameter(1, String.class, ParameterMode.IN); query.registerStoredProcedureParameter(2, void.class, ParameterMode.REF_CURSOR);
query.setParameter(1, "OR-1001");
List results = query.getResultList();.
Comment: Could anyone knows on how to convert procedure response(may receive multiple cursor's as a response) to Map or to JSONNode ????
Answer: Yes, you can either return Map<String, Object> from your controller class or use the generic JsonNode class from Jackson.
An example of the Map might be the following (with the help of Java 11):
@RestController
@RequestMapping("/public")
public class PublicController {

  @GetMapping("/data")
  public Map<String, Object> returnFoo() {
    Map<String, Object> resultFromDatabase = Map.of("name", "duke", "data",
      Map.of("id", 1337, "enabled", true));
    return resultFromDatabase;
  }
}

The result is the following
curl -v localhost:8080/public/data

{"data":{"id":1337,"enabled":true},"name":"duke"}

It is also possible to just return Object, but then you have to make sure the result of your database is always parsable to JSON, as you otherwise might get exceptions:
@GetMapping("/data")
public Object returnFoo() {
  Map<String, Object> resultFromDatabase = Map.of("name", "duke", "data",
    Map.of("id", 1337, "enabled", true));
  return resultFromDatabase;
}

Comment: please add this code properly formatted to your question
Comment: Thanks @rieckpil. Added code snippet in my question.  Is there any way to convert resultSet to map or resultset to JSONNode?


Can a block span more than one region in a theme?

Question: Can a block span more than one region in a theme? Sometimes I would like content to do this, depending on the page.
I'm aware (and heavily use) Panels which are great but I thought it would be worth considering other solutions.
Answer: Not by default, but the MultiBlock module will solve this for you, out of the box.
Comment: +1 and Accepted WOW thanks - and for Drupal 7 too! "There's a module for that" :)
Comment: Also try http://drupal.org/project/panels_everywhere
Answer: Another option would be to consider Panels Everywhere: http://drupal.org/project/panels_everywhere
From the project page:

"Panels Everywhere is an advanced method to completely do away with
  Drupal's restrictive blocks system and instead use the much more
  freeing Panels Layout system to control how your pages look."

A further convincing point, referring to Drupal themes:

"Panels Everywhere modifies the page as it is being rendered to 'wrap'
  the content in a display and can even take over your page theme to do
  away with the need for a page.tpl.php."


Python 3.6 USZIPCODE Install

Question: I'm brand new to Python. I've been using Python through Anaconda. I'm running Python 3.6.5. Right now, I'm trying to install a package called USZIPCODE from https://pypi.org/project/zipcodes/. I downloaded the zip file from this site in to my downloads folder. I then tried to use $ pip install uszipcode from the CMD prompt. This doesn't work. "Invalid Syntax" is returned. I've looked at videos on how to use pip to install other packages but they have not been able to solve my problem. The picture below should give some insight in to the issue. Any pointers on how I can install this package into python? Thank you!  enter image description here
Comment: Are you running the command prompt as an administrator? I was able to succesfuly install the module with a: 'sudo pip install zipcodes' command on my linux box. So, try opening the command prompt as an administrator and typing: pip install zipcodes. If that doesn't work then let me know.
Comment: Unforunately that did not work. I tried both "sudo pip install uszipcodes" and "pip install uszipcodes" but neither worked. Do i need to tell the CMD prompt where the zip file is located in order for it to be extracted?
Comment: The zip file? Why do you have a zip file?  Are you trying to download the zipcodes module that you linked to or...?
Comment: As far as I can tell there is no such module: uszipcodes. The module you provided a link to is called: zipcodes. The only downloads available via the link you provided are for the zipcodes module. If (for some strange reason) you wanted to get the module via a zip or tar you could use those downloads then unzip or unpack them and add them to your modules folder. However, if you just want the module then its simple: sudo pip install zipcodes.
Answer: 2 years too late to help bbranham, but for other Anaconda Python beginners having trouble using pip install, use the "Anaconda Powershell Prompt". For instance, on Windows 7:

Hit the windows key and type "Anaconda Powershell Prompt" and select in the search bar to open the Anaconda Powershell cmd prompt.
If you are using different environments make sure to activate your environment with conda activate yourenvname
pip install uszipcode should work out then (to download and install the uszipcode package to your active anaconda environment).

Python 3.6 USZIPCODE Install


StrongswanPKI - Adding status_request or MustStaple TLS extension to certificate?

Question: Using PKI tool from strongswan to setup a CA.
Trying to setup the OCSP side of things have run into many issues as per another thread I posted (Strongswan PKI - ED25519 Certifcates - OCSP Responder having issues).
Now I am looking at how to add stapling.
Is there a way of creating certificates with strongswan pki that use the MustStaple Extension or as the latest openssl refers to it status_request?
I can't seem to find the documentation on it nor any information on it. Can't even find out how to add extensions to the certificate with strongswan pki.
Answer: The X.509 extension you refer to is actually called "TLS feature extension" and is defined in RFC 7633. If defined, it contains a list of TLS extension identifiers of which one might be status_request. So it's quite specific to TLS.
Likewise, OCSP stapling is a feature of TLS (via its Certificate Status Request extension as defined in RFC 6066) so it's not directly applicable to IKEv2.
However, there is a similar feature defined in RFC 4806, which allows exchanging OCSP responses during IKEv2 authentication. strongSwan currently doesn't support it, though.
For these reasons the extension is currently not relevant to strongSwan, which is why the pki tool doesn't provide support for it.
Comment: Okay that is unfortunate as I am trying to deploy a Ed25519 Certificate and OpenSSL wouldn't create the certificates. Is there a tool that I can use that does support it for ed25519 certificates?
Comment: You could always use a newer version of OpenSSL that supports Ed25519 to generate your certificates (e.g. in a Docker container).
Comment: Is it just the docker container one? because when i built with the April 2018 release it had ed25519 as an algorithm option but not for any thing else like CA, x509, ecparam or ec. The only thing that was available to use Ed25519 for was req. Unless there was some configuration I was meant to do? to get those other commands working with ed25519
Comment: No, Docker was just a suggestion if you didn't want to mess with the OpenSSL version installed on your system. Just use a new enough version (install it on your system, use Docker, whatever...) and you should be able to use Ed25519 for the certificates (that's at least what [this ticket](https://github.com/openssl/openssl/issues/487) and this [related one](https://github.com/openssl/openssl/pull/3503) suggest).
Comment: So below is what ive used to generate the root CA and under this comment I will add the intermediary which is where it falls apart.

`sudo openssl genpkey -algorithm ED25519 -out private/ca.cheese.key.pem` to generate the private key for the root ca

`sudo openssl genpkey -algorithm ED25519 -out intermediate/private/int.cheese.key.pem`
to generate the private key for the intermediary
`sudo openssl req -config openssl_root.cnf -new -x509 -extensions v3_ca -key private/ca.cheese.key.pem -out certs/ca.cheese.crt.pem`to create the root ca certificate.
Comment: `openssl req -config intermediate/openssl_intermediate.cnf -new -key intermediate/private/int.cheese.key.pem -out intermediate/csr/int.cheese.csr` this is the generate the csr in preparation for getting the certificate signed
`openssl ca -config openssl_root.cnf -extensions v3_intermediate_ca -days 3600 -md sha256 -in intermediate/csr/int.cheese.csr -out intermediate/certs/int.cheese.crt.pem` This is where it falls appart with 140523012273920:error:1010F08A:elliptic curve routines:pkey_ecd_ctrl:invalid digest type:crypto/ec/ecx_meth.c:783
When attempting debug - removing md does not work
Comment: Or should I be closing this question and creating a new one ? As this seems to be a bit away from strong Swan now
Comment: You should definitely remove `-md`, but also make sure you don't have `default_md` set anywhere in `openssl_root.cnf` (Ed25519 always uses SHA2-512).
Comment: I have made sure in both intermediary and root ca cnf files make no mention to digest files. However I get an error message even after regenerating root and intermediary req files. The error is as follows `no default digest
139869736675072:error:0E06D06C:configuration file routines:NCONF_get_string:no value:crypto/conf/conf_lib.c:275:group= name=unique_subject` This reads to me like it needs the message digest to be there in some manner; ive tried parsing NULL to it, tried just giving it garbage, tried SHA2-512. All didn't work. Any ideas?
Comment: I guess, there are some changes missing to make this work. Compared to `req`, the `ca` command seems to insist on a digest. `req` on the other hand is perfectly fine passing NULL when singing, which is what OpenSSL's Ed25519 implementation [apparently requires](https://github.com/openssl/openssl/blob/8c8fbca92dc95bb8672dea194bbe414059a874d2/crypto/ec/ecx_meth.c#L780), and which happens if you don't configure a digest in `[ req ] default_md` or via command line (like `ca`, it fails if you do). You should probably [file an issue](https://github.com/openssl/openssl/issues).
Comment: I guess you already saw, the problem with the `ca` command and Ed25519 is now [fixed](https://github.com/openssl/openssl/commit/f3021aca4a154c2ff9bd0030f7974eb6a719550d).
Comment: In fact I did not. Thank you for pointing that out.


Is this picture of a differential $1$-form correct?

Question: Consider the following picture of $x dy$:

(this picture appears in this article).
I believe these lines should be vertical:
At each point in $\mathbb R^2$  a basis for the tangent space is given by $dx, dy$. If $x$ and $y$ are the coordinates (local but in this case they are also global) then a point in $\mathbb R^2$ is given by $(x,y)$.
To get a basis for the tangent at a point one applies the partial derivatives and then evaluates at the point giving:
$$ dx = {\partial \over \partial x} (x,y) = (1,0)$$
and
$$ dy = {\partial \over \partial y} (x,y) = (0,1)$$
Then
$$ x dy = (0,x)$$
Which seems to me should be a vertical line.

What am I doing wrong? Please could someone help me understand this?
Answer: You are confusing an important point: the coordinate differentials $dx$ and $dy$ are (at each point) a basis of the cotangent space. What you are doing is identifying both spaces via the canonical inner product of $\mathbb R^2$, and saying $dy \mathrel{\hat=} \partial_y$. But then you miss an important point, namely that there is an important difference between forms and vectors. Their geometrical interpretations are straight lines (forms) and directed lines/arrows (vectors). Hence in your picture, the lines doesn't have arrows at the end!
To make this picture clear, let us leave differential geometry behind, because this is not the essential ingredient here and only complicates things. Let us consider some vector space $V$ and its dual space $V^*$. Vectors $v\in V$ have a straight geometric interpretation: They are elements of the vector space and can be identified with directed line segments going from the origin 0 to $v$. But how can we interpret forms $f\in V^*$? These are linear maps $f:\,V\rightarrow\mathbb{R}$ and hence as long as $f\neq 0$, their kernels always have codimension 1. Hence it seems kind of natural to take this subspace as the geometric interpretation of $f$. Since its codimension is 1, it divides $V$ in half-spaces $f\leq 0$ and $f\geq 0$.
Back to the original setting: Here we have $V=\mathbb{R}^2$ and hence the mentioned kernels are 1-dimensional. Additionally, we now have to deal with differential forms and vector fields, which is essentially the same, except that we have to make every concept point-wise.
We want to compute the kernel of dy. The action of $dy$ on a vector field $X=X_x \partial_x + X_y \partial_y$ is given by $dy(X) = X_x dy(\partial_x) + X_y dy(\partial_y) = X_y$, so it picks out the $y$ component of $X$.$^1$ Then the solution of the equation $dy(X)=X_y=0$ are those vector fields which are proportional to $\partial_x$. This means that at each point, the kernel of $dy$ is just the $x$ axis of the tangent space.
Finally, this is why we identify $dy$ with horizontal lines. Since $dy$ and $f(x,y)dy$ would result in the same line, we could say that we use a horizontal line, starting at $(x,y)$ which is $|f(x,y)|$ long and end up with your picture.
$^1$ Alternatively, this can be computed using the inner product via $dy(X) = \langle \partial_y, X \rangle = X_y$.
Comment: Thank you for your answer. But I'm still confused and need a little bit more help: It seems to me that you are confirming what I wrote. If $dy$ picks out the $y$ component it seems to me that $x dy$ would be a vertical line. It's really not clear to me. When you write for example $dy$ do you implicitly mean the line that is the kernel of the derivative with respect to $y$?
Comment: The point is that you can identify vector fields and differential forms if you have metric - this is essentially the same as identifying $V$ and $V^*$ after having chosen an inner product. But these two objects have a totally different geometrical interpretation in the first place. Besides this, if $dy$ picks out the $y$ component, $dy=0$ means that your vectors only have $x$ components, so point in $x$ direction. See my edited posts for some more details about the geometry of dual spaces.


add region with module installation

Question: I am developing a module, that when installed, should place an HTML element in a drupal block.
That block, will be inside a region. I would like the region to be added to the theme on module installation. Any workarounds?
Answer: you cant create custom region in a theme using custom module in a drupal way . however you can fetch available regions

add region with module installation


How exactly do we get that for every $n>kN$ we have $|S_n-T|<\epsilon$?

Question:
Let $(a_n)$ be a sequence such that $a_n\to 0$ and let $k$ be a natural number such that $k>1$.
Let $b_1=a_1+\dots+a_k$ and $b_{n+1}=a_{kn+1}+a_{kn+2}+\dots+a_{k(n+1)}$ for every $n\geq 1$.
Prove that if $\sum b_n$ converges then $\sum a_n$ converges.

The proof:
Let $$T_n=\sum_{i=1}^{n} b_i,S_n=\sum_{i=1}^{n} a_i$$ and let $T_n\to T$.
Let $\epsilon>0$. We have $$S_{kn}=\sum_{i=1}^{kn}a_i=\sum_{i=1}^{n}b_i=T_n\to T$$ and therefore there exists an $N_0$ such that $|S_{kn}-T|<\epsilon$ for every $n>N_0$.
We have $$S_{kn+1}=T_n+a_{kn+1}\to T+0=T$$ and therefore there exists an $N_1$ such that $|S_{kn+1}-T|<\epsilon$ for every $n>N_1$.
We have $$S_{kn+2}=T_n+a_{kn+1}+a_{kn+2}\to T+0=T$$ and therefore there exists an $N_2$ such that $|S_{kn+2}-T|<\epsilon$ for every $n>N_2$.
...
We have $$S_{kn+(k-1)}=T_n+\sum_{i=1}^{k-1}a_{kn+i}\to T+0=T$$ and therefore there exists an $N_{k-1}$ such that $|S_{kn+(k-1)}-T|<\epsilon$ for every $n>N_{k-1}$
(we are given that $a_n\to 0$ and therefore for every $i=1,...,k-1$ we have $a_{kn+i}\to 0$).
But for every $n\in\mathbb{N}$ there exists an $i\in\{0,1,...,k-1\}$ and an $n_0$ such that $n=kn_0+i$ and therefore the sequences $$(S_{kn}),(S_{kn+1}),...,(S_{kn+(k-1)})$$ cover the sequence $(S_k)$. Define $N=\max\{N_0,N_1,...,N_{k-1}\}$ and we get that for every $n>kN$ we have $|S_n-T|<\epsilon$ and therefore $S_n\to T$ and in particular, the series $\sum a_n$ converges.
How exactly do we get that for every $n>kN$ we have $|S_n-T|<\epsilon$?
Answer: You have proved the following:

Proposition. For any $\epsilon>0$ and any $j\in\{0,\cdots,k-1\}$, there exists $N_j$, s.t. $n>N_j\implies|S_{kn+j}-T|<\epsilon$.

Now let $\epsilon>0$ be any positive number and let $N=\max_{j=0,\cdots,k-1}\{N_j\}$ where $N_j$ is given by the proposition above. For any $n>kN$, write $n=km+r$, where $r$ is the remainder of $n$ divided by $k$, so that $0\leq r<k$ and $m>kN/k=N\geq N_r$. Hence, by the proposition above, $m>N_r\implies |S_{km+r}-T|<\epsilon\implies|S_n-T|>\epsilon$.
Answer: Let $S_{n}=\sum_{i=1}^{n}a_{i}$ and $T_{n}=\sum_{i=1}^{n}b_{i}$.
Note that $S_{kn}=T_{n}$ for all $n\geq1$.
Suppose that $\sum b_n$ converges.
We go to show that $(S_{n})$
is a Cauchy sequence. Let $\varepsilon>0$ be arbitrary. Choose $N_{1}$
such that $|a_{n}|\leq\frac{\varepsilon}{k}$ whenever $n\geq N_{1}$
(This is possible because $a_{n}\rightarrow0$). Since $(T_{n})$
converges, we can choose $N_{2}$ such that $|T_{m}-T_{n}|<\varepsilon$
whenever $m,n\geq N_{2}$. Let $N=\max(N_{1},kN_{2})$. Let $m,n\geq N$
be arbitrary. There exist integers $l_{1},l_{2}$ such that $l_{1}k\leq m<(l_{1}+1)k$
and $l_{2}k\leq n<(l_{2}+1)k$. Clearly $l_{1},l_{2}\geq N_{2}$.
(For, if $l_{1}<N_{2}$, then $m<(l_{1}+1)k\leq N_{2}k\leq N$, contradicting
to the condition that $m\geq N$). We have estimation:
\begin{eqnarray*}
 &  & \left|S_{m}-S_{n}\right|\\
 & \leq & \left|S_{m}-S_{l_{1}k}\right|+\left|S_{l_{1}k}-S_{l_{2}k}\right|+\left|S_{l_{2}k}-S_{n}\right|\\
 & \leq & \left\{ |a_{l_{1}k+1}|+\ldots+|a_{m}|\right\} +\left|T_{l_{1}}-T_{l_{2}}\right|+\left\{ |a_{l_{2}k+1}|+\ldots+|a_{n}|\right\} \\
 & \leq & \frac{\varepsilon}{k}(m-l_{1}k)+\varepsilon+\frac{\varepsilon}{k}(n-l_{2}k)\\
 & \leq & 3\varepsilon.
\end{eqnarray*}
This show that $(S_{n})$ is a Cauchy sequence and hence it converges.


Como permitir que o meu app seja movido para o cartão de memória?

Question: Todos os aplicativos que estou desenvolvendo de testes não permitem que eu mova-os para o cartão de memoria. Tem como fazer isso? Tem que pedir alguma permissão no Manifest.xml?
Coloquei a propriedade que o android:installLocation no Manifest.xml e até funcionou, mas estou com o seguinte problema.
Quando eu troco de celular para o Cartão e tento abrir o app da erro, mas se eu reiniciar o celular e abrir o app de novo funciona, dai eu passo para o celular e quando vou abrir o app da erro, mas se eu reiniciar e abrir o app funciona e assim fica.
Eu até consegui trocar de um lugar para outro, mas preciso reiniciar o celular para funcionar o app. Vou colocar aqui um trecho do erro oque está dando:
09-21 01:03:46.868  21820-21820/com.anderson.escuta W/art? Failed to find OatDexFile for DexFile /mnt/asec/com.anderson.escuta-1/pkg.apk in OatFile /data/dalvik-cache/mnt@email@example.com@firstname.lastname@example.com
09-21 01:03:46.868  21820-21820/com.anderson.escuta E/art? oat file /data/dalvik-cache/mnt@email@example.com@firstname.lastname@example.com does not contain contents for /mnt/asec/com.anderson.escuta-1/pkg.apk with checksum 786154219
09-21 01:03:46.868  21820-21820/com.anderson.escuta E/art? oat file /data/dalvik-cache/mnt@firstname.lastname@example.com@firstname.lastname@example.com contains contents for /data/app/com.anderson.escuta-2.apk
09-21 01:03:46.868  21820-21820/com.anderson.escuta A/art? art/runtime/class_linker.cc:955] Failed to remove obsolete oat file from /data/dalvik-cache/mnt@firstname.lastname@example.com@firstname.lastname@example.com: Permission denied
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203] Runtime aborting...
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203] Aborting thread:
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203] "main" prio=10 tid=1 Runnable
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   | group="main" sCount=0 dsCount=0 obj=0x653aa060 self=0x4191cdd8
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   | sysTid=21820 nice=-20 cgrp=apps sched=0/0 handle=0x400a1154
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   | state=R schedstat=( 40233070 20508181 144 ) utm=2 stm=2 core=1 HZ=100
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   | stack=0xbe526000-0xbe52a000 stackSize=8MB
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: art::Thread::DumpStack(std::ostream&) const+87 [0x41848214] (libart.so)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: ??? [0x418403a4] (libart.so)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: art::Runtime::Abort()+79 [0x41840934] (libart.so)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: art::LogMessage::~LogMessage()+505 [0x416eda7a] (libart.so)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: art::ClassLinker::FindDexFileInOatFileFromDexLocation(std::string const&, unsigned int)+571 [0x41709dd8] (libart.so)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: ??? [0x4181064c] (libart.so)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.DexFile.openDexFileNative(Native method)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.DexFile.openDexFile(DexFile.java:296)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.DexFile.<init>(DexFile.java:80)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.DexFile.<init>(DexFile.java:59)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.DexPathList.loadDexFile(DexPathList.java:263)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.DexPathList.makeDexElements(DexPathList.java:230)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.DexPathList.<init>(DexPathList.java:112)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:65)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:57)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   - locked <0x60c49c30> (a android.util.ArrayMap)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.app.LoadedApk.getClassLoader(LoadedApk.java:329)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   - locked <0x658d5558> (a android.app.LoadedApk)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.app.LoadedApk.makeApplication(LoadedApk.java:518)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4388)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.app.ActivityThread.access$1500(ActivityThread.java:139)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.os.Handler.dispatchMessage(Handler.java:102)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.os.Looper.loop(Looper.java:136)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at android.app.ActivityThread.main(ActivityThread.java:5103)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at java.lang.reflect.Method.invoke(Native method)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203] Dumping all threads without appropriate locks held: thread list lock
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203] All threads:
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203] DALVIK THREADS (13):
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203] "main" prio=10 tid=1 Runnable
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   | group="main" sCount=0 dsCount=0 obj=0x653aa060 self=0x4191cdd8
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   | sysTid=21820 nice=-20 cgrp=apps sched=0/0 handle=0x400a1154
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   | state=R schedstat=( 72628903 34617087 268 ) utm=5 stm=2 core=0 HZ=100
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   | stack=0xbe526000-0xbe52a000 stackSize=8MB
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: art::Thread::DumpStack(std::ostream&) const+87 [0x41848214] (libart.so)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: art::ThreadList::DumpLocked(std::ostream&)+67 [0x41853184] (libart.so)
09-21 01:03:47.128  21820-21820/com.anderson.escuta A/art? art/runtime/runtime.cc:203]   native: ??? [0x41840456] (libart.so)

Alguém tem alguma ideia do que possa ser?
Comment: Anderson, como assim mover? Você diz o usuário mover pelas configurações do sistema?
Comment: Isso mesmo! Eu vi que alguns app permitem isso, mas os que eu  fiz aqui nenhum permite. kkk
Comment: Anderson, qual versão do seu Android? Achei um bug: https://code.google.com/p/android/issues/detail?id=34880 relacionado a essa questão, que é da versão 4.1.1, talvez corrigido na 4.1.2. Parece que tem relatos que ainda persiste na versão 4.4...
Comment: A minha versão é a 4.1.1 mesmo. Mas o estranho que já testei vários app que baixei do playsotre até com versões bem inferiores e nunca tive esse problema. Não sei oque pode ser. ;/ Mas mesmo assim vlw
Comment: Não sei o que pode ser mesmo, tente em outro dispositivo. Não sei nem como ajudar muito, pesquisei bem e não achei nenhum padrão ou alguma solução...
Comment: ok, tudo bem.
Quando eu descobrir eu posto aqui.
Answer: Usando a propriedade android:installLocation da tag <manifest> é possível controlar a local de instalação do aplicativo.
Existem três valores possíveis:

internalOnly, que é o padrão caso não informe um valor para o atributo. Com ele o aplicativo é instalado na memória interna e não pode ser movido.
preferExternal define que a instalação deve ser feita preferencialmente no armazenamento externo. O Sistema pode não honrar essa requisição caso não haja nenhum armazenamento externo montado durante a instalação.
Com o valor auto o sistema instala o aplicativo na memória interna, mas fornece a possibilidade ao usuário de alterar para o armazenamento externo.

Para usar basta setar no seu AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nome.do.seu.pacote"
    android:installLocation="internalOnly|preferExternal|auto">

    ...

</manifest>

Referências:

http://developer.android.com/guide/topics/manifest/manifest-element.html#install
Comment: Coloca na sua pergunta, formatada como código, porque tem altura limitada.
Comment: Funcionou, consegui passar o app para o cartão de memoria, mas quando eu abro ele da vários erros, vou colar algumas linhas aqui:
art/runtime/class_linker.cc:955] Failed to remove obsolete oat file from /data/dalvik-cache/mnt@firstname.lastname@example.com@email@example.com: Permission denied

Dumping all threads without appropriate locks held: thread list lock

Na verdade o erro é bem grande mas não coube aqui, como devo fazer para reportar o erro nesses casos?
Comment: Estranho, pelo que entendi do erro, tem uma mistura de `dalvik` com `art`, mudou de `runtime` nesse meio tempo? Esse erro aparece naquele `Dialog` padrão do Android? Ou no logcat? Tente voltar o app todo pra memória interna e reinstala pra ver se resolve. Amanhã eu dou uma pesquisada melhor.
Comment: Não mudei nada, só coloquei o "android:installLocation".
Queria te mostrar o Log inteiro do erro, como faço? Pois não cabe aqui. Coloco como resposta da pergunta?


Consulta para calcular a quantidade de alunos do sexo feminino e do sexo masculino

Question: Nessa consulta eu usaria o GROUP BY?
Esse e o modelo
Comment: Sim, com *group by*. Já tentou algo? Se sim, poste na pergunta a consulta e os resultados que obteve.
Comment: Você quer a quantidade de alunos matriculados em algum curso de alguma disciplina, ou todos os alunos independentes disso? Etc...
Answer: Traduzinho o tabela Aluno do seu modelo para PL/PgSQL:
CREATE TABLE Aluno
(
  idaluno BIGINT PRIMARY KEY,
  nome TEXT NOT NULL,
  cpf VARCHAR(11) NOT NULL,
  rg VARCHAR(14) NOT NULL,
  sexo VARCHAR(1) NOT NULL,
  data_nascimento DATE NOT NULL
);

Cadastrando Alunos:
INSERT INTO Aluno ( idaluno, nome, cpf, rg, sexo, data_nascimento ) VALUES
( 100, 'JOAO', '11122233300', '1234567', 'M', '1980-03-10' ),
( 200, 'JESUS', '99988877766', '45678', 'M', '1970-06-17' ),
( 300, 'MARIA', '44455566677', '09876', 'F', '1953-04-30' ),
( 400, 'MADALENA', '33399955511', '345677', 'F', '1965-10-23' ),
( 500, 'JOSE', '00987243643', '8181818', 'M', '1988-03-03' );

Solução #1: Com GROUP BY e count():
SELECT
  sexo,
  COUNT(1)
FROM
  Aluno
GROUP BY
  sexo;

Saída:
| sexo | count |
|------|-------|
|    F |     2 |
|    M |     3 |

Solução #2: Com sum() e sem GROUP BY:
SELECT
   SUM(CASE WHEN sexo = 'M' THEN 1 ELSE 0 END) AS qtd_masculino,
   SUM(CASE WHEN sexo = 'F' THEN 1 ELSE 0 END) AS qtd_feminino,
   SUM(1) AS qtd_total
FROM
  Aluno;

Saída:
| qtd_masculino | qtd_feminino | qtd_total |
|---------------|--------------|-----------|
|             3 |            2 |         5 |

SQLFiddle: http://sqlfiddle.com/#!17/557d1/2


NullInjection Error for modular Angular Material Sidenav

Question: Building a sidenav using angular-material. Wanted to make it more modular so made the sidenav bar and the main toolbar different components. This caused a NullInjectorError: No provider for MatSidenavContainer! in our main toolbar test cases even though the MaterialModule/MatSidenavModule is available to the tests.
Tried adding MaterialModule (which contains modules for AngularMaterial) to the imports in the test suite. Also tried adding the other components (NavbarComponent and AppComponent) to the test suite. Also tried adding the modules separately into the file (e.g. import { MatSidenavModule } from '@angular/material/sidenav). Still throwing the error.
Setup
Angular 8
Angular Material 8
Jasmine: 3.4
Karma: 4.1
To reproduce error:
Clone repository https://github.com/ChadwickSchool/Weight-Lifting-App/tree/sidenav-test-error
From inside directory
git checkout sidenav-test-error
npm install
ng test

This is how the sidenav is set up
app.component.html
<mat-sidenav-container>
  <mat-sidenav #sidenav role="navigation">
    <wla-sidenav></wla-sidenav>
  </mat-sidenav>
  <mat-sidenav-content>
      <wla-navbar (toggleSidenav)="sidenav.toggle()"></wla-navbar>
      <router-outlet></router-outlet>
  </mat-sidenav-content>
</mat-sidenav-container>

Here goes the test case throwing the error:
describe('NavbarComponent', () => {
  let component: NavbarComponent;
  let fixture: ComponentFixture<any>;

  const authServiceStub = {
    user$: of(null),

    async googleSignin() {
      this.user$ = of(TestUtils.getTestUser());
    },

    async signOut() {
      this.user$ = of(null);
    }
  };

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [NavbarComponent],
      imports: [
        MatSidenavModule,
        MatIconModule,
        MatToolbarModule,
        NoopAnimationsModule,
        AngularFireModule.initializeApp(environment.firebaseConfig),
        AngularFirestoreModule,
        AngularFireAuthModule
      ],
      providers: [
        {
          provide: AuthService,
          useValue: authServiceStub
        }
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(NavbarComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Github repository
https://github.com/ChadwickSchool/Weight-Lifting-App/tree/sidenav-test-error
full error message
Answer: You should add MatSidenavModule to your module
imports: [
        MaterialModule,
        NoopAnimationsModule,
        AngularFireModule.initializeApp(environment.firebaseConfig),
        AngularFirestoreModule,
        AngularFireAuthModule,
        MatSidenavModule // this one
      ],

Comment: Tried it and it doesn't work. MatSideNavModule is already included in MaterialModule
Comment: Which version of Material you currently using ?
Comment: Angular Material 8.0.1
Comment: You should import each of the material modules from their source not just from angular/material
Comment: @YejielWahnich what exactly do you mean? Are you saying
`import { MatSideNavModule } from @angular/material` is the problem?
Comment: Per the documentation, angular material modules should be imported from its each module allowing tree shakeablility, https://update.angular.io/#7.0:8.0. you can also see in the example use : https://material.angular.io/components/sidenav/api
Comment: @YejielWahnich tried and it's still throwing the error. It seems to be just the main-toolbar component (`wla-navbar`) that's throwing the error.  The actual sidenav bar (`wla-sidenav`) test cases seem to be running fine. I created a special branch for it https://github.com/ChadwickSchool/Weight-Lifting-App/tree/sidenav-test-error


opencart 4 Multi language Remove ?language= from Url

Question: Hello Im trying to remove ?language= from my url in opencart 4

the exmpale is i Want the url change from this
exmpale.com/en?language=en-gb
exmpale.com/fr?language=fr

To
exmpale.com/en

First Step I added This code To (catalog/controller/startup/seo_url.php)
if($data['route'] === 'common/home') {
   if($data['language'] === 'fr' && $key ==='language') {
      $url = '/';
      unset($data[$key]);
   } elseif($data['language'] === 'en-gb' && $key ==='language') {
      $url = '/en';
      unset($data[$key]);
  }
}

that change worked and removed the ?language= from url but the route is giving me not found error
Answer: edited: oh, sorry, i simply misunderstoor your queestion.
you have to use few keys in oc_seo_url table:
language_id = 1 (let's say you have en-gb as ID 1)
key = language
value = en-gb
keyword = en
sort_order = -2

language_id = 2 (lets say fr have ID 2)
key = language
value = en-gb
keyword = en
sort_order = -2

language_id = 1
key = language
value = fr
keyword = fr
sort_order = -2

language_id = 1
key = language
value = fr
keyword = en
sort_order = -2

P.S.
In my case i added next code to remove main language from url at all
                if (empty($this->request->get['language'])) {
                    $this->request->get['language']='uk-ua';
                }

after foreach ($parts as $part) {...} (~line #20 in orig file)
in catalog/controller/startup/seo_url.php
seo url for that language is set to empty string in database.
P.S. and don't forget to modify rewrite function in same file like this.
replace
        foreach ($paths as $result) {
            $url .= '/' . $result['keyword'];
        }

with
foreach ($paths as $result) {
            if (!empty($result['keyword'])) {

                $url .= '/' . $result['keyword'];
            } else {
                $url .= $result['keyword'];

            }
        }


Split cart items when quantity is updated on cart page in WooCommerce

Question: I want to split cart items of same product in individual lines. When I increase quantity on single product page and add to cart, it shows as separate cart items.
Im using WooCommerce - Treat cart items separate if quantity is more than 1  answer code.
I want when quantity is updated on cart page and clicked on 'Update cart', then items must split on individual lines.
How can I do that?
Answer: The code below will split items on individual lines when quantity is updated on the cart page.
Comment with explanation added to the code.
<code>function on_action_cart_updated( $cart_updated ) {
    if ( $cart_updated ) {
        // Get cart
        $cart_items = WC()->cart->get_cart();

        foreach ( $cart_items as $cart_item_key => $cart_item ) {
            $quantity = $cart_item['quantity'];

            // If product has more than 1 quantity
            if ( $quantity > 1 ) {

                // Keep the product but set its quantity to 1
                WC()->cart->set_quantity( $cart_item_key, 1 );

                // Run a loop 1 less than the total quantity
                for ( $j = 1; $j <= $quantity -1; $j++ ) {
                    // Set a unique key.
                    $cart_item['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );

                    // Get vars
                    $product_id = $cart_item['product_id'];
                    $variation_id = $cart_item['variation_id'];
                    $variation = $cart_item['variation'];

                    // Add the product as a new line item with the same variations that were passed
                    WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item );
                }
            }
        }
    }
}
add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
</code>


Como fazer para ignorar uma linha vazia em uma tabela

Question: Estou fazendo uma tabela e quando um valor for vazio eu gostaria que ignorasse o valor e fosse preenchido com a próxima linha com valor.
Com o código abaixo, nada aparece, nem com valor e nem sem valor.

Observações

    <?php while ($row_itens_vazio = $resultado_itens_vazio->fetch_array()) {?>

      <?php if($row_itens_vazio["obs_tab_itens_cot"]>0){?>

           <?php echo $row_itens_vazio["obs_tab_itens_cot"];?>

           <?php } ?>

    <?php } ?>

Comment: A variável `$resultado_itens_vazio` está recebendo quais dados?
Comment: Posta a ```query``` pra gente.
Answer: Você pode resolver isso usando o continue e o empty do php.

Exemplo

Observações

    <?php while ($row_itens_vazio = $resultado_itens_vazio->fetch_array()) {
             //verifica se um valor é vazio.
             if(empty($row_itens_vazio['obs_tab_itens_cot'])) {
                 continue; //volta para cima no próximo registro
             }
      ?>

      <?php if($row_itens_vazio["obs_tab_itens_cot"]>0){?>

           <?php echo $row_itens_vazio["obs_tab_itens_cot"];?>

           <?php } ?>

    <?php } ?>


Best pattern for returning one of set of types of data

Question: I have  method that accepts a string and uses regex methods to determine if that string represents a float, int or string.
What is the best pattern to return the resultant float, int or string?
So my basic method prototype is:
void interpretString(const std::string &s);

What is the best way to get back the resultant float, int or string?
At present I extended my method as follows:
enum dataType { None, String, Float, Int };
dataType interpretString(const std::string &s,
                         string &s,
                         float &f,
                         int &i);

where the data is returned in the corresponding s, f or i variables. This seems unsatisfactory, as I am passing around data that will never get used (e.g. if the input string is a float, the string and integer return values will be unused)
Is there a better way?
Comment: Use `boost::variant` or define your own `union`.
Comment: Something like boost any might be a good return type. However, if you limit it to floating point, integer or string, you might use a union with type information (int, float string).
Answer: Here is another method using function objects:
struct Evaluation_Assignment
{
  virtual void assign(const std::string& s) = 0;
  virtual void assign(int i) = 0;
  virtual void assign(double d) = 0;
};

void Interpret_String(const std::string string_to_process,
                      Evaluation_Assignment& processor)
{
  if (interpretation_was_string)
  {
     processor.assign(s);
  }
}

Comment: Yes, it is incomplete, needs to be filled in by the OP.  As for performance, that was not a criteria.  This version is nice because you can pass different instances for different assignments.  It is presented as an alternative.


How can I terminate an application using Python on macOS?

Question: I have created a program and I want to make it exit Visual Studio Code once it is done with the program, how can I do that?
Comment: Is the VS Code process a parent of the Python process? Why do you want to close VS Code from Python?
Comment: Why do you need VS Code open to run your program? Can you just run it in the terminal?
Comment: I don't think you should do this, closing programs with Python is fairly easy, if you Google it you will find numerous ways to do this, however if you don't know how, then you should probably start with something easier, like bash. Though Python is powerful, it isn't designed to do administrative tasks, as simple tasks may require relatively complex solutions. To do system administration, PowerShell is your friend.
Answer: Possible solution is
import os
os.system('pkill <vs process name>')

..but it looks kind of lame. Consider to run your application from cli:
python app.py

Comment: I don't understand why anyone would want to run such a simple command using `os.system()` instead of directly running it in bash.


why is one point set in a first countable $T_1$ space a $G_\delta$?

Question: why is one point set in a first countable $T_1$ space a $G_\delta$ set?
I thought, in first countable space, there may be a point $x$ such that a local basis at x does not contain one point set ${x}$..
Thank you for attention in advance.
Answer: Let $X$ be a first countable $T_1$ space, and let $x\in X$. Since $X$ is first countable, there is a countable local base $\mathscr{B}= \left\{B_n:n\in \Bbb N \right\}$ at $x$. Let
$$ G=\bigcap_{B \in \mathscr{B}} B =\bigcap_{n\in\Bbb N}B_n; $$
clearly $G$ is a $G_\delta$ set in $X$, and I claim that $G=\{x\}$. Certainly $x\in G$. Suppose that $y\in X$ and $y \neq x$. Let $U=X\setminus\{y\}$; $X$ is $T_1$, so $U$ is open, and clearly $x\in U$. Thus, there is some $B_n\in\mathscr{B}$ such that
$$ x\in B_n\subseteq U. $$ But then
$$ G \subseteq B_n\subseteq U, $$
so $y\notin G$. This shows that $x$ is the only point of $X$ lying in $G$ and hence that $G=\{x\}$, so that $\{x\}$ is indeed a $G_\delta$ set in $X$.
Comment: @JeongNam-ho: If $X$ is not first countable, it is indeed quite possible to have a point $x\in X$ such that $\{x\}$ is not a $G_\delta$. (There are, however, $T_1$ spaces that are not first countable, but in which every $\{x\}$ is a $G_\delta$. First countability is sufficient for that, but not necessary.)
Comment: Thanks but I have one more question. If the condition 'first countable' omits, does not the statement hold?
Comment: What is the example that in $T_1$, but not first countble space, every one point set is a $G_\delta$?
Comment: @JeongNam-ho: There are many. One of the simplest is to let $\mathscr{U}$ be a non-principal ultrafilter on $\Bbb N$, let $p$ be any point not in $\Bbb N$, and let $X=\{p\}\cup\Bbb N$. A subset $U\subseteq X$ is open iff $p\notin U$ or $p\in U\in\mathscr{U}$. $X$ is Hausdorff, and since $X$ is countable and $T_1$, $\{x\}$ is a $G_\delta$ for each $x\in X$, but there is no countable local base at $p$.
Comment: I don't know the collection $\mathscr{U}$ exactly... Would you explain by using other representation?
Answer: Let $\{B_n:n\in\Bbb{N}\}$ be a local basis of $x$. We assume that each $B_n$ is open set. If $x\neq y$, then there is a open set $U$ satisfy that $x\in U$ and $y\notin U$. Therefore,
$$\bigcap_{x\in U,\text{$U$ open}} U=\{x\}$$
and you can easily chech that $x\in \bigcap_{n\in\Bbb{N}} B_n\subset \bigcap_{x\in U,\text{$U$ open}} U$. So $\{x\}$ is $G_\delta$.
Answer: The upper limit in the closed ordinal space [0, $\omega_1$] is not a $G_\delta$ and there is no countable basis at $\omega_1$.


Find and remove element with specific content

Question: i have lots of
 &nbsp
 tags in the middle of description contents. i want to find and remove the tags containing only &nbsp. The description container has a class-name desc_container.
Here is the example of container,

 Some contents!

 &nbsp

 Again some contents!

 &nbsp

Appreciate the help. Thanks!
Comment: Please include some of your html code and show us what you have tried to create this.
Answer: You need to check the $(this).html().trim() === ' ' inside the loop for each
 element and remove the
 element. You can use browser's inspect element option in the below example for further verification.

$(document).ready(function(){
  $('.desc_container p').each(function(){
    if($(this).html().trim() === ' '){
      $(this).remove();
    }
  });
});

 This is a example

 &nbsp
 for some content here
 test data

 &nbsp

Comment: Works like a gem! Thank yu.
Comment: @srihs glad to help!!
Answer: The :contains() selector selects elements containing the specified string. The following code will remove the
 tag which contains &nbsp and might have other content too.
$("p:contains(&nbsp)").remove();

If you want to remove the
 which contain only &nbsp then you can loop
 tag and remove whose value is &nbsp
$('p').each(function(){
   if($(this).text().trim() == "&nbsp"){
       $(this).remove();
   }
});

Answer: You don't need to use find since you can simply select all
 tags inside the desc_container.
Just use the following selector: $('.desc_container p') or jQuery('.desc_container p') if you haven't mapped the jQuery object to $.
This will return you all the Nodes, which can be looped with jQuery's each method and then removed with .remove(). Also, make sure to trim the string before comparing.

$(document).ready(function(){
  $('.desc_container p').each(function() {
    if($(this).html().trim() === ' '){
      $(this).remove();
    }
  });
});

Text

 &nbsp

Text

 &nbsp

Answer: You can try following

$(document).ready(function(){
  $(".desc_container p").each(function(){
      if($(this).text().trim() == "") {
      $(this).remove();
    }
  });
});

 Some contents!

 &nbsp

 Again some contents!

 &nbsp

Answer:

$("#deleteEmpty").on('click', function() {
  $(".desc_container p").each(function(index, element) {
    var $element = $(element);
    if ($element.html().trim() === " ") {
      $element.remove();
    }
  });
});
.desc_container p {
  padding: 5px 8px;
  background-color: #EEE;
  margin: 5px 0;
}

Sample text here

Sample text here

Sample text here

Sample text here

Sample text here

Sample text here

<button id="deleteEmpty">Delete empty `p` tags</button>


Hide block if no submenu in Twig

Question: I'm creating a node--page.html.twig and I'm using twig_tweak to help render a block for my sidebar to display the sub-menus. Is there a way to hide it if the page doesn't have any submenus?
Here's my code:

    {{ drupal_entity('block', 'mainmenu', check_access=false) }}

Is there some sort of if statement I could use to hide the block if the page has no submenu?
Thank you!
Answer: I generally handle this sort of thing like:
// First render my content and set it to a variable.
{% set submenu = drupal_entity('block', 'mainmenu', check_access=false)|render %}
// Next, check if the variable have content.
{% if submenu %}
  // If variable has content, render with wrapping div(s).

    {{ submenu }}

{% endif %}

It's important to render the submenu whether or not it has any displayable content to make sure it's cache settings are bubbled up.
Note: this will only work if whatever your rendering returns no html. If you're seeing some wrapping divs, etc return, but no content, you can alter your if statement like {% if submenu|striptags|trim %} to strip html and check for only content.
Comment: Hi sonfd!

I was wondering if it's possible to display a specifc level of the menu using your method? I have some sidebars that's just displaying the title of the submenu because in my 'mainmenu' block Menu Levels is "initial visibility level: 3" and "Number of levels to display": 2.

So pages that are in level-2 that don't have any level-3 links display nothing except the title.

My partner made a post too: https://drupal.stackexchange.com/questions/298415/display-level-3-submenu-on-twig

Hide block if no submenu in Twig


SQL Server 2008 - Restore database without restoring transaction log (LDF)

Question: I have a production database with Full recovery mode where the MDF and LDF files are approx 80GB each.
We have a test server that we restore this database to on a regular basis for dev / test purposes, etc.
At the moment the restore has happened, we set the recovery model to Simple and shrink the log file as the server is low on disk space.
Is there a way of restoring the database straight to Simple mode so that the large LDF file is never created?
Answer: Unfortunately, no.
The best you can do is move the file: not resize it and not change any db settings
Comment: Thought as much, but the move is a good idea! I can actually restore the log file to somewhere else shrink it and move it back.
Answer: You could always script the log file to be shrunk down to nothing out of hours, then take a backup, and then resize back. Whether that works depends on the load on the server during this operation.
One alternative is to use a third party tool such as Red Gate SQL Compress on your dev/test server - though adding more storage may be more standard:
http://www.red-gate.com/products/dba/sql-storage-compress/
Comment: This would fragment the production LDF = not a good idea...
Answer: One thing you could do is attach just the MDF by itself. that is, if you have a copy of the mdf somewhere detached already:
http://msdn.microsoft.com/en-us/library/ms174385.aspx
the sp_attach_single_file_db command can be used to attach the database from an mdf and creates an empty ldf for you. in order to get a copy of the mdf, you'd have to either restore the original and detach it (might defeat the purpose?) or take the original offline and copy the mdf. so, perhaps it might not fit your scenario, but it might help others in a similar situation.


Programmatically hide comment and view tabs

Question: Can you please help me to hide the tabs from the Find/Add/Edit content page?

Is there a way to programmatically hide them?
Answer: A more generic solution could be Tab Tamer.

Tab Tamer is an administration utility that provides easy re-ordering, hiding, and deactivation of tabs and subtabs. You can also rename tab labels.
Comment: @Mr.Black To programmatically remove tabs, you still need a module. Whatever is one created by you, or one created by others, it doesn't make any difference: It still is an extra module. The only difference would be when you have already a custom module; even in that case, putting all in a single module would be a bad idea.
Comment: @Mr.Black Well, you can just look at the module and see how it is doing it. I guess you should look for menu_alter. [Or read first result I got from Google](https://drupal.org/node/483324).
Comment: Thanks @Potney Switters.. Sorry I don't want to use any other module.. I need a solution to solve this in programmatic way
Comment: @Molot Thank you for your suggestion.. Anyway I'll be looking the code on the same. Thanks.
Answer: To hide the menu tabs you want to hide, you just need to create a module containing the following code.
<code>function mymodule_menu_alter(&$items) {
  $paths = array('admin/content/comment', 'node/%node/view');

  foreach ($paths as $path) {
    $items[$path]['type'] = MENU_CALLBACK;
  }
}
</code>
I would rather follow Potney Switters's suggestion, and install the Tab Tamer module, since:

It implements a user interface to change which tabs should be hidden; this is better than hard coding paths in the code
It doesn't force you to find which module implements which tab, and get the path from the code of that module
It doesn't require too much resources
It is code that you don't maintain, so you don't need to port it to Drupal 8 because somebody else will do it

The last point could also mean you need to wait to upgrade to Drupal 8 until the Tab Tamer module is ported to Drupal 8, but since you are probably using other third-party modules, that should not be a problem.
Of all the points I listed, I think the first two are the reason why it is worth using the Tab Tammer module. The only reason not to use that module would be using a less resource-consuming module, which would also mean using a module that uses less code, including the one used for the settings page; that would mean having hard-coded settings, with the consequences reported in the second point.


How to load a CSV string into MySQL using Python

Question: In my use case, I have a csv stored as a string and I want to load it into a MySQL table. Is there a better way than saving the string as a file, use LOAD DATA INFILE, and then deleting the file? I find this answer but it's for JDBC and I haven't find a Python equivalent to it.
Comment: Why not just put an insert stmt together ?
Answer: Yes what you describe is very possible! Say, for example, that your csv file has three columns:
<code>import MySQLdb
conn = MySQLdb.connect('your_connection_string')
cur = conn.cursor()
with open('yourfile.csv','rb') as fin:
    for row in fin:
        cur.execute('insert into yourtable (col1,col2,col3) values (%s,%s,%s)',row)
cur.close(); conn.close()
</code>
Comment: Will this be slow if I have many rows in my csv? I heard that LOAD DATA INFILE is more efficient when loading a large amount of data.


Why is an equivalent, more complex query 10 times faster

Question: I have a flat table that I want to organize. The table basically represents a tree structure:
Channel -> (n0) Partners -> (n1) CampaignGroups -> (n2) Campaigns -> ... (ni) Other levels
CREATE TABLE campaign_tree (
    channel_id int,
    channel_name text,
    partner_name text,
    campaign_group_name text,
    campaign_name text,
    ad_name text
);

In order to sanitize the data, make names case-insensitive, and lose redundant IDs, I first find the data that needs to be updated. So I have 2 approaches to this problem:
Approach 1
First get the structure of the tree on the upper levels, then lose the different IDs for the same names:
SELECT
    count(1),
    min(campaign_id) AS new_campaign_id,
    campaign_name,
    channel_name,
    partner_name,
    campaign_group_name
FROM
(SELECT DISTINCT
    campaign_id,
    upper(channel_name) AS channel_name,
    upper(partner_name) AS partner_name,
    upper(campaign_group_name) AS campaign_group_name,
    upper(campaign_name) AS campaign_name
FROM
    campaign_tree
) tmp
GROUP BY channel_name, partner_name, campaign_group_name, campaign_name
HAVING count(1)>1 --only need to get those that we need to sanitize

This query takes around 350ms to execute. The query plan is as follows:
HashAggregate  (cost=18008.63..18081.98 rows=5868 width=136) (actual time=391.868..404.130 rows=33 loops=1)
  Output: count(1), min(campaign_tree.campaign_id), (upper(campaign_tree.campaign_name)), (upper(campaign_tree.channel_name)), (upper(campaign_tree_campaign_code.partner_name)), (upper(campaign_tree.campaign_group_name))
  Group Key: (upper(campaign_tree.channel_name)), (upper(campaign_tree.partner_name)), (upper(campaign_tree.campaign_group_name)), (upper(campaign_tree.campaign_name))
  Filter: (count(1) > 1)
  Rows Removed by Filter: 64855
  ->  Unique  (cost=15324.20..16394.93 rows=58680 width=83) (actual time=282.253..338.041 rows=64998 loops=1)
        Output: campaign_tree_campaign_code.campaign_id, (upper(campaign_tree.channel_name)), (upper(campaign_tree.partner_name)), (upper(campaign_tree.campaign_group_name)), (upper(campaign_tree.campaign_name))
        ->  Sort  (cost=15324.20..15502.65 rows=71382 width=83) (actual time=282.251..305.340 rows=71382 loops=1)
              Output: campaign_tree_campaign_code.campaign_id, (upper(campaign_tree.channel_name)), (upper(campaign_tree.partner_name)), (upper(campaign_tree.campaign_group_name)), (upper(campaign_tree.campaign_name))
              Sort Key: campaign_tree.campaign_id, (upper(campaign_tree.channel_name)), (upper(campaign_tree.partner_name)), (upper(campaign_tree.campaign_group_name)), (upper(campaign_tree.campaign_name))
              Sort Method: external merge  Disk: 6608kB
              ->  Seq Scan on campaign_tree  (cost=0.00..6153.64 rows=71382 width=83) (actual time=0.015..146.611 rows=71382 loops=1)
                    Output: campaign_tree.campaign_id, upper(campaign_tree.channel_name), upper(campaign_tree.partner_name), upper(campaign_tree.campaign_group_name), upper(campaign_tree.campaign_name)
Planning time: 0.085 ms
Execution time: 407.383 ms

Approach 2
A direct approach: count the distinct ids of items with the same name. Also determine the minimum id of these distinct ids.
SELECT
    count(distinct campaign_id) AS cnt,
    min(campaign_id) AS new_campaign_id,
    upper(campaign_name) AS campaign_name,
    upper(channel_name) AS channel_name,
    upper(partner_name) AS partner_name,
    upper(campaign_group_name) AS campaign_group_name
FROM campaign_tree
GROUP BY upper(channel_name), upper(partner_name), upper(campaign_group_name), upper(campaign_name)
HAVING count(distinct campaign_id)>1

Results are the same, just in a different order. Execution time is around 4 seconds each time. Query plan is as follows:
GroupAggregate  (cost=15324.20..17912.57 rows=51588 width=83) (actual time=3723.908..4004.447 rows=33 loops=1)
  Output: count(DISTINCT campaign_id), min(campaign_id), (upper(campaign_name)), (upper(channel_name)), (upper(partner_name)), (upper(campaign_group_name))
  Group Key: (upper(campaign_tree.channel_name)), (upper(campaign_tree.partner_name)), (upper(campaign_tree.campaign_group_name)), (upper(campaign_tree.campaign_name))
  Filter: (count(DISTINCT campaign_tree.campaign_id) > 1)
  Rows Removed by Filter: 64855
  ->  Sort  (cost=15324.20..15502.65 rows=71382 width=83) (actual time=3718.016..3934.400 rows=71382 loops=1)
        Output: (upper(campaign_name)), (upper(channel_name)), (upper(partner_name)), (upper(campaign_group_name)), campaign_id
        Sort Key: (upper(campaign_tree.channel_name)), (upper(campaign_tree.partner_name)), (upper(campaign_tree.campaign_group_name)), (upper(campaign_tree.campaign_name))
        Sort Method: external merge  Disk: 6880kB
        ->  Seq Scan on campaign_tree (cost=0.00..6153.64 rows=71382 width=83) (actual time=0.014..150.634 rows=71382 loops=1)
              Output: upper(campaign_name), upper(channel_name), upper(partner_name), upper(campaign_group_name), campaign_id
Planning time: 0.066 ms
Execution time: 4006.323 ms

Approach 3
After some discussion, I decided to try and change the second approach, and refer to expressions instead of explicitly write them in the GROUP BY clause:
SELECT
    count(distinct campaign_id) AS cnt,
    min(campaign_id) AS new_campaign_id,
    upper(campaign_name) AS campaign_name,
    upper(channel_name) AS channel_name,
    upper(partner_name) AS partner_name,
   upper(campaign_group_name) AS campaign_group_name
FROM campaign_tree
GROUP BY 3, 4, 5, 6
HAVING count(distinct campaign_id)>1

Query Plan:
GroupAggregate  (cost=15324.20..17912.57 rows=51588 width=83) (actual time=1148.957..1316.564 rows=33 loops=1)
  Output: count(DISTINCT campaign_id), min(campaign_id), (upper(campaign_name)), (upper(channel_name)), (upper(partner_name)), (upper(campaign_group_name))
  Group Key: (upper(campaign_tree.campaign_name)), (upper(campaign_tree.channel_name)), (upper(campaign_tree.partner_name)), (upper(campaign_tree.campaign_group_name))
  Filter: (count(DISTINCT campaign_tree.campaign_id) > 1)
  Rows Removed by Filter: 64855
  ->  Sort  (cost=15324.20..15502.65 rows=71382 width=83) (actual time=1148.849..1240.184 rows=71382 loops=1)
        Output: (upper(campaign_name)), (upper(channel_name)), (upper(partner_name)), (upper(campaign_group_name)), campaign_id
        Sort Key: (upper(campaign_tree.campaign_name)), (upper(campaign_tree.channel_name)), (upper(campaign_tree.partner_name)), (upper(campaign_tree.campaign_group_name))
        Sort Method: external merge  Disk: 6880kB
        ->  Seq Scan on campaign_tree  (cost=0.00..6153.64 rows=71382 width=83) (actual time=0.014..148.835 rows=71382 loops=1)
              Output: upper(campaign_name), upper(channel_name), upper(partner_name), upper(campaign_group_name), campaign_id
Planning time: 0.067 ms
Execution time: 1318.397 ms

And no, there are no indexes created on this table. I know they will improve things. That's not the point of this question.
The question is: why is there such a big difference in execution time? The query plan doesn't shed any light for me.
Comment: Please show the output of `explain (analyze, verbose)` not plain analyze
Comment: The aggregation on the (three!) upper()s looks ugly to me. I'd try to first pre-aggregate without the upper(), and sum over its result afterwards. That will probably allow the aggregates to fit into hash tables.
Comment: You are grouping by an expression in the second case with the first ("most significant") column being one as well. In the first statement the first "sort" key is an integer - I guess that makes a difference. Might have to do with sorting optimization due to "abbreviated" keys (which might not be possible or as efficient with an expression involving strings - plus on some platforms this has been disabled due to a bug in collation handling)
Comment: Wouldn't Postgresql optimize it? Oh wait, it doesn't. I changed the `group by` clause like so: `GROUP BY 3, 4, 5, 6` and it now runs for 1.627 seconds. Still significantly slower than the first approach. Wow, I'm surprised at this. I'll update the question with the relevant info. Thanks for the suggestion.
Comment: the reason for upper() is to have a case-insensitive collation. It shouldn't be that costly.
Comment: Problem is that upper/lower are not sargable, so an index would not be helpful, so sorting will be needed. https://en.wikipedia.org/wiki/Sargable
Comment: in Postgresql indexes can be created on expressions, which are then used when those expressions are used.
http://stackoverflow.com/questions/7005302/postgresql-how-to-make-case-insensitive-query check out the correct answer, and the most popular comment to it.
Answer: Reading the plans it looks like they diverge when you do the unique vs the group by distinct campaign_id.
This suggests to me that the issue is that group by count(*) > 1 (same as what you are doing) is much cheaper than group by count(distinct campaign_id)
Which makes sense because you have already grouped in the former while in the second you have a secondary calculation you have to make on the grouped set on the second.
Comment: You misunderstood.  count(*) and count(1) are the same, performance-wise.  Sorry if I was not clear.
Comment: Not really the same. Changing `count(distinct campaign_id)` in both the `group by` clause and in the expression enumerator, to `count(*)` would just count all of the leafs from down this tree, and the result would be much different in this case.

And it's weird because in the first approach I am also doing selecting a `distinct` set.
Comment: My point was that having count(distinct ...) looks like it is extremely expensive and prevents certain optimizations.
Comment: Yes, I misunderstood. I'm just used to writing `count(1)`, that's all.
As for distinct - I do need that. And I get that in the first approach, at a very low cost. Not in the second or third (like the second approach, but with optimized grouping, that would seem superfluous, but is definitely not).
Comment: @ChrisTravers see my answer for some simulated data
Answer: Just a thought, but you might instead try:
having max(campaign_id) > min(campaign_id)

It ought to be easier for the execution to track the values of the min and max rather than the distinct number of ids.
Comment: Great idea! but unfortunately it doesn't do anything to reduce the execution time. And just to make the experiment run well, I removed the `count(distinct campaign_id)` entirely from the query. Still the same time to run, which means that there's nothing wrong in `count(distinct campaign_id)` in this situation (as long as there are other aggregate functions on campaign_id anyway)
Answer: [elaborating on my comment about pre-aggregation]
IMHO the cost is in the sorting, which is needed for the aggregation over a set of functions, which cannot be done any other way (anyway not in the current version).
The real solution would of course be to constrain the four domains (maybe even enumerate them, and/or squeeze them into separate tables)
untested, because I don't have the table definitions:

SELECT
    channel_name
    , partner_name
    , campaign_group_name
    , campaign_name
    , min(campaign_id) AS new_campaign_id
    , sum(the_count) AS the_count
FROM (SELECT DISTINCT
        upper(channel_name) AS channel_name
        , upper(partner_name) AS partner_name
        , upper(campaign_group_name) AS campaign_group_name
        , upper(campaign_name) AS campaign_name
        , MIN(campaign_id) AS campaign_id
        , sum(the_count) AS the_count
        FROM (SELECT DISTINCT
            channel_name AS channel_name
            , partner_name AS partner_name
            , campaign_group_name AS campaign_group_name
            , campaign_name AS campaign_name
            , MIN(campaign_id) AS campaign_id
            , COUNT(1) AS the_count
            FROM campaign_tree
            GROUP BY 1,2,3,4
            ) pre
    group BY 1,2,3,4
    ) agg
GROUP BY channel_name, partner_name, campaign_group_name, campaign_name
HAVING sum(the_count) > 1 --only need to get those that we need to sanitize
        ;

Comment: I had to modify this query. Replace distinct with group by for the innermost 2 queries, and I don't need to trickle up the count, since I needed a way to trim the tree to the top 4 levels (channel, partner, campaign group, campaign), and only THEN to count dupplicates (same campaign name, with case-insensitive match, but different IDs).

I ended up with something similar to my first approach, but with 3 levels - aggregation, transformation (upper), and then aggregation again. Even though it was more similar to my first approach, it was as slow as the second approach.
Comment: Yes, that is probably correct. It *could* have been without errors if you had added the table definition(s) to your question. BTW: why do you have four levels of hierarchy combined in one table? Looks like a lot of redundancy (in the upper levels), which has to be aggregated away afterwards ...
Comment: This query costs less but is slower. Tested with simulated data.
Comment: @wildplasser see my answer for some testing data.
Comment: I added a table definition. Not that it's any help since the point is in the data.
The reason it's flat is because it's imported as CSV, and it needs to be sanitized, and structured afterwards, which this question is really about.
Comment: @pietrop The table is ok, (thanks!), but the data is not ;-) The four text columns all stem from the same 21-word domain, and there are no upper<-->lowercase duplicates.
Answer: This is not a real answer but simply a help for others who would like to test on some simulated data. I hope it helps on understanding what's going on.
Here is the Python code to create a table named campaign_tree in your database and populate it with n=71382 rows of simulated data (I took this number from the plans):
import random

n = 71382
table_name = "campaign_tree"

set = ["You", "may", "say", "I''m", "a", "dreamer", "But", "I''m", "not", "the", "only", "one", "I", "hope", "someday", "you''ll", "join", "us", "And", "the", "world", "will", "be", "as", "one"]
lset = len(set) - 1

transaction = """
BEGIN;
CREATE TABLE """ + table_name + """
(
    campaign_id integer,
    campaign_name text,
    channel_name text,
    partner_name text,
    campaign_group_name text
);

INSERT INTO """ + table_name + """  (campaign_id, campaign_name, channel_name, partner_name, campaign_group_name)
VALUES """

values = []

i = 1
while i <= n:
    values = values + ["(" + \
                            `i` + ", '" + \
                            set[random.randint(1, lset)] + "', '" + \
                            set[random.randint(1, lset)] + "', '" + \
                            set[random.randint(1, lset)] + "', '" + \
                            set[random.randint(1, lset)] + "')"]
    i = i + 1

transaction = transaction + ",\n".join(values) + "; COMMIT;"

foutput = open("test.sql", "w")
foutput.write(transaction)
foutput.close()

Save it as test.py and then execute python test.py. It will generate a file named test.sql. Finally, execute psql -f test.sql and you are done. Happy testing :)


Cross PDO driver compatible GROUP_CONCAT?

Question: Is there an efficient alternative to GROUP_CONCAT that works in the major PDO drivers?
Answer: Given this MySQL:
select group_concat(c separator ',') from t

you could do this in PostgreSQL:
select array_to_string(array_agg(c), ',') from t

or this in SQLite:
select group_concat(c, ',') from t

I don't know about SQL Server though.
References:

MySQL group_concat
PostgreSQL array_agg
PostgreSQL array_to_string
SQLite group_concat
Comment: Brilliant! I can implement each variant on my database driver wrapper classes. It's for an open source project so I'll let somebody else implement mssql version if it's needed. I don't tend to use mssql much these days
Comment: @Lea: You might want to do a version check on the PostgreSQL version, the one I gave works in 9.0, let me know if you need to support 8 and I'll see what I can do.


Cookies for a webform using page breaks

Question: I'm using the Webform module in a Drupal 6 installation. This webform is appearing in an iFrame, and can be found on every page. The website that contains the iframe is static, the Drupal installation with the Webform module is the only thing being loaded in the iframe. This webform is a quiz with 20 questions. The rest of the site is kind of like a handbook, and I want my users to be able to use the static handbook website as a guide while taking the quiz. Each quiz question is on its own page; I am using webform's pagebreaks to split each question up.
Is there a way to set a cookie that takes the user back to the question they left off at, should they choose to leave the form and return?
Answer: You can find some well written answers on using cookies in Drupal here.
Comment: This answer is just a link to another question asked on this very site. To be an answer, you would write a summary in your own answer.
Comment: OK I will, I just thought those answers did a good job and I had nothing else I wanted to add for this question.

Cookies for a webform using page breaks