Por que criar um objeto usando a superclasse?

Question: Dado o seguinte código:
public class Musico {

    public void tocaInstrumento() {
        // faz algo
    }
}

.
public class Baterista extends Musico {

    public void giraBaqueta() {
        // faz algo
    }
}

.
public class Violonista extends Musico {

    public void trocaCordas() {
    }
}

Eu posso me valer do polimorfismo para fazer o seguinte:
    Musico musico1 = new Violonista();
    Musico musico2 = new Baterista();

    musico1 = musico2;

Porém, eu não consigo enxergar os métodos da subclasse:
    musico1.trocaCordas(); //ERRO DE COMPILAÇÃO!

Se eu utilizar:
Violonista musico1 = new Violonista();

Não seria melhor? Qual a vantagem em utilizar tipo Musico para referenciar um objeto de uma subclasse? Seria simplesmente a possibilidade de eu conseguir atribuir musico1 = musico2;, por exemplo? Ou há outras vantagens?
Answer: O seu exemplo não se trata exatamente de "criar" um objeto usando a superclasse, e sim de criar um objeto usando uma subclasse e manipulá-lo por meio da superclasse.
Para explicar de uma forma mais simples, vou partir do seguinte código:
Musico musico1 = new Violonista();
Musico musico2 = new Baterista();

O que você fez ali foi basicamente declarar duas variáveis do tipo Musico (as variáveis musico1 e musico2) e inicializá-las com conteúdo que são instâncias de classes filhas (herdadas) de Musico (respectivamente instâncias de Violonista e Baterista). Isso é justamente o caráter do polimorfismo (etimologia: poli [muitos] + morfos [forma] = qualidade ou estado do que pode assumir diferentes formas)
Também como já responderam apropriadamente, o polimorfismo é o que permite você adicionar instâncias de diferentes objetos em uma mesma lista (ArrayList<Musico>, por exemplo), uma vez que essa característica da orientação a objetos permite manipular os objetos de forma "abstrata" (isto é, sem conhecer detalhes específicos da implementação ou de qual instância realmente se encontra guardada na variável).
O erro de compilação decorre do fato de que o compilador não tem como saber qual classe você instanciou, pois o tipo da variável é Músico. Você pode, entretanto, tentar fazer um type casting:
((Violonista) musico1).trocaCordas();

Basicamente você está dizendo ao compilador para tratar o conteúdo variável musico1 como uma instância da classe Violonista. Se esse conteúdo realmente for uma instância dessa classe, tudo funcionará como esperado, mas se não for você terá um erro em tempo de execução. Pra garantir que não está fazendo bobagens, você pode verificar se a a instância é, de fato, herdada daquela classe:
if(musico1 instanceof Violonista)
    ((Violonista) musico1).trocaCordas();

Eu sei que provavelmente estou chovendo no molhado com todas essas explicações (porque você já teve respostas realmente muito boas), mas eu queria finalmente oferecer um exemplo em que há uma real vantagem em se utilizar esse tipo de manipulação por meio da classe pai/abstrata.
Considere, por exemplo, que você é o criador/desenvolvedor do Sistema Operacional Windows. É claro que você deseja que outros desenvolvedores criem aplicações para o seu SO. Imagine que eu sou o desenvolvedor do MS Word. De minha parte, eu desejo que o meu software seja capaz de imprimir texto na impressora do cliente. Mas, o problema é que o cliente pode ter diferentes tipos de impressoras, e novos tipos podem ser criados após o lançamento do meu software. Como proceder?
Suponha que você, desenvolvedor do SO, disponha de uma interface para impressoras, em que basicamente você criou e publicou uma classe abstrata (ou seja, um tipo) chamda Impressora. Tal classe tem apenas um método:
public abstract class Impressora {
    public void imprimir(String sTexto);
}

O "abstract" está ali apenas para dizer que essa classe não serve para ser instanciada, apenas para ser herdada. Enfim, agora suponha que o fabricante da impressora XPTO, sabendo da interface publicamente disponível feita por você, crie seu próprio driver de impressão, em que ele herda a sua class Impressora, por exemplo, da seguinte forma:
import IMPRESSORA.DO.SISTEMA.OPERACIONAL;

public class ImpressoraXPTO extends Impressora {
    public void imprimir(String sTexto) {
        . . .
        // Faz a impressão específica da impressora XPTO
        . . .
    }
}

Dai, eu, desenvolvedor do MS Word, preciso solicitar a impressão dos textos criados no meu software. Igualmente, sabendo que existe algo publicamente disponível criado por você eu simplesmente reutilizo o que o SO já prove, por exemplo de uma forma similar a isso:
import IMPRESSORA.DO.SISTEMA.OPERACIONAL;

public class MSWord {

    public void imprimirArquivo(String sTexto)
    {
        // Suponha que exista uma função do sistema operacional que
        // devolva a instância da impressora padrão
        // (sem importar se é uma XPTO, uma HP, uma Epson, uma George Foreman, etc).
        Impressora oImpressora = SO.getImpressoraPadrao();

        // Como todas têm o método `imprimir` (por herdar de `Impressora`), eu posso
        // chamá-lo sem me preocupar com os detalhes de como cada impressora
        // realmente trabalha!
        oImpressora.imprimir(sTexto);
    }
}

Nesse simples exemplo dá pra ter uma ideia da vantagem de se trabalhar com a abstração e o polimorfismo. Primeiramente, você pode desacoplar o uso da implementação; isto é, quem utiliza o seu código pode "trabalhar na abstração", significando que pode utilizar os métodos e propriedades básicas que todas as heranças terão. Em segundo lugar, facilita a extensão, pois é muito fácil e prático adicionar novas classes específicas (no exemplo, novas impressoras - contanto que herdem da interface padrão Impressora - serão garantidamente utilizávels no meu MS Word no seu sistema operacional sem que esse software precise ser alterado).
No caso do seu exemplo, a sua classe pai/abstrata é Musico e as classes de real interesse são as que herdam dela: Violonista e Baterista. A herança faz muito sentido quando as classes compartilham algo, sejam atributos (variáveis) ou comportamentos (métodos). No seu exemplo, a única coisa que um violonista e um baterista compartilham é o comportamento de "tocar instrumento" (por meio do método tocaInstrumento herdado).
Idealmente esse método precisa ser reimplementado nas classes filhas para que cada tipo de músico toque de forma apropriada (o violonista troca [vc não quis dizer "toca"?] as cordas, e o baterista gira a baqueta). Quando alguma outra parte do seu código manipular os "músicos" (isso mesmo, de forma abstrata), irá "solicitar" a eles que toquem o instrumento (isto é, invocarão o método tocaInstrumento), mas cada tipo de músico (cada objeto) irá fazê-lo de forma específica, conforme sua própria implementação. O fato é que quem manipula os "musicos" (alguém citou um palco em exemplos, mas poderia ser o simulador de orquestra) não precisa saber exatamente o que cada um faz.
Comment: Como citado em http://pt.stackoverflow.com/a/5337/4156 eu já conhecia as vantagens da herança e polimorfismo, a dúvida era realmente específica no que diz respeito em o porquê de utilizar a superclasse (no seu exemplo, uma interface) ao invés de usar direto a subclasse. Sua resposta é quase um artigo! Parabéns!
Comment: Agora, se você tem apenas um uso local de uma classe específica (mas que herda de outra para "ganhar" atributos e métodos já existentes), faz mais sentido mesmo você manipular a instância em sua própria classe. Quero dizer, ai a abstração e o polimorfismo não têm tanta relevância como a herança em si.
Comment: Excelente explicação, @LuizVieira, +1 =)
Comment: Porém, me faz surgir outra dúvida. Faz todo sentido utilizar, como no seu exemplo, `Impressora oImpressora = SO.getImpressoraPadrao()`, pois você não sabe qual impressora específica o método retornará, pode vir qualquer uma, mas eu sei que sempre será subclasse de `Impressora`. Porém, haveria razão para alguém deliberadamente escrever um código `Impressora oImpressora = new ImpressoraXPTO()` ?
Comment: Sim, há. No exemplo da impressora o próprio sistema operacional faria isso quando o usuário selecionasse XPTO como impressora padrão. No final das contas, *alguém tem que instanciar* o objeto específico. Aliás, facilitar esse processo é um dos principais motivadores de padrões de projeto como Abstract Factor (http://pt.wikipedia.org/wiki/Abstract_Factory) e Factory Method (http://pt.wikipedia.org/wiki/Factory_Method).
Comment: Encontrei esse link que pode ser útil para um melhor entendimento: http://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n
Answer: Acho que entendi sua pergunta, você quer uma razão para instanciar
Musico musico1 = new Violonista();

ao invés de
Violonista musico1 = new Violonista();

Qualquer método que aceitar um Musico vai aceitar seu objeto Violonista, tanto nos parâmetros como no retorno, e inclusive você pode sim adicionar seu Violonista a uma lista de Musico Acho que vemos mais esta construção por razões didáticas.
Acredito que você já sabia das características e  vantagens da herança e do polimorfismo, e que basicamente a única dúvida era usar o Musicoou o Violonista ao declarar sua variável. Estou correto?
Se for isso, não vejo razão para utilizar a primeira opção. Por favor me corrijam se eu estiver errado.
(Agora, criar um campo numa classe como Musico ao invés de Violonista  faria muita diferença)
Comment: Acho que você tem razão. A pergunta era sobre que caso seria necessário instanciar dessa forma e de fato não há vantagem. O cast pode ser feito a qualquer momento e um método que recebe Musico como parâmetro vai aceitar qualquer filho.
Answer: As vantagens de usar abstração são as mesmas do polimorfismo, que do meu ponto de vista a principal consiste na criação de rotinas genéricas que não precisam saber dos tipos específicos. Isso permite a você criar novas variações dos tipos de dados sem alterar os algoritmos existentes.
Talvez você esteja com dificuldades porque você está pensando nos detalhes das classes e não numa rotina que pode abstrair os conceitos.
Imagine que uma CasaDeShow vai receber uma banda composta por vários músicos. Essa casa somente precisa pedir para eles tocarem os instrumentos, ela não quer saber os detalhes de cada um deles. Aqui o uso da abstração cai perfeitamente.
Por outro lado, se tivéssemos uma oficina de conserto de instrumentos, aí sim deveria haver uma seção específica para cada tipo de músico e instrumento, com tratamento individual para as particularidades de cada um. Isso lembra bastante a sobrecarga de métodos.
Enfim, a maioria dos conceitos relacionados a Orientação à Objetos possui alguma relação com a realidade, mas às vezes temos de nos esforçar para pensar de forma abstrata.
Comment: entendi a questão de poder criar rotinas genéricas que não precisem saber dos tipos específicos. Mas eu poderia perfeitamente passar um argumento do tipo Baterista para um método que só recebe tipo Musico, pois Baterista **é** um Musico.
Comment: @dellasavia Isso mesmo. A questão não é que você *sempre* deve trabalhar com tipos abstratos, pois também não vejo vantagem nisso. Mas quanto mais você conseguir trabalhar com as abstrações, menos código repetido você vai ter.
Answer: Calma, vamos por partes.
Por que dá erro de compilação?
Em primeiro lugar, você não consegue acessar trocaCordas, porque o musico1 passa a ser um baterista quando você coloca musico1 = musico2.
Quais as vantagens da herança?
Segundo, existem muitas vantagens em se utilizar uma superclasse. A primeira é que todas as classes filhas vão herdar os métodos public e protected do pai. No caso de exemplo que você utilizou, tanto Baterista, quanto Violinista possuem o método tocaInstrumento. Isso significa que, quando você quiser editar o método tocaInstrumento, você NÃO precisa editar ele várias vezes, mas apenas uma, de forma que o Violinista e o Baterista receberão as alterações. Essa e outras propriedades podem ser vistas nessa aula sobre heranças.
Qual a vantagem de utilizar a classe pai para definir objetos?
Essa pergunta parte mais de quando você precisa de algo genérico, vamos imaginar que você queira salvar uma banda. Sabemos que a banda não é composta só por bateristas ou violinistas, mas sim por músicos, então nós teríamos que minha banda é uma lista de Musico.
ArrayList<Musico> banda = new ArrayList<Musico>();

e dentro dessa lista eu terei cada tipo de músico:
banda.add(new Violinista());
banda.add(new Baterista());

a vantagem disso é que, quando eu precisar realizar uma ação de Musico eu posso iterar somente em um for:
for(int i = 0; i < banda.size(); i++){
  banda.get(i).tocaInstrumento();
}

ou
for(Musico musico: banda){
  musico.tocaInstrumento();
}

Isso foi só uma introdução das vantagens de uma característica básica de Orientação a Objetos, mais informações podem ser encontradas aqui.
Comment: eu entendi, mas veja: Se eu criar o objeto Violonista musico1 = new Violonista(); eu também vou conseguir inserir musico1 numa lista que só aceite objetivos do tipo Musico, pois musico1 **é** um Violonista e Violonista **é** um Musico. Ainda não vejo vantagem alguma de usar o supertipo para se criar a referência.
Comment: Sim, mas se você criar um `ArrayList`, na sua lista banda, só terão violinistas, enquanto um `ArrayList` permite **qualquer** tipo de músico. Isso é usado quando você **não sabe** qual será o filho que será referenciado. Um exemplo é o `ArrayList`, outro exemplo poderia ser, por exemplo, se um usuário for escolher que tipo de músico ele é, você não saberá, diante mão, o que ele escolherá, logo você não poderá referenciar um tipo de músico específico. Ficou mais claro o que eu quero dizer?
Answer: Há outras vantagens. A diferença é implícita e, se você olhar bem, é quase óbvia: Um Musico pode ser um Baterista ou um Violonista, mas um Baterista não pode ser um Violonista e vice versa.
Porque isso seria útil? Quando você não precisa de um Baterista ou Violinista em específico, você precisa de qualquer Musico.
Vamos supor isso com um exemplo: uma Banda. Como poderíamos expressar isso? Bem, pode ser como uma classe:
class Banda {
  private Vocalista vocalista;
  private Guitarrista guitarrista;
  private Baterista baterista;
  private Violonista violonista;

  // encapsulamento, construtores e afins...
}

Ok, isso funciona. Mas temos um problema: E se a banda tem dois guitarristas? Ou se ela não tem um violonista?
Podemos resolver isso assim:
class Banda {
  private Vector<Musico> musicos;

  // encapsulamento, construtores e afins

  public void tocarMusica() {
    for (Musico musico : musicos) musico.tocaInstrumento();
  }
}

Desse modo, não importa se uma banda tem mais ou menos músicos de cada tipo, pois ela aceita qualquer número de músicos, e todos podem tocar seu instrumento.
Comment: mesmo que eu crie o objeto Violonista musico1 = new Violonista(); eu conseguirei perfeitamente inserí-lo nesse vetor. Então, qual a vantagem de usar o supertipo para se criar a referência?
Comment: Olha no `for`. Você está criando uma lista de `Musico`s, e não de `Violonista`s. Essa é a vantagem: Poder ser tanto um como outro.
Comment: eu entendi, mas a minha dúvida é na forma de criação do objeto e não no acesso a ele. Tanto faz eu criar `Musico musico1 = new Violonista()` ou `Violonista musico1 = new Violonista()`. Em ambas as formas, eu conseguirei mandar `musico1` para um `for` que só aceite Musico.
Comment: Sim. A única diferença é no acesso. Não vejo uma vantagem na criação.
Answer: Sim há outras vantagens para utilizar polimorfismo em seu código. Ou, nas suas palavras, referenciar objetos por sua superclasse.
Duas, por exemplo:
Substituir ifs por polimorfismo
Vamos supor que, no seu exemplo de músicos, você queira criar uma página Web para exibir os detalhes de cada um deles. Caso o músico seja um baterista, você deseja exibir uma imagem (avatar) de uma bateria. Caso seja um guitarrista, uma guitarra; violinista, violino, e assim sucessivamente.
Então, em uma primeira versão, você escreve:
public void exibirAvatar(PaginaWeb pagina, Musico musico) {
  String imagem;
  if (musico instanceof Baterista) {
    imagem = "/img/bateria.jpg";
  } else if (musico instanceof Guitarrista) {
    imagem = "/img/guitarra.jpg";
  } else if (musico instanceof Violinista) {
    imagem = "/img/violino.jpg";
  } else {
    imagem = "/img/imagem-padrao.jpg";
  }
  pagina.exibirAvatar(imagem);
}

Acho que todos já escrevemos algum tipo de código parecido com este.
O problema é que, por vezes, esquecemos que código não é estático, isto é, esquecemos que:

Código vai precisar de manutenção;
Você poderá ter que fazer a manutenção do seu próprio código;
Outra pessoa poderá ter que fazer a manutenção do código que você escreveu; e
Você poderá ter que fazer a manutenção de um código escrito por outra pessoa.

De maneira mais prática: e se mais para frente, for necessário adicionar Baixistas, vocalistas, saxofonistas, etc? Neste caso, inclusive, seria melhor ter pensado em outra modelagem para as classes, mas isto não vem ao caso agora.
Aí, em uma segunda versão, você altera seu código inicial. Mas, na pressa, e utilizando CTRL-C, CTRL-V (uso todos os dias, que fique claro):
public void exibirAvatar(PaginaWeb pagina, Musico musico) {
  String imagem;
  if (musico instanceof Baterista) {
    imagem = "/img/bateria.jpg";
  } else if (musico instanceof Guitarrista) {
    imagem = "/img/guitarra.jpg";
  } else if (musico instanceof Violinista) {
    imagem = "/img/violino.jpg";
  } else if (musico instanceof Baixista) {
    imagem = "/img/guitarra.jpg";
  } else if (musico instanceof Saxofonista) {
    imagem = "/img/saxofone.jpg";
  } else {
    imagem = "/img/imagem-padrao.jpg";
  }
  pagina.exibirAvatar(imagem);
}

Baixista já está com a imagem incorreta... Uma solução alternativa, utilizando polimorfismo, é 'mover' os ifs para cada uma das subclasses:
public void exibirAvatar(PaginaWeb pagina, Musico musico) {
  String imagem = musico.getImagem();
  pagina.exibirAvatar(imagem);
}

public class Baterista extends Musico {
  public String getImagem() {
    return "/img/bateria.jpg";
  }
}

public class Guitarrista extends Musico {
  public String getImagem() {
    return "/img/guitarra.jpg";
  }
}

// etc

Substituindo, assim, uma série de ifs encadeados por polimorfismo.
Utilizar implementações falsas/alternativas em testes
Este é um exemplo que costumo dar a quem trabalha comigo. Suponha que você esteja desenvolvendo um sistema para enviar mensagens SMS quando algum evento ocorra. Por exemplo, quando um servidor na sua rede cai, você quer notificar todos os sysadmins.
Para facilitar o exemplo, vamos supor que a sua operadora de telefonia provê uma biblioteca em que, para enviar mensagens SMS, basta saber o número do celular e o conteúdo da mensagem.
Em uma primeira versão, você escreve:
public interface Notificador {
  void notificar(PossuiCelular pessoa, String mensagem);
}

public class NotificadorProducao implements Notificador {

  private final OperadoraTelefonia operadora; // biblioteca da sua operadora

  public NotificadorSysadmin(OperadoraTelefonia operadora) {
    this.operadora = operadora;
  }

  public void notificar(PossuiCelular pessoa, String mensagem) {
    String celular = pessoar.getCelular();
    operadora.enviarSms(celular, mensagem);
  }

}

e aí, onde periodicamente verifica-se efetivamente o status dos servidores:
public class VerificadorStatusServidor {

  private final Sysadmins sysadmins;
  private final Servidores servidores;
  private final Notificador notificador;

  public NotificadorSysadmin(Sysadmins sysadmins,
                             Servidores servidores,
                             Notificador notificador) {
    this.sysadmins = sysadmins;
    this.servidores = servidores;
    this.notificador = notificador;
  }

  public void check() {
    for (Servidor servidor : servidores.getTodos()) {
      if (servidor.naoResponde()) {
        notificarSysadmins(servidor);
      }
    }
  }

  private void notificarSysadmins(Servidor servidor) {
    for (Sysadmin admin : sysadmins.getTodos()) {
      notificador.notificar(admin, "Servidor " + servidor.getNome() + " não responde");
    }
  }

}

Como boa prática, você escreveu um teste para isto (e utilizou TDD inclusive).
public void sysadmin_deve_ser_notificado() {
  Sysadmins admins = new Sysadmins();
  admins.add(new Sysadmin("A", "555-0000"));
  admins.add(new Sysadmin("B", "555-1111"));
  admins.add(new Sysadmin("C", "555-2222"));

  Servidores servidores = new Servidores();
  servidores.add(new Servidor("1", "online"));
  servidores.add(new Servidor("2", "offline"));
  servidores.add(new Servidor("3", "offline"));

  Notificador notificador = new NotificadorSysadmin(new OperadoraTelefonia());

  VerificadorStatusServidor verificador = new VerificadorStatusServidor(admins, servidores, notificador);

  verificador.check();

  // assertivas
}

Todos os testes passam e todos ficam felizes.
A única questão é que, um mês depois, o gerente de seu projeto pergunta:
"Nossa conta de telefonia teve um aumento de 1200%!!!!!!!!!!! Quem ligou trocentas vezes para os sysadmins?"
E a verdade é que ninguém ligou, apenas o servidor de integração que rodou os testes a cada commit. E cada vez que o teste acima rodou, enviou um SMS real, com custo real de telefonia para os sysadmins.
A solução, neste caso, é utilizar implementações falsas. Substituir o Notificador real por um para ambiente de testes. Ou seja, no código do teste acima, substituir
Notificador notificador = new NotificadorSysadmin(new OperadoraTelefonia());

por
Notificador notificador = new NotificadorFalso();

Sendo que NotificadorFalso é:
public class NotificadorFalso implements Notificador {

  private List<Notificacao> notificacoes = new ArrayList<Notificacao>();

  public void notificar(PossuiCelular pessoa, String mensagem) {
    Notificacao n = new Notificacao(pessoa, mensagem);
    notificacoes.add(n);
  }

}

Assim, acessando a lista de notificacoes do seu NotificadorFalso, você ainda é capaz de verificar se o seu VerificadorStatusServidor funciona corretamente, sem ter os custos reais de envio de SMS.
Inclusive é possível simular casos em que a comunicação SMS falha, por exemplo:
public class NotificadorComErroSMS implements Notificador {
  public void notificar(PossuiCelular pessoa, String mensagem) {
    throw new ExcecaoDeComunicacaoSms();
  }
}

Por fim, notar que é o tipo de coisa que fazemos várias vezes ao dia e, por vezes, não notamos:
 Set<String> nomesDistintos = new HashSet<String>();
 // oops, quero percorrer com nomes ordenados
 Set<String> nomesDistintos = new TreeSet<String>();
 // oops, quero percorrer na ordem que foram inseridos
 Set<String> nomesDistintos = new LinkedHashSet<String>();

Comment: Eu sou adepto das explicações seguidas de exemplos reais. Acho que é uma excelente abordagem didática e facilita muito o entendimento. Obrigado!
Answer: A vantagem, no seu caso, da utilização do tipo Musico é a possibilidade de usar métodos que tratam das características que Violinista e Baterista possuem em comum.
Por outro lado, a não utilização implicaria na duplicidade de código para tratar particularmente Violinista e Baterista.
Você não consegue usar os métodos de Violinista em uma variável do tipo Musico porque neste momento, a variável do tipo músico pode ser tanto Baterista, Flautista, etc..., então não dá para chamar, por exemplo, Musico.trocaCordas() sendo que este poderia ser do tipo Baterista.
Digamos portanto que, se Musico possuir o método AbrirPartitura, este método poderá ser chamado diretamente, tanto em Violisita.AbrirPartirura(), Baterista.AbrirPartitura(), Flautista.AbrirPartitura() e Musico.AbrirPartitura (sendo que músico pode ser qualquer classe que herda de Musico).
Answer: Em Design de Software devemos sempre previlegiar as abstrações em detrimento das classes concretas para diminuir o Acoplamento e a dependência entre os diversos colaboradores do sistema. Ou seja devemos procurar promover Acoplamento Fraco entre os objetos que colaboram para executar um serviço ou funcionalidade.
O Acoplamento forte ocorre quando uma classe dependente contém uma referencia direta para uma classe concreta, que fornece o comportamento desejado. A dependência não pode ser substituída, sem a necessidade de uma mudança na classe dependente.
O fraco acoplamento ocorre quando a classe dependente contém referencia para uma interface, que pode, ser implementada por uma ou várias classes concretas.
A Interface determina um "contrato" especifico com uma lista definida de métodos e / ou atributos que as classes concretas devem implementar.
Qualquer classe que implementa a interface pode satisfazer a dependência e uma alteração em sua implementação não vai gerar propagação de alteração nas outras classes concretas, ou seja, esta abordagem promove a extensibilidade no design do software, a um custo muito baixo.
Quanto mais fazemos referencia a classes concretas no nosso código, mais Aumentamos o Acoplamento e perdemos a capacidade de fazer manutenção a baixo custo. Por este motivo Acoplamento Alto diminui a qualidade do software e deve ser evitado.
Veja imagem ilustrativa de baixo acoplamento.


Tcpdump on ec2 not seeing all packets

Question: I'm running tcpdump on an Amazon EC2 instance to monitor HTTP traffic going to Nginx  (this is just a test box, the only resource is the example test page).
Running tcpdump with the command
# tcpdump -vn -i any port 80

shows the packets from a browser's request to the site, but shows nothing when accessing the page using a Python script (using the Requests library) or with manually crafted packets (Scapy).

There's no local firewall running and the Security groups are properly set up.
The scripts work properly: I can capture the transactions in tcpdump on the local end easily enough, and they return the page with status 200 OK.
Saving the packets directly to a file (-w) also makes no difference, ruling out buffering issues (I believe?)
I've also tried seeing if VLANs are causing the issue, but no luck; grepping for "80" still gives no results.

Questions:

What could be causing tcpdump to miss these quite specific packets that are definitely getting through the firewall to Nginx and back out again?
Why are packets from Firefox being seen whilst packets sent from the scripts slip by?

Thank you
Answer: Try using a specific interface instead of "any". That 'device' can't be used in promiscuous mode. See the tcpdump manpage: http://www.tcpdump.org/manpages/tcpdump.1.html
Comment: Doesn't seem to be it unfortunately; and I've no idea why that could capture only a subset of packets, rather than all or none?
Answer: A technology that was released after this question was asked is VPC Flow Logs. This lets you look at network traffic in your VPC, filtered as you like.
VPC Flow Logs won't show you full packet contents, just basics like source, destination, port, protocol, size, time, and action like accept or reject, and a few others. It also won't show you traffic that originates and terminates inside an EC2 instance.
This isn't a direct answer to the question, as it doesn't solve the problem. It may however provide an alternate way to gather information to solve a similar problem.
Answer: Perhaps the snaplength?

-s snaplen
--snapshot-length=snaplen

Snarf  snaplen  bytes  of  data from each packet rather than the default of 262144 bytes.  Packets truncated
  because of a limited snapshot are indicated in the output with
  ``[|proto]'', where proto is the name of the protocol level at which
  the truncation has occurred.  Note that taking larger snapshots both
  increases the amount of  time  it takes  to  process packets and,
  effectively, decreases the amount of packet buffering.  This may cause
  packets to be lost.  You should limit snaplen to the smallest number
  that will capture the protocol information you're interested in.
  Setting snaplen to 0 sets it to the default of 262144, for backwards
  compatibility with recent older versions of tcpdump.

I usually use 65535.

Tcpdump on ec2 not seeing all packets


Using modules with almost similar functionality together on production site

Question: I have 2 modules with similar/alternative available and I have to use both. Those are Entity Reference and Search API.
Now the situation is that there are few modules which require References and Search modules as a dependency. It is a need and I have to use it.
Can I use all these modules at the same time on a production site?
Is there any possibility of clash or site crash?
Comment: Did you try installing those modules on a test site, and see what happens? Apart that, I don't see any reason for installing the _Entity Reference_ and the _References_ modules at the same time, since their purpose is similar. Choose the one you need basing on your need, and other modules you need. If you need a module that depends from the _References_ module, you would not install the _Entity Reference_ module.
Comment: That is why changes are never done directly on a production site, but first tested on a test/staging site.
Comment: Yes, but not much. I guess it is risky because as clive said, it might cause problem some where sometime.
Answer: Yes, there's always a possibility that modules will be incompatible with other modules that do the same thing.
There's also the possibility that modules whose functionality is completely unrelated will be incompatible with one another. Such is the nature of a modularly extensible system.
The only way you'll know is if:

The documentation for one or more modules list their known issues with other modules, or
You thoroughly test the two modules while both installed on a single site


Bulk of HTML files to node Migration

Question: I am developing a module for migrating data from html file to drupal node.
Each Html file is having 500+ logs. I want to create node for each log record.
I have started development of module as it is explained in DRPAL DOCUMENTATION i.e.: https://www.drupal.org/node/2029713/
But I am not getting exact solution for resolving it. Getting this error:

An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging
  information follows. Path: /batch?id=40&op=do StatusText: Service
  unavailable (with message) ResponseText: PDOException:
  SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer
  value: '/Chat Log - 07:01:2014.html' for column 'sourceid1' at row 1:
  INSERT INTO {migrate_map_alertnodemigrate} (sourceid1, needs_update,
  rollback_action) VALUES (:db_insert_placeholder_0,
  :db_insert_placeholder_1, :db_insert_placeholder_2); Array (
  [:db_insert_placeholder_0] => /Chat Log - 07:01:2014.html
  [:db_insert_placeholder_1] => 3 [:db_insert_placeholder_2] => 0 ) in
  MigrateSQLMap->saveIDMapping() (line 337 of
  /data/disk/o1/static/chartz_2/sites/all/modules/contrib/migrate/plugins/sources/sqlmap.inc).

Here is my code for this:
class AlertNodeMigration extends Migration    {
  public function __construct($arguments)
  {
    parent::__construct();

// SOURCE OBJECT
$directories = array('/data/disk/o1/static/chat_log/html');
$base_dir = '/data/disk/o1/static/chat_log/html';
$regex = '/(.*\.htm$|.*\.html$)/i';
$list_files = new MigrateListFiles($directories, $base_dir, $regex);
$item_file = new MigrateItemFile($base_dir);
$fields = array(
'title' => t('Node: Title'),
'uid' => t('Node: User Id'),
'created' => t('Node: Created Timestamp'),
'changed' => t('Node: Modified timestamp'),
'status' => t('Node: Published'),
'promote' => t('Node: Promoted to front page'),
'sticky' => t('Node: Sticky at top of lists'),
'revision' => t('Node: Create new revision'),
'log' => t('Node: Revision Log message'),
'language' => t('Node: Language (fr, en, ...)'),
'tnid' => t('Node: The translation set id for this node'),
'translate' => t('Node: A boolean indicating whether this translation page needs to be updated'),
'revision_uid' => t('Node: Modified (uid)'),
'is_new' => t('Option: Indicates a new node with the specified nid should be created'),
'field_instrument' => t('Field: Instrument (entityreference)'),
'field_alert' => t('Field: Alert (text_long)'),
'field_alert:language' => t('Subfield: Language for the field'),
'field_sentiment_analysis' => t('Field: Sentiment Analysis (list_text)'),
'field_tags' => t('Field: Tags (taxonomy_term_reference)'),
'field_tags:source_type' => t('Option: Set to \'tid\' when the value is a source ID'),
'field_tags:create_term' => t('Option: Set to TRUE to create referenced terms when necessary'),
'field_tags:ignore_case' => t('Option: Set to TRUE to ignore case differences between source data and existing term names'),
'field_source' => t('Field: Source (list_text)'),
'path' => t('Node: Path alias'),
'comment' => t('Whether comments may be posted to the node')
);
$this->team = array(new MigrateTeamMember('alertnode', 'firstname.lastname@example.com', t('Implementor')));
    $this->description = t('Alert Node Migration');

$this->source = new MigrateSourceList($list_files, $item_file, $fields);

// DESTINATION Object
$this->destination = new MigrateDestinationNode('alert');

// MAP OBJECT
    $this->map = new MigrateSQLMap($this->machineName,
    array(
      'sourceid' => array('type' => 'int',
                      'unsigned' => TRUE,
                      'not null' => TRUE,
         )
    ),
    MigrateDestinationNode::getKeySchema()
  );

// FIELD MAPPING OBJECT
$this->addFieldMapping('title', 'title');
$this->addFieldMapping('uid', 'uid');
$this->addFieldMapping('created', 'created');
$this->addFieldMapping('changed', 'changed');
$this->addFieldMapping('status', 'status');
$this->addFieldMapping('promote', 'promote');
$this->addFieldMapping('sticky', 'sticky');
$this->addFieldMapping('revision', 'revision');
$this->addFieldMapping('log', 'log');
$this->addFieldMapping('language', 'language');
$this->addFieldMapping('tnid', 'tnid');
$this->addFieldMapping('translate', 'translate');
$this->addFieldMapping('revision_uid', 'revision_uid');
$this->addFieldMapping('is_new', 'is_new');
$this->addFieldMapping('field_instrument', 'field_instrument');
$this->addFieldMapping('field_alert', 'field_alert');
$this->addFieldMapping('field_alert:language', 'field_alert:language');
$this->addFieldMapping('field_sentiment_analysis', 'field_sentiment_analysis');
$this->addFieldMapping('field_tags', 'field_tags');
$this->addFieldMapping('field_tags:source_type', 'field_tags:source_type');
$this->addFieldMapping('field_tags:create_term', 'field_tags:create_term');
$this->addFieldMapping('field_tags:ignore_case', 'field_tags:ignore_case');
$this->addFieldMapping('field_source', 'field_source');
$this->addFieldMapping('path', 'path');
$this->addFieldMapping('comment', 'comment');

}

public function prepareRow($row)
 {
     return true;
 }

 public function prepare($node, stdClass $row) {

// Set to admin for now.
$node->uid = 1;

// Create a new SourceParser to handle HTML content.
$source_parser = new SourceParser(substr($row->sourceid, 1), $row->filedata);
$alerthtml = $source_parser->parse_alert_html($row->html);

   $node->title = $alerthtml['title'];      // Node: Title
   $node->uid = '1';                //Node: User Id
   $node->created = $alerthtml['timestamp'];    //Node: Created Timestamp
   $node->changed = '0';            //Node: Modified timestamp
   $node->status = 'published';         //Node: Published
   $node->promote = '0';            //Node: Promoted to front page
   $node->sticky = '0';             //Node: Sticky at top of lists
   $node->revision = '0';           //Node: Create new revision
   $row->log = '0';             //Node: Revision Log message
   $node->language = 'en';          //Node: Language (fr, en, ...)
   $node->tnid = '0';               //Node: The translation set id for this node
   $node->translate = '0';          //Node: A boolean indicating whether this translation page
   $node->revision_uid = '1';           //Node: Modified (uid)
   $node->is_new = '1';             //Option: Indicates a new node with the specified nid
   $node->field_instrument = $alerthtml['inst'];//Field: Instrument (entityreference)
   $node->field_alert = $alerthtml['alert_text'];//Field: Alert (text_long)
   $node->field_alert['language'] = 'en';   //Subfield: Language for the field
   $node->field_sentiment_analysis = 'positive';//Field: Sentiment Analysis (list_text)
   $node->field_tags = '0';         //Field: Tags (taxonomy_term_reference)
   $node->field_tags['source_type'] = $alerthtml['src_typ'];//Option: Set to \'tid\' when the value is a source ID
   $node->field_tags['create_term'] = $alerthtml['term'];//Option: Set to TRUE to create referenced terms
   $node->field_tags['ignore_case'] = $alerthtml['ignr'];//Option: Set to TRUE to ignore case differences
   $node->field_source = $alerthtml['src']; //Field: Source (list_text)
   $node->path = $alerthtml['title'];       //Node: Path alias
   $node->comment = $alerthtml['comments']; //Whether comments may be posted to the node
}

}

Is anyone can help me ?
Thanks in advance.
Answer: The exception is telling you what the problem is:
Invalid datetime format: 1366 Incorrect integer value: '/Chat Log - 07:01:2014.html' for column 'sourceid1' at row 1

What this means is that the value that you're trying to assign to a datetime field is incorrect, and it gives you the illegal value that was encountered.
I suspect it's in this code:
<?php
  $node->created = $alerthtml['timestamp'];    //Node: Created Timestamp**strong text**
?>

The value of $alerthtml['timestamp'] appears to be "/Chat Log - 07:01:2014.html", not quite what you expected.  You'll want to do some validation to make sure that when you parse out that date value, you get what you expect and clean up as necessary.
Comment: yup it's probably looking for an mysql `timestamp` compatible value. This is an example of where you need to cleanup (or convert) your input data before passing them along to be used/saved as Node values.

Bulk of HTML files to node Migration


Stuck between ANOVA (with transformation) and multinomial logistic regression

Question: I need help in determining the appropriate stat test to use. I want to know if the 5 treatments (including the control variable) has significant differences based on their Antioxidant Activity as Gallic Acid (%w/w). Here is the data:
Comment: Why logistic regression? Your dependent variable seems to be continuous.
Comment: Well, you can disregard that thought.I'm just trying to find out what appropriate test that needs to be used
Comment: But, based on your comment, you are considering only one test then!  There seems to be no question left to answer.
Answer: A regression with just categorical independent variables is basically just an ANOVA, because both underlies a t-distribution for calculating the significance ( in case of regression of the coefficients). In both cases, residuals should be (approximately) be normally distributed. So both ways are fine.


How to play two voices with a large range in the same hand?

Question: My copy of Leonard Cohen's Hallelujah includes the following measures:

I don't know how to play the left hand. My problem is that I cannot reach from F2 to A3 (in the first bar, others correspondingly). My right hand is busy and far away, so I can't use that either.
The answer to similar questions regarding large chords is usually to arpeggiate them, but I'm not sure whether this applies (and how) in my situation where the wide range is in the melody rather than in the harmony.
Currently I'm playing it as if the F2 was only a dotted quarter, with the fingering 5, 2, 1, 2, 1, 2.
Answer: This is a case where use of the pedal is needed. You use the pedal to hold the bass note and you change it when the bass is changing. In this excerpt at the beginning of every measure.
Answer: Chopin and Liszt had different philosophies about writing heard/intended vs. held note values. Liszt had no compunctions about writing, say, left hand chords that cannot be held their entire value with the fingers (required elsewhere) and which therefore need the sostenuto pedal to "sound" their full indicated value (even when no pedal is explicitly indicated). Chopin, on the other hand, was fairly scrupulous in writing note/chord values that could be sustained with the fingers depressing the keys; otherwise, he would write a shorter "practical/playable" note value and use pedal markings to approximate the intended duration/effect.

Chopin Valse Op. 69 n.1. The left hand downbeats change in duration from three beats to one when they can't be held, but the pedaling remains the same! (By writing these as dotted half-notes, Chopin clearly wants the player to bring out these notes as their own descending, legato voice.)

Chopin Barcarolle. Note the very practical left-hand rest after the staccato quarter-note octave, giving the pianist time to move the hand up. The value intended/heard is determined by the end-pedal mark.

Chopin Prelude n.17

This example is similar to the Valse above. No doubt Chopin  would have superimposed a dotted quarter-note on the lowest eighth-note in each measure but for the impossible reach, in parallel with the right hand, which does, since it can be held there (and is, in fact, tied over). The listener is none the wiser, given the pedaling, but this nuanced writing shows that pedal indications were a separate consideration for Chopin. Some might argue that he is being too picky, but, from the viewpoint of piano technique, it helps to know what the composer thought is possible to play (and hold) and what, instead, needs the pedal to accomplish.

Liszt Lyon. Liszt writes pedal markings, but conceptually, at least, the left hand values continue the style of the preceding measure (no rests in the bass in either measure), so he writes a double-dotted half note, which, of course, can sound its full value only with the aid of the pedal. The pianist has to decide how long the double-dotted notes can be practically held.

Liszt. Hungarian Rhapsody n.2

The left-hand quarter notes are the intention, but their length has to be implied through the strongest emphasis/accent and pedaling, since they cannot be held their full value. In the second measure shown (and four measures later as well), the sf quarter-note octaves should be dotted, as corroborated by the lack of an eighth rest in this voice were it a true quarter note. This was probably a printing error in the first edition and was corrected in the Bärenreiter urtext edition, which has a dotted quarter-note.

I'm not saying Chopin adheres to this writing style 100% of the time, but I think you'll find he usually does write note values that can actually be held with the fingers. To sum up, you might say Liszt adopts a quasi orchestral approach--the piano score is almost an orchestral reduction; Chopin is ever the pianist, writing note values that can  actually be played (held), overlaid with pedal markings. Liszt, of course, was a flamboyant composer, and  often wrote multiple voices whose clarity would be lost if he were forced to write note values based strictly on their playability: he is creating a virtual orchestra, even if all the parts are played by a one-man-band.
Comment: So would it be fair to say that (in general) Chopin wrote what he wanted you to _play_, while Liszt wrote what he wanted you to _hear_?
Comment: @gidds Generally speaking, yes, if we use "play" to mean "strike and hold." When we take into account the pedal, both approaches put on paper what we hear. Keep in mind that the pedal is extending the values of all notes played during its duration.
Answer: Note that Leonard Cohen basically is a guitar player, and for guitar, this kind of per-measure bass note with a treble figure spanning a large interval all-in-all is pretty much par for the course.  The piano transcription here captures the musical spirit if not the execution.  Also note that with a guitar, the treble notes are usually left to ring when they form a broken chord and the string is not required for other notes of the figure.  The treble strings, however, don't have quite the sustain power of the metal-wound bass strings.
Taking all that together, a piano rendition of that passage will fit the original Cohen style quite well by just employing the sustain pedal every measure.  Not every wide-range polyphonic string instrument passage can be mapped to piano that well, but this one works fine by pedaling it.
Answer: As Tim H says, the pedal will come to your aid. Here, it works well, as the harmony stays the same for each bar, which is why the one bass note can last the bar too. If there were chord changes within the bar, but the bass note stayed long, it would be problematic.


How to add multiple calendar events using Microsoft graph

Question: I am able to create a single outlook calendar event but how to create multiple at the same time, is there is any way using Microsoft graph api?
I have tried passing an array of events to api, throwing JSON error
<code>var event = {
    "subject": "Let's go for lunch",
    "body": {
        "contentType": "HTML",
        "content": "Does late morning work for you?"
    },
    "start": {
        "dateTime": "2017-04-15T12:00:00",
        "timeZone": "Pacific Standard Time"
    },
    "end": {
        "dateTime": "2017-04-15T14:00:00",
        "timeZone": "Pacific Standard Time"
    },
    "location": {
        "displayName": "Harry's Bar"
    },
    "attendees": [{
        "emailAddress": {
            "address": "email@example.com",
            "name": "Samantha Booth"
        },
        "type": "required"
    }]
}

client
    .api('/me/events')
    .post(event, (err, res) => {
        console.log(res)
    })
</code>
for one event it's working but I need to add multiple events lets say 2 pm to 3 pm and 6 pm to 7 pm, for loop consuming too much time
Answer: Currently the 'Events' api's post method allows to send request body with information only for a single event.
In order to create multiple events in a single request you can batch your request for various endpoints as a request body and post it to
Batch Graph Request Endpoint.
You can find more info on
Json Batching Multiple Graph Requests here


Image of base change of immersion

Question: I'm revising for my exams now and struggling with the following exercise:
Let $f: X \to S$ be an open or closed immersion and $g: S' \to S$ another morphism where $X,S,S'$ are schemes. Then the base-change morphism $f' : X \times_S S' \to S'$ is such that $\text{Im}(f') = g^{-1}(\text{Im}(f))$.
If anyone could prove this for me I'd be really grateful!
Comment: Is $\operatorname{Im}$ the scheme-theoretic or set-theoretic image?
Comment: Scheme-theoretic, I think...
Answer: In this answer, $X$(resp. $X\otimes_S S'$), and $\mathrm{Im} f$(resp. $\mathrm{Im} f'$) are identified. (This is not problematic at all, especially about $X\otimes_S S'$, and $\mathrm{Im} f'$, thanks to the following fact. This is a standard result in algebraic geometry, and I think you have proved it in class. However, if you do not know it, please see Section 7.3, Proposition 13 of S. Bosch, Algebraic Geometry and Commutative Algebra, Springer, 2013 for a proof.)
Fact $f'$ is an open (resp. closed) immersion if $f$ is an open (resp. closed) immersion.
When $f$ is an open immersion, it is clear from the pull-back diagram
$$
\begin{array}{ccccccccc}
g^{-1}(X)& \xrightarrow{f'} & S' \\
\downarrow&  & \downarrow{g} \\
X& \xrightarrow{f} & S,
\end{array}
$$
which owes to the universal property of open immersion.
Prop (The Universal Property of Open Immersion)
If $f:X\to Y$ is a morphism between schemes, $V\subseteq Y$ an open subset(, or subscheme), and $f(X)\subseteq V$, then there exists a unique morphism $f':X\to V$ whose composition with the open immersion $V\to X$ is $f$.
Proof You can do it through looking at the structure sheaf of V, etc.
When $f$ is a closed immersion, first assume that $S$, and $S'$(, and so $X$) are affine schemes. Let $S = \mathrm{Spec} A, X = \mathrm{Spec} A/\mathfrak{a}$, and $S' = \mathrm{Spec} B$. Then, $X\times_S S'=\mathrm{Spec} A/\mathfrak{a}\otimes_A B=\mathrm{Spec} B/\mathfrak{a}B = V(\mathfrak{a}B) = g^{-1}(V(\mathfrak{a})) = g^{-1}(X)$. For the second last equality, if $\phi: A\to B$ is the corresponding homomorphism between rings to $g$, then $\mathfrak{q}\in g^{-1}(V(\mathfrak{a}))\Leftrightarrow \phi^{-1}(\mathfrak{p})\in V(\mathfrak{a})\Leftrightarrow \phi^{-1}(\mathfrak{p})\supseteq\mathfrak{a}\Leftrightarrow\mathfrak{p}\supseteq\phi(\mathfrak{a})(=\mathfrak{a}B)\Leftrightarrow \mathfrak{p}\in V(\mathfrak{a}B)$.
In the general case, take an affine open covering $S = \cup_{j\in J} V_j$, and for each $j\in J$, $g^{-1}(V_j) = \cup_{i\in I_j} U_{i,j}$. Let $g_{i,j}:U_{i,j}\to V_j$, and $g_{j}:g^{-1}(V_j)\to V_j$ be the restriction of f. Then, $X\times_S S' = \cup_{j\in J}\cup_{i\in I_j} f^{-1}(V_j)\times_{V_j} U_{i,j} = \cup\cup g_{i,j}^{-1} (V_j) = \cup g_{j}^{-1} (V_j) = g^{-1}(V)$.

Image of base change of immersion


Diferença entre os parâmetros maxiter e maxfun na função fmin_l_bfgs_b

Question: No help da função:
maxfun : int
Maximum number of function evaluations.
maxiter : int
Maximum number of iterations.

Mas o que é o número máximo de iterações e o número de avaliação de funções?
Para mim, o número de iterações era diretamente relacionado ao número de avaliações da função, mas eles dão resultados diferentes. Como é calculado o número de avaliações de função?
Answer: Posso estar errado, mas quando você otimiza uma função, você pode ter que avaliar o valor ótimo dos outputs, realizando diversas 'evaluations' a cada 'iteration'.
Em uma interação do algoritmo L-BFGS-B pode ter que realizar (e geralmente realiza) múltiplas avaliações/comparações de valores até chegar ao valor ótimo para os parâmetros.
O parâmetro maxiter controla o número de iterações que o algoritmo pode realizar.
Já o parâmetro maxfun, refere-se o número de avaliações/comparações que o algoritmo pode fazer, a cada iteração, acerca da função objetivo.
Ou em outras palavras:
O processo de otimização de uma função pode requerer ao otimizador, que este avalie o valor da função objetivo, diversas vezes com diferentes conjuntos de parâmetros.
O nº máximo de interações que o otimizador pode rodar é controlado pelo parâmetro maxiter.
O nº máximo de vezes que a função objetivo pode ser avaliada/testada, com diferentes conjuntos de parâmetros, é controlado pelo parâmetro maxfun.
Por exemplo:
Sabemos que a execução do algoritmo de otimização em uma determinada função objetivo vai levar 10 interações, com cada interação requerendo avaliação dos valores obtidos por 5 vezes, até chegar ao valor ótimo.
Temos então que o maxiter deve ser igual a 10 e o maxfun igual a 10*5 = 50.
Caso contrário a função não rodará o nº mínimo de vezes para ter seus parâmetros otimizados.


Why is this activation function nonlinear in the parameters but linear in the variables?

Question: I am taking a course on machine learning. My textbook says that a certain category of activation function of the following form, are linear in the variable vector x, but nonlinear in the parameters:

y(x) = f(w^T x + w0)

Why is the function nonlinear in the parameters but linear in the variables, even though in the above equation we can just substitute the variables with the parameters without changing the functional form?
here is the relevant excerpt:

f( · ) is known as an activation function. [...] The decision surfaces
  correspond to y(x) = constant, so that wTx + w0 = constant and hence
  the decision surfaces are linear functions of x, even if the function
  f(·) is nonlinear. [...] Note, however, that in contrast to the models
  used for regression, they are no longer linear in the parameters due
  to the presence of the nonlinear function f(·).
Comment: This might make sense if you specify a particular $f$. Just for the sake of argument, suppose that $f(x)=\cos(w^Tx+w_0)$. What do you notice happens when you fix $w$ and $x$ but vary $w_0$ between $-2\pi$ and $2\pi$?
Comment: "The decision surfaces
correspond to y(x) = constant, so that wTx + w0 = constant and hence the decision
surfaces are linear functions of x, even if the function f(·) is nonlinear"
why y(x) = constant, isn't y(x) changes as x changes?
Answer: Lets call $r:=r(x)=w^Tx+w_0$ the response. The response is a number, and as a function of response the activation is $f(r)$. This function is nonlinear in $r$. On the other hand, the response $r(x)$ is linear in $x$: $r(ax+by)=ar(x)+br(y)$ for constants $a,b$ and vectors $x,y$.
Your features $x$ combine with the weight $w$ to form a response $r$. In this way, many different kinds of $x$ will give you a similar response $r$. For example if $w=(1,1)$ and $w_0=0$, then $x=(1,2)$ and $x=(4,-1)$ will give you the same response $r=3$. To the activation function, data points which yield similar responses are indistinguishable and hence will yield similar activations. However the activation is nonlinear in $r$ so that if $x$ yields response $r(x)$ and $y$ yields response $r(y)$, then $f(r(x)+r(y))$ does not necessarily equal $f(r(x))+f(r(y))$. Thinking of $f$ as a function of $x,y$:
$$f(r(x+y))=f(r(x)+r(y))\neq f(r(x))+f(r(y)).$$


How to visualize a decision tree?

Question: I am doing a multi class classification of the data generated from a few group of subjects.
I have a dataset of 61 attributes and 4 groups. And I tried plotting decision tree for the same using the following code.
<code>>dataset

>fit <- rpart(Group ~ a1 + a2 + a3 + a4 +..... + a61, data=dataset)
>plot(fit)

>text(fit, use.n = TRUE)
</code>
Where a1, a2 and a3 ..... a61 are the attributes of the dataset.
The tree plotted is very huge and doesnot fit in the desktop.. Could anyone help to visualize the tree? Or is there any other alternative method to prune the tree?
Answer: You can use the rpart.plot package.
Check out this blog post.
However, you should consider pruning the tree: you'll get an easier to interpret (and to plot) model.

How to visualize a decision tree?


assign a double value from the model to a property of a control

Question: I have a very struggling problem:
In the Model, I have this property:
Double ConversionRate;

I would like to assign that value to a property of a control:
<StoreControl:StoreUI ConversionRate="10.1">

It works when I try to hard-coded the value but when I try it like this:
<StoreControl:StoreUI ID="StoreUI1" runat="server" ConversionRate='<% (double)Model.ConversionRate %>'>

Then I got this error:
Cannot create an object of type 'System.Double' from its string representation

How can I solve this problem?
Comment: ASP.NET MVC doesn't use controls.
Comment: really? so how can I reuse controls from Asp.NET?
Comment: Have you tried removing `(double)`?
Comment: Yes, I did too but there is still an error
Comment: Same error? Where does the error occur? What is returned by `Model.ConversionRate` when the error occurs?
Answer: Try this instead:
'<%: (double)Model.ConversionRate %>'

Or Alternately
'<%= (double)Model.ConversionRate %>'

Hope this will help !!
Comment: These two solutions don't help. It shows 0??? but the true value is not 0. What is wrong?
Comment: What value you have assigned to it in your controller?


Third-party modules installed with Composer don't have their version in the .info.yml file

Question: I have a project installed by composer, everything working perfectly, except when i run composer install contrib module installed successfully in web/modules/contrib but none of the modules .info.yml contain something like:
# Information added by Drupal.org packaging script on 2016-06-09
version: '8.x-3.0-alpha2'
core: '8.x'
project: 'MODULENAME'
datestamp: 1465505348

Issue is i don't see module needs update in admin/reports/updates
and when i run drush ups i get

Or even composer show i get

Even on admin/modules i can't see the module version

Like the following picture from another project

And i can't run drush updb throw an error

The Pathauto module is not compatible with the current version of Drupal core. Update the Pathauto module to 8.x-1.6 or later.

Because it check on pathauto version in system_requirements
and get the version as null:
$info = \Drupal::service('extension.list.module')->getExtensionInfo('pathauto');
// This is null
$info['version']

Composer version 1.9.1 .
My composer.json:
{
  "authors": [
    {
      "name": "Author",
      "email": "firstname.lastname@example.com"
    },
    {
      "name": "Author 2",
      "email": "firstname.lastname@example.com"
    }
  ],
  "autoload": {
    "psr-4": {
      "Osinet\\Deploy\\": "scripts/Osinet/"
    },
    "classmap": [
      "scripts/composer/ScriptHandler.php"
    ]
  },

  "config": {
    "discard-changes": true,
    "preferred-install": "source",
    "process-timeout": 3600,
    "secure-http": false,
    "sort-packages": true
  },
  "conflict": {
    "drupal/drupal": "*"
  },
  "description": "Project",
  "extra": {
    "installer-paths": {
      "drush/contrib/{$name}": ["type:drupal-drush"],
      "import": ["type:zend-extra"],
      "web/core": ["type:drupal-core"],
      "web/libraries/{$name}": ["type:drupal-library"],
      "web/modules/contrib/{$name}": ["type:drupal-module"],
      "web/profiles/contrib/{$name}": ["type:drupal-profile"],
      "web/themes/contrib/{$name}": ["type:drupal-theme"]
    },
    "osinet-builder": {
      "params": "settings/params.local.yml",
      "tmp-dir": "tmp"
    },
    "patches": {
      "drupal/media_entity": {
        "Fix broken route remote": "https://www.drupal.org/files/issues/dynamically_define_menu-2831071-17.patch"
      }
    },
    "bin-dir": "bin"
  },
  "license": "GPL-2.0+",
  "minimum-stability": "dev",
  "prefer-stable": false,
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/dawehner/drush_language.git"
    },
    {
      "type": "vcs",
      "url": "https://github.com/kgaut/drupal-potx.git"
    },
    {
      "type": "composer",
      "url": "https://packages.drupal.org/8"
    }
  ],
  "require": {
    "php": "^7.0.0",
    "ext-SPL": "*",
    "ext-SimpleXML": "*",
    "ext-Zend-OPcache": "*",
    "ext-ctype": "*",
    "ext-curl": "*",
    "ext-date": "*",
    "ext-dom": "*",
    "ext-filter": "*",
    "ext-gd": "*",
    "ext-hash": "*",
    "ext-json": "*",
    "ext-openssl": "*",
    "ext-pcre": "*",
    "ext-pdo": "*",
    "ext-pdo_mysql": "*",
    "ext-session": "*",
    "ext-tokenizer": "*",
    "ext-xml": "*",
    "lib-curl": "^7.36.0",
    "lib-iconv": "*",
    "lib-libxml": "*",
    "lib-openssl": "*",
    "lib-pcre": "*",
    "composer/installers": "^1.3",
    "cweagans/composer-patches": "~1.6.2",
    "drupal-composer/drupal-scaffold": "^2.3",
    "drupal/admin_toolbar": "^1.19",
    "drupal/advagg": "^4.0",
    "drupal/allowed_formats": "^1.1",
    "drupal/autologout": "^1.0",
    "drupal/cdn": "3.x-dev",
    "drupal/ckeditor_iframe": "^2.0",
    "drupal/ckeditor_media_embed": "1.x-dev",
    "drupal/color_field": "2.x-dev",
    "drupal/console": "^1.8.0",
    "drupal/core": "8.8.1",
    "drupal/crop": "1.x-dev",
    "drupal/date_popup": "1.x-dev",
    "drupal/draggableviews": "1.x-dev",
    "drupal/drush_language": "dev-8.x-1.x",
    "drupal/eck": "^1.0",
    "drupal/entity": "^1.0-rc1",
    "drupal/entityqueue": "1.x-dev",
    "drupal/fakeobjects": "1.x-dev",
    "drupal/field_group": "^3.0-beta1",
    "drupal/field_hidden": "^1.0",
    "drupal/file_version": "^1.0",
    "drupal/form_mode_manager": "2.x-dev",
    "drupal/image_widget_crop": "^2.1",
    "drupal/inline_entity_form": "1.x-dev",
    "drupal/jwt": "^1.0@alpha",
    "drupal/maxlength": "1.0-beta4",
    "drupal/media_entity": "1.x-dev",
    "drupal/media_entity_image": "1.x-dev",
    "drupal/memcache": "2.x-dev",
    "drupal/migrate_plus": "^4.0",
    "drupal/migrate_source_csv": "2.2",
    "drupal/migrate_tools": "^4.0",
    "drupal/pathauto": "1.6",
    "drupal/restui": "^1.17",
    "drupal/toolbar_menu": "^2.1",
    "drupal/ultimate_cron": "^2.0",
    "drush/drush": "8.x-dev"
  },
  "require-dev": {
    "behat/behat": "^3.4",
    "behat/mink-extension": "^2.2",
    "behat/mink-goutte-driver": "^1.2",
    "composer/composer": "^1.5@dev",
    "doctrine/cache": "~1.6.2",
    "doctrine/collections": "~1.4.0",
    "doctrine/common": "~2.7.0",
    "doctrine/inflector": "~1.2.0",
    "doctrine/instantiator": "~1.0.5",
    "drupal/coder": "^8.2",
    "drupal/config_inspector": "1.x-dev",
    "drupal/devel": "1.x-dev",
    "kgaut/potx": "8.x-1.x-dev",
    "phpunit/phpunit": "^6.1",
    "squizlabs/php_codesniffer": "^2.7.0",
    "symfony/phpunit-bridge": "^3.4.3"
  },
  "scripts": {
  },
  "type": "project"
}

Any ideas, will be appreciated!
Answer: Remove "preferred-install": "source" from config or set it to "dist".
 Putting "source" means Composer will get a package from a version control repository. And it seems to be downloading directly from https://git.drupalcode.org where all projects have no fixed version in their info files no matter which Git tag you check out.
You also can set more advanced patterns, check https://getcomposer.org/doc/06-config.md#preferred-install. In your case it probably can be boiled down to the following.
{
    "config": {
        "preferred-install": {
            "drupal/*": "dist",
            "*": "source"
        }
    }
}

Then remove vendor/, remove the lock file and reinstall. rm -rf vendor/ && rm composer.lock && composer install.
Answer: As pointed out in the other answer, you are getting Git checkouts due to the "preferred-install": "source" in the composer.json file. You could remove that (or replace the "source" value with "dist", although presumably you have that there for a reason).
To solve this problem, there is the Composer Deploy module, which augments Drupal's internal version information with information from composer's metadata. If you want to install from source and you want version information, this is the way to do it.


What search tool to use for data living outside of Drupal on the external API?

Question: I have an external API with Doctors data. Every doctor item will have:

Title
Location ( geolocation address )
Opening hours
Short description
Area of expertise
Price range

On my Drupal 8 website, I want to be able to utilize a search of Doctors data that will have filters ( location proximity and zip, some checkboxes, and dropdowns ).
I also want to have a google map view with these filters.
I'm looking for the best approach in the selection of search tools. My current options are:

Search API ( i used it before but never with external data )
Elastic Search ( still not sure about implementation )
Introducing Feeds to import data into Drupal and then use Search API
Something else?

I'm open to any suggestion in terms of implementation and tools selection.
Comment: 1. Wont do unless you have direct access to db. 2. This is not what you think. 3. While possible, I would consider this bad practice. 4. Custom module utilizing the Form API is the way to go.
Answer: If you have access to the external database credentials, there is a module called views database connector that gives you access to an external database in views.
I think it might be possible to configure a view to search through an external database with that module.
UPDATE: Otherwise you might consider creating a rest export view, there are a ton of guides on how to do that, here is one.
Good luck.
Comment: Since OP mentioned `external API` sounds like he doesn't have, direct access, access to any DB credentials.


UNION não está consolidando a SOMA entre 2 tabelas PostgreSQL

Question: Sou novo por aqui e estou buscando uma solução para este problema.
Tenho duas tabelas que somam valores de produção por hora de dois times distintos, porém, com o UNION ocorre a duplicação das linhas com a hora que contém valores.
Ex.:
Resultado da query A

Resultado da query B

Resultado da query com UNION entre as duas tabelas

Eu preciso que os valores das horas coincidentes sejam apresentados consolidados (somados).
SELECT DISTINCT
   TO_char(a.acodatinc,'HH24') AS hora,
   SUM(ap.acoparvallan) AS valor_acordo
FROM acordo a
 INNER JOIN acordo_parcela ap on ap.acocod = a.acocod
 INNER JOIN usuario u on u.usucod = a.acousuinc
 INNER JOIN devedor d ON d.devcod = a.devcod
WHERE a.acodatinc::DATE = current_date-1
AND a.acoati = 0
AND ap.acoparati = 0
AND d.devsal > 0
AND ap.acoparnum = 1
AND a.carcod = 621
AND u.usunom NOT ILIKE '%1987%'
AND u.usunom NOT ILIKE '%105%'
AND u.usunom NOT ILIKE '%463%'
AND u.usunom NOT ILIKE '%1992%'
AND u.usunom NOT ILIKE '%2014%'
GROUP BY TO_CHAR(a.acodatinc,'HH24')

 UNION

SELECT
 x.hora_excecao::TEXT AS hora,
 SUM(x.valor) AS valor_acordo

FROM (
 SELECT DISTINCT

 (SELECT
   CASE
   WHEN x.carqueevecod = 3 THEN x.carresiteval
   END AS valor
   FROM carteira_resposta_item x
   WHERE x.carcod = cri.carcod
   AND x.devcod = cri.devcod
   AND x.carqueevecod = 3
   ORDER BY carresdat DESC LIMIT 1
 ) AS valor,

 (SELECT
   CASE
   WHEN x.carqueevecod = 7 THEN x.carresiteint::TEXT
   END AS vencimento
   FROM carteira_resposta_item x
   WHERE x.carcod = cri.carcod
   AND x.devcod = cri.devcod
   AND x.carqueevecod = 7
   ORDER BY carresdat DESC LIMIT 1
 ) AS hora_excecao

 FROM carteira_questionario_evento cqe,
   carteira_questionario_evento_o cqeo,
   carteira_resposta_item cri,
   carteira_resposta cr,
   devedor d
 WHERE cqe.carqueevecod = cqeo.carqueevecod
 AND cri.carqueevecod = cqe.carqueevecod
 AND cqe.carcod = cri.carcod
 AND cr.carcod = cri.carcod
 AND cr.devcod = cri.devcod
 AND d.carcod = cri.carcod
 AND d.devcod = cri.devcod
 AND cri.carcod = 621
 AND  cri.carresdat = current_date-1
)x
GROUP BY x.hora_excecao
ORDER BY hora

Comment: Seja bem-vindo ao Stackoverflow. O `Union` une os resultados, não realiza a soma. Precisará utilizar o `SUM` ou relacionar as duas consultas e fazer a soma.  Tentou fazer em vez de duas consultas, apenas uma?
Comment: Olá Clark Maciel! Obrigado pela resposta! Não consegui unir a lógica em uma consulta somente. Acabam duplicando valores e outras ocorrências nos testes que fiz. Aí consegui os valores íntegros usando UNION, porém, sem conseguir somá-los. Tentei tratar a soma no PHP pra jogar num gráfico do Charts.js, mas, não tô evoluindo tbm…
Comment: Você sempre vai usar com esse campo `AND a.carcod = 621`? Ou pretende mudar esse valor em algum momento?
Comment: Ele sempre será fixo.
Answer: O UNION não deve ser utilizado para "consolidar soma", mas sim para unir conjuntos. Após unidos, poderá usar Group By para agrupar os resultados e a função sum para somar os campos desejados.
O ideal é buscar gerar uma consulta única que relacione as tabelas envolvidas e gere o resultado esperado através de uma tabela em comum que relacione as duas consultas, neste caso, observei que a tabela devedor existe em ambas as consultas, podendo tornar-se o elo de ligação entre as duas consultas.
Visto que você já possui as consultas que geram os resultados parciais, você pode criar duas visões(acordo_parcela e acordo_carteira), usar o UNION ALL para unir os itens dos conjuntos e somá-los.
O Union desconsidera tuplas iguais de tal forma que desconsideraria se existisse duas tuplas idênticas em cada conjunto, então no Union existiria apenas uma tupla como resultado, ação que tornaria o valor somado incorreto, na sua situação. No union all existirão as duas tuplas que, na sua situação, é o necessário, por tratar-se de conjuntos distintos onde os valores não devem ser suprimidos no caso da existência de coincidência dos registros de cada conjunto.
Select hora, sum(valor_acordo) as valor_total from (
  Select hora, valor_acordo from acordo_parcela
  Union All
  Select hora, valor_acordo from acordo_carteira
) acordos
group by hora

Esta é apenas uma forma de fazer com base em união (union) de resultados já obtidos por meio das suas consultas. Também pode utilizar joins para relacionar as visões/tabelas envolvidas e somar os valores dos campos de cada visão.
Para maiores detalhes sobre:

O uso de Joins, consulte: Qual é a diferença entre INNER JOIN e OUTER JOIN?
Diferença entre Union e Union All, consulte: Qual é a diferença entre UNION e UNION ALL?
Views, consulte: O que são views em SQL? Quais vantagens e desvantagens em utilizar?


How to use the Z format specifier in Microsoft's printf (and wprintf)?

Question: I would like to use the Z conversion specifier in printf, which is a Microsoft extension as described here: https://learn.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions

When the address of an ANSI_STRING or UNICODE_STRING structure is
passed as the argument, display the string contained in the buffer
pointed to by the Buffer field of the structure. Use a size modifier
prefix of w to specify a UNICODE_STRING argument—for example, %wZ. The
Length field of the structure must be set to the length, in bytes, of
the string. The MaximumLength field of the structure must be set to
the length, in bytes, of the buffer.

My code (I use GetProcAddress here so that it can be tested without installing the Windows DDK):
typedef struct _STRING {
    unsigned short lengthInBytes;
    unsigned short capacityInBytes;
    char* data;
} ANSI_STRING;

typedef struct _UNICODE_STRING {
    unsigned short lengthInBytes;
    unsigned short capacityInBytes;
    wchar_t* data;
} UNICODE_STRING;

int main()
{
    HINSTANCE hModule = LoadLibraryW( L"NtosKrnl.exe" );

    typedef void ( CALLBACK *RtlInitAnsiStringType )( ANSI_STRING*, char const* );
    RtlInitAnsiStringType RtlInitAnsiString = ( RtlInitAnsiStringType ) GetProcAddress( hModule, "RtlInitAnsiString" );
    ANSI_STRING ansi_string;
    RtlInitAnsiString( &ansi_string, "Text" );
    errno = 0;
    int Z_a_return = printf( "printf %%Z <= ansi_string: %Z.\n", &ansi_string );
    int Z_a_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", Z_a_return < 0  ?  "\n"  :  "", Z_a_return, Z_a_errno );
    errno = 0;
    int hZ_a_return = printf( "printf %%hZ <= ansi_string: %hZ.\n", &ansi_string );
    int hZ_a_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", hZ_a_return < 0  ?  "\n"  :  "", hZ_a_return, hZ_a_errno );
    errno = 0;
    int wZ_a_return = printf( "printf %%wZ <= ansi_string: %wZ.\n", &ansi_string );
    int wZ_a_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", wZ_a_return < 0  ?  "\n"  :  "", wZ_a_return, wZ_a_errno );

    errno = 0;
    int w_Z_a_return = wprintf( L"wprintf %%Z <= ansi_string: %Z.\n", &ansi_string );
    int w_Z_a_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", w_Z_a_return < 0  ?  "\n"  :  "", w_Z_a_return, w_Z_a_errno );
    errno = 0;
    int w_hZ_a_return = wprintf( L"wprintf %%hZ <= ansi_string: %hZ.\n", &ansi_string );
    int w_hZ_a_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", w_hZ_a_return < 0  ?  "\n"  :  "", w_hZ_a_return, w_hZ_a_errno );
    errno = 0;
    int w_wZ_a_return = wprintf( L"wprintf %%wZ <= ansi_string: %wZ.\n", &ansi_string );
    int w_wZ_a_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", w_wZ_a_return < 0  ?  "\n"  :  "", w_wZ_a_return, w_wZ_a_errno );

    typedef void ( CALLBACK *RtlInitUnicodeStringType )( UNICODE_STRING*, wchar_t const* );
    RtlInitUnicodeStringType RtlInitUnicodeString = ( RtlInitUnicodeStringType ) GetProcAddress( hModule, "RtlInitUnicodeString" );
    UNICODE_STRING unicode_string;
    RtlInitUnicodeString( &unicode_string, L"Text" );
    errno = 0;
    int Z_u_return = printf( "printf %%Z <= unicode_string: %Z.\n", &unicode_string );
    int Z_u_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", Z_u_return < 0  ?  "\n"  :  "", Z_u_return, Z_u_errno );
    errno = 0;
    int hZ_u_return = printf( "printf %%hZ <= unicode_string: %hZ.\n", &unicode_string );
    int hZ_u_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", hZ_u_return < 0  ?  "\n"  :  "", hZ_u_return, hZ_u_errno );
    errno = 0;
    int wZ_u_return = printf( "printf %%wZ <= unicode_string: %wZ.\n", &unicode_string );
    int wZ_u_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", wZ_u_return < 0  ?  "\n"  :  "", wZ_u_return, wZ_u_errno );

    errno = 0;
    int w_Z_u_return = wprintf( L"wprintf %%Z <= unicode_string: %Z.\n", &unicode_string );
    int w_Z_u_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", w_Z_u_return < 0  ?  "\n"  :  "", w_Z_u_return, w_Z_u_errno );
    errno = 0;
    int w_hZ_u_return = wprintf( L"wprintf %%hZ <= unicode_string: %hZ.\n", &unicode_string );
    int w_hZ_u_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", w_hZ_u_return < 0  ?  "\n"  :  "", w_hZ_u_return, w_hZ_u_errno );
    errno = 0;
    int w_wZ_u_return = wprintf( L"wprintf %%wZ <= unicode_string: %wZ.\n", &unicode_string );
    int w_wZ_u_errno = errno;
    printf( "%sprintf returned %d, errno = %d.\n\n", w_wZ_u_return < 0  ?  "\n"  :  "", w_wZ_u_return, w_wZ_u_errno );

    return 0;
}

The output:
printf %Z <= ansi_string:
printf returned -1, errno = 42.

printf %hZ <= ansi_string: Text.
printf returned 33, errno = 0.

printf %wZ <= ansi_string:
printf returned -1, errno = 42.

wprintf %Z <= ansi_string: Text.
printf returned 33, errno = 0.

wprintf %hZ <= ansi_string: Text.
printf returned 34, errno = 0.

wprintf %wZ <= ansi_string: ??.
printf returned 32, errno = 0.

printf %Z <= unicode_string: Text.
printf returned 35, errno = 0.

printf %hZ <= unicode_string: T e x t .
printf returned 40, errno = 0.

printf %wZ <= unicode_string: Text.
printf returned 36, errno = 0.

wprintf %Z <= unicode_string: T
printf returned -1, errno = 0.

wprintf %hZ <= unicode_string: T
printf returned -1, errno = 0.

wprintf %wZ <= unicode_string: Text.
printf returned 37, errno = 0.

A tabular summary:
printf  | %Z  | ANSI_STRING    | Bad  | errno == EILSEQ
printf  | %hZ | ANSI_STRING    | Good |
printf  | %wZ | ANSI_STRING    | Bad  | errno == EILSEQ
wprintf | %Z  | ANSI_STRING    | Good |
wprintf | %hZ | ANSI_STRING    | Good |
wprintf | %wZ | ANSI_STRING    | Bad  | output == "??"
printf  | %Z  | UNICODE_STRING | Good |
printf  | %hZ | UNICODE_STRING | Bad  | output == "T e x t "
printf  | %wZ | UNICODE_STRING | Good |
wprintf | %Z  | UNICODE_STRING | Bad  | output == "T"
wprintf | %hZ | UNICODE_STRING | Bad  | output == "T"
wprintf | %wZ | UNICODE_STRING | Good |

Conclusion:
hZ means ANSI_STRING, wZ means UNICODE_STRING both in printf and wprintf, while Z stands for UNICODE_STRING in printf and ANSI_STRING in wprintf.
My problem:
According to the documentation, Z means that the argument is the address of an ANSI_STRING, and wZ means that the argument is the address of a UNICODE_STRING, however, my example contradicts this, the meaning of Z depends on the function, it is UNICODE_STRING in printf and ANSI_STRING in wprintf.
What is wrong with my code? (Or did I misunderstand the documentation?)
Update
I reread the documentation, and it has been changed a few days ago, now it says this:

VS 2013 and earlier
When the address of an ANSI_STRING or UNICODE_STRING structure is
passed as the argument, display the string contained in the buffer
pointed to by the Buffer field of the structure. Use a size modifier
prefix of w to specify a UNICODE_STRING argument—for example, %wZ. The
Length field of the structure must be set to the length, in bytes, of
the string. The MaximumLength field of the structure must be set to
the length, in bytes, of the buffer.
Universal C Runtime (UCRT)
There is a known issue in the UCRT that is currently maintained for
compatibility. Like the S specifier, the Z specifier without a size
modifier prefix refers to a UNICODE_STRING when using a narrow
printing function (like printf) and an ANSI_STRING when using a wide
printing function (like wprintf). Instead of Z, use hZ to specify an
ANSI_STRING. wZ (or lZ) can still be used to specify a UNICODE_STRING.

However, the documentation does not provide any practical advice on how to deal with this inconsistency.
If I use a statically linked library, how can I detect the CRT version at compile time?
If I use a dinamically linked library, how can I detect the CRT version at run time?
How can I choose between the above programmatically (e.g. with a macro)?
Comment: Under what circumstances would you not control the format string? This is starting to sound like an [XY problem](https://xyproblem.info/).
Comment: sprintf_s / swprintf_s  implemented in many different modules. and may be some implementation is different. i say that implementations inside ntdll.dll, msvcrt.dll and ntoskrnl.exe always correct handle %Z format with ANSI_STRING
Comment: Microsoft told you exactly what to do, didn't they?  Use `%hZ` to print ANSI_STRINGs, use `%wZ` to print UNICODE_STRINGs, don't use `%Z`.  Or is there something I'm not understanding?
Comment: @zwol: If I cannot influence the format string, and it contains a `Z`, and I have to provide the argument either as an `ANSI_STRING` or as a `UNICODE_STRING`, how can I decide, which one to supply? If it is the old version, it should be the first, if the new version, then the latter. But how do I detect the version?
Comment: `swprintf_s` and `sprintf_s`, `_vsnwprintf`, ... ( all string formated api ) correct handle `%Z` - `ANSI_STRING`. also `DbgPrint` too. can first format string and then print it, if you need exactly print to console. also not `LoadLibraryW( L"NtosKrnl.exe" );` but `GetModuleHandleW(L"ntdll.dll")` already
Comment: @RbMm: I've tested your suggestion by printing to a buffer first, then printing its contents to stdout; my conclusion is that `sprintf_s` and `swprintf_s` handle `Z` the same way as I described it in my question. (I received the exact same output except for one case: `printf` with `hZ` and a `UNICODE_STRING` included `'\0'` characters, so the it was truncated after the first one, "T e x t ." has become "T" but this is not caused by a difference between `printf` and `sprintf_s`.)
Comment: @zwol: It's a long story. After calling functions like `vfprintf`, the contents of the `va_list` become indeterminate according to the C standards. So I would like to create my own version that accepts a `va_list*` instead. This function could be used to do some custom processing with format strings. I know that `Z` is rarely used but I would like it prepare for every possibility.
Comment: from which dll you call `sprintf_s / swprintf_s` ? i use ntdll.dll and msvsrt.dll. in bot this case it correct handle `%Z` format with ansi_string
Comment: Sounds like the solution to your problem is [`va_copy`](https://en.cppreference.com/w/c/variadic/va_copy).
Comment: @RbMm: I have a Microsoft Developer Studio 2022 project where the "Runtime Library" is setting equals "Multi-threaded Debug DLL (/MDd)". The point is, however, that - after the change of the documentation - you cannot say which behavior is the correct one. Both can be depending on the version of the CRT. And I don't see why sprintf_s would differ from printf in this respect, since their documentation links to the same page regarding the format specifiers.
Comment: @IInspectable: `va_copy` is part of my solution but is not enough because I need to track which arguments printf consumed.
Comment: @RbMm: Right, `( ( int ( * )( char const*, ... ) ) GetProcAddress( LoadLibraryW( L"ntdll.dll" ), "sprintf_s" ) )( buffer, 1024, "out: %Z", &ansi_string );` prints it correctly (according to the old rules). I'm not sure, however, that after changing it in VS, they won't change it at other places.
Comment: ntdll/msvcrt not depend frpm VS and any crt version. this was always, how thay handle %Z
Answer: All the discussion of what various concrete implementations, in various DLLs, of various versions of sprintf, actually do --- is a big fat red herring.
The real obstacle here is that your caller may have provided either a UNICODE_STRING or an ANSI_STRING along with an unqualified %Z argument, and you have no way of knowing which.
In your shoes, I would document that unqualified %Z is not supported at all, due to historical ambiguity, and callers must use either %hZ or %wZ.  And I'd enforce that in my code: prescan the format string and bomb out if an unqualified %Z is encountered.
Comment: Thanks, I'll avoid `Z` for now. But I may need to change this later, so I posted my own solution.
Answer: Alright, I think this is a quite rare situation but if anyone is interested, there are two ways to handle this question.

I wrote a function, isUniversalCRunTimeLibrary that - based on testing with snprintf - returns true if the C runtime library is successfully detected as this new universal one, and false otherwise (if the detection was successful but it is the old version, then the optional parameter detectionSucceededOptional is set to true; if it is set to false, it means that an error occurred during detection).

As RbMm suggested, ntdll's sprintf_s uses the old version: Compare bufferA and bufferB in the output. However, I'm afraid that if Microsoft changed this behavior in VS, then (assuming they use their own development product), they may change ntdll's sprintf_s in the future.

Here is my code:
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <Windows.h>

typedef struct _STRING {
    unsigned short lengthInBytes;
    unsigned short capacityInBytes;
    char* data;
} ANSI_STRING;

bool tryRtlInitAnsiString( ANSI_STRING* ansi_string, char const* content )
{
    HINSTANCE hModule = LoadLibraryW( L"NtosKrnl.exe" );
    assert( hModule != NULL );
    if( hModule == NULL ) {
        return false;
    }
    typedef void ( CALLBACK *RtlInitAnsiStringType )( ANSI_STRING*, char const* );
    RtlInitAnsiStringType pRtlInitAnsiString = ( RtlInitAnsiStringType ) GetProcAddress( hModule, "RtlInitAnsiString" );
    assert( pRtlInitAnsiString != NULL );
    if( pRtlInitAnsiString == NULL ) {
        return false;
    }
    pRtlInitAnsiString( ansi_string, content );
    return true;
}

bool detectUniversalCRunTimeLibrary( bool* detectionSucceeded )
{
    char const* testFormat = "%Z";
    char const testString[] = "tt";
    ANSI_STRING ansi_string;
    bool successRtlInitAnsiString = tryRtlInitAnsiString( &ansi_string, testString );
    if( !successRtlInitAnsiString ) {
        *detectionSucceeded = false;
        return false;
    }
    int errnoBefore = errno;
    errno = 0;
    int charactersWritten = snprintf( NULL, 0, testFormat, &ansi_string );
    int errnoRelevant = errno;
    errno = errnoBefore;
    bool correctOld = charactersWritten == sizeof( testString ) / sizeof( testString[ 0 ] ) - 1 && errnoRelevant == 0;
    bool correctNew = charactersWritten < 0 && errnoRelevant == EILSEQ;
    assert( correctOld || correctNew );
    *detectionSucceeded = correctOld || correctNew;
    return correctNew;
}

bool isUniversalCRunTimeLibrary( bool* detectionSucceededOptional )
{
    static bool universalCRunTimeLibrary = false;
    static bool detectionSucceeded = false;
    static bool alreadyTested = false;
    if( !alreadyTested ) {
        alreadyTested = true;
        universalCRunTimeLibrary = detectUniversalCRunTimeLibrary( &detectionSucceeded );
    }
    if( detectionSucceededOptional != NULL ) {
        *detectionSucceededOptional = detectionSucceeded;
    }
    return universalCRunTimeLibrary;
}

int main()
{
    bool detectionSucceeded = false;
    bool universalCRunTimeLibrary = isUniversalCRunTimeLibrary( &detectionSucceeded );
    printf( "universalCRunTimeLibrary: %d (detectionSucceeded: %d)\n", universalCRunTimeLibrary, detectionSucceeded );

    char const* testFormat = "%Z";
    char const testString[] = "tt";
    ANSI_STRING ansi_string;
    bool successRtlInitAnsiString = tryRtlInitAnsiString( &ansi_string, testString );
    if( !successRtlInitAnsiString ) {
        return EXIT_FAILURE;
    }

    char bufferA[ 256 ] = "";
    errno = 0;
    int writtenA = sprintf_s( bufferA, sizeof( bufferA ) / sizeof( bufferA[ 0 ] ), testFormat, &ansi_string );
    int errnoA = errno;
    printf( "bufferA = \"%s\", writtenA = %d, errnoA = %d\n", bufferA, writtenA, errnoA );

    HINSTANCE hModuleNtdll = LoadLibraryW( L"ntdll.dll" );
    assert( hModuleNtdll != NULL );
    if( hModuleNtdll == NULL ) {
        return EXIT_FAILURE;
    }
    typedef int ( *sprintf_sType )( char const*, ... );
    sprintf_sType psprintf_s = ( sprintf_sType ) GetProcAddress( hModuleNtdll, "sprintf_s" );
    assert( psprintf_s != NULL );
    if( psprintf_s == NULL ) {
        return EXIT_FAILURE;
    }

    char bufferB[ 256 ] = "";
    errno = 0;
    int writtenB = psprintf_s( bufferB, sizeof( bufferB ) / sizeof( bufferB[ 0 ] ), testFormat, &ansi_string );
    int errnoB = errno;
    printf( "bufferB = \"%s\", writtenB = %d, errnoB = %d\n", bufferB, writtenB, errnoB );

    return EXIT_SUCCESS;
}

The output:
universalCRunTimeLibrary: 1 (detectionSucceeded: 1)
bufferA = "", writtenA = -1, errnoA = 42
bufferB = "tt", writtenB = 2, errnoB = 0


Simplifying PowerShell pipeline splitting data from command output

Question: I'm trying to do some processing on the output from a psftp "ls" command.  Basically, I want to put all of the file names that match certain conditions into an array.  I have this:
PS C:\path\to\pwd> $a = & "C:\Program Files (x86)\PuTTY\psftp.exe" -l myusername -batch -pw mypassword -b sftpbatch.txt myserver | where {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[0] -eq "-rw-r--r--"} | select-object {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[8]}

(If you want more details about that command, I can provide them.  The output is very similar to the output of the "ls" command in PowerShell.)
It seems to me that I can do better by selecting the split first, then filtering it with where.  When I try this:
$a = & <# ... #> | select-object {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')} | where { $_[0] -eq "-rw-r--r--" }

I get
Unable to index into an object of type System.Management.Automation.PSObject.

How can I simplify this?
Comment: You probably need to run a regex on `psftp.exe`'s stdout. Post the output of the command and specify the text you want to extract.
Answer: Something like this should work.
$a = & <#...#>| % {$b = $_ -split ' '|?{$_}; if($b[0] -eq '-rw-r--r--'){$b[8]}}

If you are placing this in a script, I would replace the alias % with Foreach-Object, and the alias ? with Where-Object
Edit:
Here is a more pipeline-oriented approach:
'-rw-r--r-- 1 2 3   4 5 6 7 test.txt'|
    select @{Name='Words'; Expression={$_ -split ' '|where {$_}}}|
    where {$_.Words[0] -eq "-rw-r--r--"}|
    foreach {$_.Words[8]}

Comment: Yes, you can do it with all piped commands; it's just a little more verbose. I've added an example to my answer.
Comment: Cool, that does it.  Out of curiosity, is it possible to do this using an all-pipes approach like I'd originally tried?  Thanks for your help!
Comment: Thanks again.  I can't yet vote up your answer, but if I could, I would!


How to get data in vue template by call a function

Question: I have multiple stores. These shops sell a variety of products. A list of shops should be displayed on the page, as well as a list of the categories of products that a shop sells.
Console.log data in coming but I can't display Store product Category pare list
   <template>

{{store.name}}

//Other Store info

{{product.category.name}}
//This data is not Show

</template>

Thanks, Advance.
Comment: What's wrong with your code?
Comment: also, are we supposed to guess how your objects in the array look like? ;) Please post sample data.
Comment: I no. Store list show well but :set vareable data not Show. I have console data show please see include img.
Comment: yes, but what does the array contain, more specific, how does an object look like? What properties do they have?
Comment: At what point do you set the `products` values?
Comment: I flow this Technic https://codepen.io/mmghv/pen/dBqGjM?__cf_chl_jschl_tk__=f2dffb5639e533b86e6052ad15ca44b791e43279-1580224433-0-AX2RiDf7Y6DMreI7DgBhwQ97TlweNRf5HJ02YsndIRN-tNvqTOJTK968Xkzf5xrDWPny_WbKfAeUUMNlHJ579Ww4aOyKkL0KNZbRcA9XzpqXLCNOMgHmV7VG5JaTWftnbfr04HtHWya7gPGDYDyMPXMQiMGs9zL2DR8Ewp1SxRdUSxBuuoin4BJlecpc24QYfgdxLjgsuGs-37QEITlW_u9e6Dnm-A1KB8ypx14IaFIauRo-rIQ1JnUbLdHnjmwZ_XLraQEFFpxOYKWfrDjQruhBjSKLiGPRmABYpXN756nhqfiXoELr-VLNbILxG2jqABkvh2mN0zBs2lAHdbsWrY8
Comment: products values in set for loop
Comment: You have any idea to solve this problem?
Answer: I would take a completely different approach, since first of all, using :set, your http-requests would be fired whenever change detection happens. Instead gather all the data when component is rendered:
created() {
  this.getStores();
},
methods: {
  getProducts(id) {
    return axios.get("...")
      .then(response => response.data)
  },
  getStores() {
    // first get stores, you can also use async await instead, if you like
    axios.get("...").then(response => {
      this.stores = response.data;
      // gather all requests for each store to get products
      const reqs = this.stores.map(store => this.getProducts(store.id));
      axios.all(reqs).then(products => {
        // merge data
        this.stores = this.stores.map((store, i) => {
          return { ...store, products: products[i] };
        });
      });
    });
  }
}

Now, for each store, there is a products property:

{{store.name}}

      {{product.category.name}}

As for the template, make sure that category.name actually exists in the product object.


Guitar intonation issue - still flat but screw as far as it'll go

Question: I am having a serious issue with my guitar and looking for some help. I recently had the guitar set up in a shop and in order to get the intonation right the guitar tech had to adjust some of the string saddle screws on the bridge as far as they'd go (towards the neck) but now my intonation is out of whack again, as much as somewhere between 1/8th and 1/4 step on the B string but as the screws are as far as they'll go I'm not sure how I can fix this.
Is it possible that there is a problem with my guitar? such as warping of the neck or similar. my guitar has a fixed no-tremolo bridge:

So this cannot be a tremolo related issue obviously.
Can anyone offer any insight as to why this might be happening?
Would adjusting the action slightly or opting for a heavier string gauge change the amount the screws need to be adjusted in one direction or the other so that i might be able get some room to play with?
Alternatively do different bridges come with longer or shorter throw on the bridge saddles?
That being said, i have had the guitar for years so the current bridge should work fine.. in theory.
Someone please help provide some insight! The intonation issue is noticeable and renders the higher frets unplayable.
Comment: Usually the B string saddle is further away from the neck than the others, so from what you've stated, the others must be even closer to the neck.What do you mean  by 1/4 and 1/8 step ? A similar problem has been answered previously on this site already.Was the intonation ever correct on this guitar ?
Comment: @Tim 1/4 1/8 of a whole tone (half/quarter of a semitone) i'm guessing
Answer: This looks like a hardtail Strat bridge; definitely not the "Tune-O-Matic" of a Gibson. I've had intonation issues before that seemingly defied a solution. Here are some suggestions:

First, what's the difference in tuning between the 12th fret harmonic and the 12th fret itself? You need to make sure the problem really is in the saddle length. Pluck the harmonic, tune that, then fret the 12th fret and pluck again. They should be exactly the same note. Equivalently, when plucking the 12th fret harmonic, the most lively harmonic should come with your finger touching the string right over the 12th fret, not in front of or behind it. This is less accurate, as the string will settle quite naturally and quickly into its harmonic standing wave.
What's your neck relief? Put a capo on the first fret, press down the last fret, then slip a gap feeler under the string at the 9th fret (which is usually where the neck straightens regardless of the amount of relief). Fender recommends about a .010" gap (you can check this pretty easily with a cutting from your last E string; it'll be between .009" and .011" depending on preferred gauge which will be close enough); you adjust this gap using the truss rod. Some guys like their necks arrow-straight, but fail to realize that this increases the speaking length of notes between the first and fifth frets, lowering their pitch even when the spring is properly tuned and the saddles intonated. It's minor, but all of these adjustments are minor (so much so that you cannot do a setup by sight or feel).
What's your string height? Depress the string at the 9th fret (to take relief out of the equation) and measure the height from the top of the last fret to the bottom string with a depth gauge. Fender recommends 4/64" to 5/64". Most guitarists I know like it lower than the recommendation, assuming they have a good neck; however, the problem with this is that it will lower the tension placed on a fretted string; great news for your fretting speed (and your calluses) but bad news if you're out of room.
What's your string gauge? If you recently increased your gauge, say from 10-46 to 11-52, then even if the neck is adjusted to compensate, the string will bend less when you fret it, and that equals flatter notes, because you're not "pinching" the fretted notes sharp (in this case quite literally, Yngwie Malmstein style).

I typically have much more trouble with Gibson's Tune-O-Matics than with the Fender bridges; I end up with the notes trending sharp across all strings in the first octave because I like the action high which increases the tension of fretted notes, and the reduced adjustable range of the Gibson-style bridge saddles pretty much requires that you set it up their way.
If the saddle length screw is at its maximum and you really do need to go with a shorter speaking length, there's nothing for it but to look for a longer screw. Sometimes other saddles will have longer screws because the manufacturer expected to you have to bring those up more than you actually have to; if you have another, longer screw with plenty of slack, try switching. Check the parts and repair counter of your local store (and if you know of a good Mom & Pop repair shop in the area, make friends and see if they have something in the spare parts bin that they'll swap you for your current saddle screw).
The other thing you can do if all strings are at or near their minimum speaking length (maximum extension on the saddle screws) is to have the bridge repositioned; the luthier will remove the bridge, fill in the holes, then very carefully drill new ones an eighth of an inch or so further up and remount the bridge. This will give the extra adjustment room you need. However, this is brain surgery for luthiers; one mistake in redrilling the bridge mounting holes and your prize axe is garbage.
Comment: Bridges like you have pictured often come with TWO sets of screws, one set longer than the other. Assuming that the saddles have remaining clearance, perhaps swapping to longer screws may resolve the range of adjustment issue. Here is a similar bridge on my Tele: https://goo.gl/photos/pjEmBw6kPhutuctp8
Answer: Indeed an unusual problem but a solution could be using longer setscrews to replace those adjusting the intonation.From the look of the whole bridge, this is feasible, but normally wouldn't be necessary.It would allow the saddles to be moved closer to the neck; there is always the possibility the bridge has been mounted slightly too far back.
Obviously, the neck relief needs to be sorted first, along with any action problem - if the action is high, the harmonic will appear lower in pitch than the fretted note. More noticeable on a guitar set up for slide, for example.
Answer: This is a weird issue indeed for fixed bridges. Does your truss-rod need adjustment? Did you switch to a lighter gauge of strings recently?
Putting heavier strings does require you to move the saddles back. But this is not a solution I would recommend ; you shouldn't change the gauge just because you can't fix your guitar. You should be able to play your preferred gauge. And besides, changing gauge requires readjusting the truss-rod and saddles.
I'm guessing your problem is with action, and neck curvature (or both):

Your truss-rod has been loosened a little (making the neck more bowed). Can you tell if that's the case?
Your action was set lower. Check for that too, and leave a comment.

These two things require the saddles to be brought closer to the neck. Other than that it could be lighter strings.
Answer: Tuned to pitch, capo your guitar at the 12th fret. Beginning with the low E, adjust the bridge saddle height, just high enough so that each string is buzz free from the 12th to the highest fret. Remove the capo and check the neck relief. Too little and the strings will buzz on the lower frets. Again, using the truss rod (Clockwise to tighten, counterclockwise to relax), set the relief so that the strings are buzz free from open to the octave, with the minimum bow required. Then set your intonation. I'll bet that fixes it. You can probably find specific specs and procedures at the manufacturer's web site. Those are a guideline, but each guitar is unique and requires its own set up.
Answer: Christy O Less is correct, I've also been machining saddles on inexpensive guitars for years to get perfect intonation. In my experience, the big E is most problematic, too short and all fretted notes are sharp even with low action. After pulling out the saddle spring and screwing the saddle all the way against the bridge, the string is still too short. So then I remove the saddle, grind it on my bench grinder, smooth it with a Dremel and then reinstall it so it can now make the string longer.  Make sure you leave a few threads left in the saddle for the screw to grab onto.
Answer: If you are at the end of adjustment and the saddle won't move any more, you can find longer screws (they might be metric) if you are trying to make the string shorter, or you can remove the spring-it's not really needed- if you need to make the string longer.
I would not start adjusting the truss rod or anything else. Intonation is simply the length of the string in relation to the location of the frets. Likely it was marginal or just off all these years.
It's not just cheap guitars that have some variance in the placement of the bridge. I have a Gibson SG that was supposedly set up on the plex machine, and the high 3 is falling off the side of the neck.
Comment: Can you clarify what you mean by a high 3.
Answer: do you have access to a strobe tuner such as a Peterson.  I have never felt that you can get an accurate assessment by ear or with an led or needle tuner such as a Boss or Korg.  In fact I don't trust my ears for anything but making sure I'm playing the right song.  Another thing to think about is the height of the p/u's if the magnetic field is too close to the strings this will have an effect as well.