R replacing NAs with increment/decrement of non-NA values

Question: I'm trying to fill in missing year values by a group both incrementally and decrementally when I know that each increment/decrement should be 1. Here's a reproducible example. Note there are two ids where the first id starts in 2002 (although it's missing) and the second id starts in 2007 (again also missing). I know this from the existing year and the fact that the increment/decrement should be 1.
id <- rep(c("01012895", "01021890"), each = 6)
value <- c(rnorm(12))
year <- c("NA", "NA", 2004, "NA", "NA", 2007, "NA", "NA", "NA", 2010, "NA", "NA")
df <- data.frame(id, value, year)

df
         id       value year
1  01012895 -1.05853432   NA
2  01012895  0.06480395   NA
3  01012895 -3.32811206 2004
4  01012895 -0.33427578   NA
5  01012895 -0.22790538   NA
6  01012895  1.46991465 2007
7  01021890 -0.65928777   NA
8  01021890 -0.25701765   NA
9  01021890  1.24331689   NA
10 01021890  0.10006530 2010
11 01021890 -0.83971071   NA
12 01021890  1.15893263   NA

df2 is what I want eventually.
year2 <- c(2002:2007, 2007:2012)
df2 <- data.frame(id, value, year2)

I've tried to find a get around using fill, like in df3, but it only carries existing values without an option to alter it.
df3 <- df %>% group_by(site_no) %>% fill(year, .direction = "updown")

Answer: Here's one way to do it.
First, your own data:
id <- rep(c("01012895", "01021890"), each = 6)
value <- c(rnorm(12))
year <- c(NA, "NA", 2004, "NA", "NA", 2007, "NA", "NA", "NA", 2010, "NA", "NA")
df <- data.frame(id, value, year)

year2 <- c(2002:2007, 2007:2012)
df2 <- data.frame(id, value, year2)

Converting to integer from factor and using row numbers and grouped max indexes, we get the following:
library(dplyr)

df3 <- df %>%
  group_by(id) %>%
  mutate(
    year = as.integer(as.character(year)), # Convert factor to integer
    year =  max(year, na.rm = TRUE) + (row_number() - which.max(year))
  )

df3
#> # A tibble: 12 x 3
#> # Groups:   id [2]
#>    id         value  year
#>    <fct>      <dbl> <int>
#>  1 01012895  0.0482  2002
#>  2 01012895 -0.445   2003
#>  3 01012895  0.459   2004
#>  4 01012895 -0.740   2005
#>  5 01012895  1.91    2006
#>  6 01012895 -1.66    2007
#>  7 01021890 -1.08    2007
#>  8 01021890 -0.816   2008
#>  9 01021890  0.395   2009
#> 10 01021890 -0.0294  2010
#> 11 01021890  0.397   2011
#> 12 01021890  0.273   2012

all(df2 == df3)
#> [1] TRUE

It does throw a warning, which should be fine to ignore.


500 server error (connection reset), JS file won't load

Question: My Website: iansvoboda.com
This JS file I am loading doesn't load properly for some reason. This randomly cropped up at some point in the last week or so and I haven't actually made any changes between now and then.
www.iansvoboda.com/js/app.min.js

My console indicates that this is due to a 500 internal server error: ERR_CONNECTION RESET
I did some research and according to my server logs I saw an entry that stood out:
[Mon Jul 14 11:09:43 2014][debug] core.c(3118): [client 220.127.116.11] redirected from r->uri = /js/app.min.js, referer: http://iansvoboda.com/
[Mon Jul 14 11:11:11 2014][error] [client 220.127.116.11] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

Can anyone provide a suggestion for how to resolve? If you all need more info, just let me know.
Answer: Dig into your server configuration. Your logs say

Use 'LogLevel debug' to get a backtrace.

This will tell you where the redirects are going, and you can spot the ones that can be removed (nobody needs more than 10 redirects — even your logs say "probable configuration error"). This will depend on your website's needs.
If nothing else, raise the LimitInternalRecursion to a value that prevents the error — but note that if it's caused by an internal loop, this won't help.
Comment: Thanks for the post. I'm not really sure how to actually do this, any advice?


What does "sax loco" mean?

Question: I have found the notation "sax loco" in these two songs. What does it mean? I believe it's related to the fact that there are parts for two instruments in the same staff. By why "loco"?
From Moanin'.

From Stars and Stripes Forever, part for Eb Clarinet and Sopranino Sax.
Comment: I am *deeply* disappointed that the correct answer below has nothing to do with the Spanish meaning of "loco". Crazysax!
Answer: "Loco" means "in the written octave", as opposed to shifted by some number of octaves.  Such a shift could be due to 8va/8vb marks, or in this case, because of instrument transpositions.  Tenor sax sounds an octave lower than trumpet, so by writing both instruments on a single staff there's an ambiguity--are those tenor sax notes in the trumpet octave or the tenor sax octave?  This mark means "in the written octave", so in the trumpet octave.  The proper tenor sax part will be written an octave higher.
The second example is unclear, since Eb clarinet and sopranino sax transpose exactly the same.  I guess this means that the clarinet is on the upper line and the sax on the lower, but that would be a clunky way to write that.
Comment: @user3338584 - any chance that the Stars&Stripes example has "8va" notated previously to the measures you posted? That would make the "loco" more reasonable here -telling the sax to drop off the 8va range while the clarinet stays high.

What does "sax loco" mean?


Using Uitextfield for user input, trying to convert to int, if not int then return 0

Question: I am building a practice app to add user inputs in an app. I have a few user defaults from a UITextfield, but when I run my function it does not work when I a missing an input, bc it does not recognize an empty textfield as an integer. I am trying to create an if statement that if = " " then return INT(0) else the INT(user default).
@IBAction func saveButton(_ sender: Any) {
    UserDefaults.standard.set(hole1.text, forKey: "hole1enter")}

@IBAction func Total(_ sender: Any) {
     let h1:String = hole1.text! {
         if h1 == " " {
           print(0)
         } else {
             print(Int(h1))
         }
  }

I have tried a bunch of attempts, but I keep getting errors, I am just getting started with swift. Do you all have any good literature on a better way to understand converting strings, text, ints or functions themselves? In the end I want to be able to sum the total of all the userdefault inputs
Comment: You can just make the keyboard a number pad if you expect only numbers as an output
Comment: The `NumberFormatter` class converts between localized strings and numeric values. Create an instance and then use the `number(String)` function to convert from a localized string to a number (object). For more complicated parsing (which I don't think you need here), there is the `Scanner` class.
Answer: Not all Strings can be represented as an Int, so you need to check if the return value of the failable Int initializer returns nil or not. You can simply supply a default value as 0.
You can safely force-unwrap UITextFields text property, because it is documented to be always non-nil.
@IBAction func total(_ sender: Any) {
    let intValue = Int(hole1.text!) ?? 0
    print(intValue)
}

You should also conform to the Swift naming convention, which is lowerCamelCase for function and variable names (total).
Answer: First problem: you should check if text property is empty String, so you need to use "" not string with whitespace if h1 == " " (you can also use designed isEmpty property which returns true if string is "").
Next, converting from String to Int can fail so it returns optional. You can use optional-binding or setting default value for safely assigning value from value returned from initialization
@IBAction func Total(_ sender: Any) {

    let h1 = hole1.text!

    if h1.isEmpty {
        print(0)
    } else {
        let int = Int(h1) ?? 0
        print(int)
    }
}

which can be shorten just to this
@IBAction func Total(_ sender: Any) {
    let int = Int(hole1.text!) ?? 0
    print(int)
}

since if text is empty string, Int can't be constructed

Last problem which I see is that you're saving optional String? to UserDefaults. Don't do that, safely force-unwrap it using exclamation mark since text property of UITextField is never nil
@IBAction func saveButton(_ sender: Any) {
    UserDefaults.standard.set(hole1.text!, forKey: "hole1enter")
}


NGINX location rewrite for Gitlab

Question: I'm trying to make Gitlab work behind Nginx. my configs:
Nginx:
upstream gitlab-workhorse {
    server unix:/var/opt/gitlab/gitlab-workhorse/sockets/socket fail_timeout=0;
}

location /gitlab/ {
       rewrite ^/gitlab(/.*)$ $1 break;
       client_max_body_size 0;
       gzip off;
       proxy_read_timeout      300;
       proxy_connect_timeout   300;
       proxy_redirect          off;
       proxy_http_version 1.1;
       proxy_set_header    Host                $http_host;
       proxy_set_header    X-Real-IP           $remote_addr;
       proxy_set_header    X-Forwarded-Ssl     on;
       proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
       proxy_set_header    X-Forwarded-Proto   $scheme;
       proxy_pass http://gitlab-workhorse;
}

/etc/gitlab/gitlab.rb:
external_url "https://myhostname.com/gitlab"
web_server['external_users'] = ['www-data']
nginx['enable'] = false
gitlab_workhorse['enable'] = true
gitlab_workhorse['listen_network'] = "unix"
gitlab_workhorse['listen_addr'] = "/var/opt/gitlab/gitlab-workhorse/sockets/socket"

at request to myhost.com/gitlab/users/sign_in I get "/users/sign_in" not found
if I change in gitlab configuration
external_url "https://myhostname.com/"

(without /gitlab path in the end) it starts to work but it's always redirects to the / of the server instead of /gitlab
Answer: Since you already configured GitLab to use an URI prefix /gitlab with the
external_url "https://myhostname.com/gitlab"

(which is the only proper way to host anything bigger that a SPA under an URI prefix), you don't need to strip that /gitlab prefix from the request URI before passing the request to the GitLab backend. Remove that rewrite ^/gitlab(/.*)$ $1 break; line from your nginx config. What made you think it is needed at all?
Comment: apparently gitlab does not support relative urls
Comment: What makes you think so? The documentation [states](https://docs.gitlab.com/omnibus/settings/configuration.html#configure-a-relative-url-for-gitlab) exactly the opposite. Did you test your enviroment without the `rewrite` directive using the `external_url "https://myhostname.com/gitlab"`?
Comment: yeah thank you it actually helped!

NGINX location rewrite for Gitlab


Como saber se houve falha ao enviar SMS?

Question: Tenho esse método de envio de SMS via aplicação:
public boolean enviaSMS(String fone, String mensagem) throws Exception {
    try {
        smsManager.sendMultipartTextMessage(fone, null,
                smsManager.divideMessage(mensagem), null, null);
        return true;
    } catch (Exception e) {
        return false;
    }
}

Porém, o Android nunca retorna exception caso não consiga enviar, existe algum meio de obter o exception quando houve alguma falha no envio?
Comment: Verifique o log no Logcat que deve aparecer uma mensagem de erro
Comment: Já tinha verificado, o método só retorna exception caso o numero de telefone esteja nulo ou vazio, e a mensagem a mesma coisa, mas não retorna o status do envio, se foi enviado, se deu erro, etc... há algo que possa ser feito?
Answer: Falhas de envio não lançam excepção, elas são reportadas de outra forma.
Se olhar para a assinatura do método sendMultipartTextMessage verá que ele recebe dois Arrays<PendingIntent>, sentIntents e deliveryIntents.
public void sendMultipartTextMessage (String destinationAddress,
                                      String scAddress,
                                      ArrayList<String> parts,
                                      ArrayList<PendingIntent> sentIntents,
                                      ArrayList<PendingIntent> deliveryIntents)

Eles(PendingIntent) são utilizados para notificar o estado de envio e entrega de cada parte da MultipartTextMessage.
Comece por criar dois BroadcastReceiver, um para receber a notificação do estado de envio e outro para receber a notificação do estado de entrega.
SentReceiver.java
public class SentReceiver extends BroadcastReceiver {

    private static SentReceiver instance;

    private SentReceiver(){

    }

    public static SentReceiver getInstance(){

        if(instance == null)instance = new SentReceiver();
        return instance;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        switch (getResultCode()){
            case Activity.RESULT_OK:
                Toast toast = Toast.makeText(context, "SMS(parte) enviado",Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                toast = Toast.makeText(context, "Generic failure",Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                toast = Toast.makeText(context, "No service",   Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                toast = Toast.makeText(context, "Null PDU", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                toast = Toast.makeText(context, "Radio off", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            break;
        }

    }

}

DeliveredReceiver.java
public class DeliveredReceiver extends BroadcastReceiver {

    private static DeliveredReceiver instance;

    private DeliveredReceiver(){

    }

    public static DeliveredReceiver getInstance(){

        if(instance == null)instance = new DeliveredReceiver();
        return instance;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        switch (getResultCode())
        {
            case Activity.RESULT_OK:
                Toast toast = Toast.makeText(context, "SMS(parte) entregue", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            break;
            case Activity.RESULT_CANCELED:
                toast = Toast.makeText(context, "SMS(parte) não entregue", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            break;
        }
    }
}

No método enviaSMS() crie os Array<PendingIntent> e passe-os ao método sendMultipartTextMessage().
public void enviaSMS(Context context, String fone, String mensagem) {

    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
    ArrayList<String> messages = smsManager.divideMessage(mensagem);
    int messagesCount = messages.size();

    //Criar os PendingIntent
    PendingIntent piSent = PendingIntent.getBroadcast(context, 0,
                               new Intent("aSuaPackage.SMS_SENT"),
                               PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent piDelivered = PendingIntent.getBroadcast(context, 0,
                                    new Intent("aSuaPackage.SMS_DELIVERED"),
                                    PendingIntent.FLAG_UPDATE_CURRENT);

    //Preenche os arrays
    for(int i=0; i<messagesCount; i++){
        sentIntents.add(piSent);
        deliveryIntents.add(piDelivered);
    }

    smsManager.sendMultipartTextMessage(fone, null, messages,
                                        sentIntents, deliveryIntents);
}

Para registar os receivers use:
registerReceiver(SentReceiver.getInstance(), new IntentFilter("aSuaPackage.SMS_SENT"));
registerReceiver(DeliveredReceiver.getInstance(), new IntentFilter("aSuaPackage.SMS_DELIVERED"));

Comment: Só para incrementar a resposta,  nunca caia no catch pois "try catch" são adicionados para tratar exceções em tempo de execução. Nesse caso não caia no catch porque não tinha erro de execução. E como o ramaral explicou faltou adicionar os pendingIntent para ser notificado dos listener de estado de entrega do sms.
Comment: @AlessandroBarreto Obrigado pela nota.
Comment: Muito obrigado, era exatamente isso, resolveu o meu problema.


Matching the size of the flattened convolution layer with the 1st FC layer size

Question: Suppose that the size of the last convolution layer or max pooling layer in a neural network was NxNxM. What would be the size of the corresponding flattened layer? I thought it would be, well, 1x1x(N*N*M).
Looking at the VGG16 network, and I'm a bit confused now. The last max pooling layer of VGG16 is 7x7x512, but the drawing seems to indicate that the flattened layer is 1x1x4096.
Maybe I misread the drawing, maybe the flattened layer is 1x1x(7*7*512)=1x1x25088, and 1x1x4096 is the 1st FC layer? I doubt that: full connectivity from 25088 to 4096 neurons would require 102760448 weights (not including bias).
Could you clarify for me what I'm seeing wrong there?
Answer: The drawing doesn't include the flattening operation. The first FC layer has 4096 units, and as you calculated the layer before it has an output size of 7 x 7 x 512 = 25,088 units, so that would require just over 100 million weights between the flattened output of the last max-pooling layer and the first FC layer.
Can this be possible? In the original paper introducing VGG16, they mention a total number of parameters around 140 million (see their Table 2). By my calculation, all the conv layers together use around 15 million parameters. The last two FC layers contribute around 20 million more parameters (4096 x 4096 + 4096 x 1000), so that leaves just over 100 million, which corresponds exactly to the number of parameters we calculated for the first FC layer.


Como mudar a escala dos mapas no ggplot2?

Question: estou fazendo um mapa com o ggplot e ggmap.
Porém, as escalas dos eixos estão em graus decimais, mas eu preciso delas em graus, minutos e segundos.
Agradeço pela colaboração!
Answer: Eu vi o seu post na versão em inglês e consegui algum avanço. Existe a função dms do pacote GEOmap que transforma decimal em graus. Segue código da minha tentativa.
library(ggplot2)
library(ggmap)
library(GEOmap)

#get my map
city<- get_map(location = c(lon= -54.847, lat= -22.25),
               maptype = "satellite",zoom = 11,color="bw")

map<-ggmap(city,extent="normal")+
  xlab("Longitude")+ ylab("Latitude")

scale_x_longitude <- function(xmin=-180, xmax=180, step=1, ...) {
  xbreaks <- seq(xmin,xmax,step)
  xlabels <- unlist(
    lapply(xbreaks, function(x){
      ifelse(x < 0, parse(text=paste0(paste0(abs(dms(x)$d),"^{o}*"),
                               paste0(abs(dms(x)$m),expression(~minute)), "*W")),
             ifelse(x > 0, parse(text=paste0(paste0(abs(dms(x)$d),"^{o}*"),
                                             paste0(abs(dms(x)$m),expression(~minute)),"*E")),
                    abs(dms(x))))}))
  return(scale_x_continuous("Longitude", breaks = xbreaks, labels = xlabels, expand = c(0, 0), ...))
}

scale_y_latitude <- function(ymin=-90, ymax=90, step=0.5, ...) {
  ybreaks <- seq(ymin,ymax,step)
  ylabels <- unlist(
    lapply(ybreaks, function(x){
      ifelse(x < 0, parse(text=paste0(paste0(abs(dms(x)$d),"^{o}*"),
                                      paste0(abs(dms(x)$m),expression(~minute)), "*S")),
             ifelse(x > 0, parse(text=paste0(paste0(abs(dms(x)$d),"^{o}*"),
                                             paste0(abs(dms(x)$m),expression(~minute)),"*N")),
                    abs(dms(x))))}))
  return(scale_y_continuous("Latitude", breaks = ybreaks, labels = ylabels, expand = c(0, 0), ...))
}

map +
  scale_x_longitude(-55.0,-54.7,.1) +
  scale_y_latitude(-22.4,-22.1,.1)

Comment: Muito obrigado, Rafael! Iria comentar que não havia conseguido adicionar os valores dos minutos... Vou continuar tentando... qualquer coisa subo a atualização dos códigos. Valeu!
Comment: Consegui adicionar o símbolo `'` de minutos, mas não ficou 100% porque ele tá um pouco distante do número. Utilizei a função `expression(~minute)`. O código e a figura estão atualizadas
Comment: Excelente, Rafael!


Erro no Netbeans usando Java: "error: diamond operator is not supported in -source 1.5"

Question: Estou tentando usar um List de Lists em Java no NetBeans pela primeira vez:
List<List<String>> listao = new ArrayList<>();

Mas ele apresenta um erro de compilação na linha onde eu tenho a lista:

error: diamond operator is not supported in -source 1.5

E apresenta a dica:

use -source 7 or higher to enable diamond operator

Ok, mas que source é esse, e como fazer isso?
Comment: A dica é clara, voce precisa usar uma versao do jdk 7 ou superior pra usar operador diamante. Sua versao é 1.5 nao suporta.
Comment: Relacionada: [Declaração de ArrayList com o tipo ou sem](https://pt.stackoverflow.com/q/287805/28595)
Answer: Primeiramente, clique com o botão direito do mouse no seu projeto pra ver o menu suspenso da figura abaixo, onde você vê o item "Propriedades" ou "Properties":

Ao abrir as propriedades, altere a caixinha do formato do código-fonte para JDK 7 ou maior:


Duplicar coluna de uma tabela para ela mesma alterando somente o ID

Question: Bom dia a todos, estou com uma duvida e não achei nada semelhante que possa me ajudar, preciso criar uma função de copiar e colocar ou seja duplicar tudo o que esta de informação e gera um novo ID mantendo as informações.
Estou fazendo um sistema de listagem e controle de projetos para a empresa na qual eu trabalho, segue tabela na qual preciso que os campos sejam duplicados.
CREATE TABLE `tb_projetos` (
      `id` int(11) NOT NULL,
      `empresa` varchar(128) NOT NULL,
      `nome` varchar(128) NOT NULL,
      `tipo` varchar(128) NOT NULL,
      `localidade` varchar(128) NOT NULL,
      `aceitacao_projeto` varchar(128) NOT NULL,
      `prazo_projeto` varchar(128) NOT NULL,
      `mub_colaborador` varchar(128) NOT NULL,
      `mub_inicio` varchar(128) NOT NULL,
      `mub_fim` varchar(128) NOT NULL,
      `lev_colaborador` varchar(128) NOT NULL,
      `lev_inicio` varchar(128) NOT NULL,
      `lev_fim` varchar(128) NOT NULL,
      `dig_colaborador` varchar(128) NOT NULL,
      `dig_inicio` varchar(128) NOT NULL,
      `dig_fim` varchar(128) NOT NULL,
      `cal_colaborador` varchar(128) NOT NULL,
      `cal_inicio` varchar(128) NOT NULL,
      `cal_fim` varchar(128) NOT NULL,
      `art_trt` varchar(128) NOT NULL,
      `engenheiro` varchar(128) NOT NULL,
      `doc_colaborador` varchar(128) NOT NULL,
      `doc_data` varchar(128) NOT NULL,
      `doc_fim` varchar(128) NOT NULL,
      `rev_colaborador` varchar(128) NOT NULL,
      `rev_data` varchar(128) NOT NULL,
      `doc_envio_colaborador` varchar(128) NOT NULL,
      `doc_envio_envio` varchar(128) NOT NULL,
      `doc_envio_entrega` varchar(128) NOT NULL,
      `doc_envio_aprovacao` varchar(128) NOT NULL,
      `protocolo_ei` varchar(128) NOT NULL,
      `situacao` varchar(128) NOT NULL,
      `concessionaria` varchar(128) NOT NULL,
      `observacao` varchar(256) NOT NULL,
      `adicionado` date NOT NULL,
      `modificado` date DEFAULT NULL
    )

fazendo alguns teste, cheguei nesta formula que ao testar no bd funciona mas na interpretação do codigo ele não executa corretamente.
include('../../class/db_conexao.php');  $id = 0;
$conexao->query("CREATE TEMPORARY TABLE tmp SELECT * FROM tb_projetos WHERE id=$id;") or die($conexao->error);
$conexao->query("UPDATE tmp SET id=$id+1 WHERE id = $id;") or die($conexao->error);
$conexao->query("INSERT INTO tb_projetos SELECT * FROM tmp WHERE id=$id+1;") or die($conexao->error);

Comment: suponha que a tabela tenha só "id, empresa, nome", faria um `insert into tabela (id, empresa, nome) select 1 as novoid, empresa, nome from tabela` simples assim, basta por todos os campos e o where que precisar e claro mudar o valor de "novoid", por exemplo "id + 1000 as novoid"
Comment: oi, tudo bom.
fiz um teste aqui com um exemplo na internet e coloquei direto pelo mysql e funcionou mas tentei interpretar no codigo e ele não gera a copia.

`include('../../class/db_conexao.php');

$id = 0;
$conexao->query("CREATE TEMPORARY TABLE tmp SELECT * FROM tb_projetos WHERE id=$id;") or die($conexao->error);
$conexao->query("UPDATE tmp SET id=$id+1 WHERE id = $id;") or die($conexao->error);
$conexao->query("INSERT INTO tb_projetos SELECT * FROM tmp WHERE id=$id+1;") or die($conexao->error);`
Answer: Vou usar uma versão simplificada da tabela original para exemplificar a solução. A solução para a tabela final é a mesma, acresentandos os restantes campos.
A vantagem desta abordagem é que não é necessário uma tabela intermédia. Mesmo a solução de usar uma tabela intermédia pode ser feita usando uma tabela temporária - através da sintaxe WITH - que tem a vantagem de não a instanciar "fisicamente" na DB.
Vamos à solução que me parece a mais simples:
CREATE TABLE `tb_projectos` (
  `id` int(11) NOT NULL,
  `empresa` varchar(128) NOT NULL,
  `nome` varchar(128) NOT NULL,
  `tipo` varchar(128) NOT NULL
);

O resultado pretendido obtêm-se da seguinte forma:
INSERT INTO tb_projectos
SELECT
  max_id.id + src.id,
  src.empresa,
  src.nome,
  src.tipo
FROM tb_projectos src
  JOIN (SELECT MAX(ID) + ABS(MIN(ID)) + 1 AS ID FROM tb_projectos) max_id ON 1 = 1;

O novo indice é calculado somando o maior valor do campo id, com o módulo do menor existente na tabela à data da inserção e o valor inicialmente atribuído ao campo id. A este valor adiciona-se 1 para garantir que não existe colisão mesmo que o indice mais pequeno seja 0.
Uma pergunta: Porque não usar a coluna id como chave auto-incrementada?
Dessa forma, a preocupação do índice único fica entregue à DB e o insert de replicação fica ainda mais simples:
INSERT INTO tb_projectos (empresa, nome, tipo)
SELECT empresa, nome, tipo
FROM tb_projectos;

A tabela teria de ser definida atribuindo a propriedade AUTO_INCREMENT ao campo id e promovê-lo a chave primária.
CREATE TABLE `tb_projectos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `empresa` varchar(128) NOT NULL,
  `nome` varchar(128) NOT NULL,
  `tipo` varchar(128) NOT NULL,
  PRIMARY KEY (id)
);


Issue, iteration cross KV (NoSql/Redis) Target

Question: I would like to iterated cross KV (NoSql,Redis) Target in MLRun without specification of primary key(s) also, but I saw only ability to get specific KV item(s) based on specific key(s). I have to use only unique items, not duplicit items.
I chose NoSql/Redis Target because key operations in-memory are critical for quick responce (based on specific key), but from time to time I have to iterate cross whole collection of keys (and it is case of this question).
You can see part of my code for getting values based on keys:
import mlrun
import mlrun.feature_store as fs
...

svc=fs.get_online_feature_service(fs.FeatureVector("myVector", ["client.*"]))
resp = svc.get([{"cuid": 1926, "key1": 100023}])

Do you know, how to iterate cross items in KV (NoSql, Redis) target in MLRun CE/Paid editions (version 1.2.1)?
Comment: how to ask a good question:
https://stackoverflow.com/help/how-to-ask
Comment: What is going wrong?
Comment: @D.L, do you see issues with this question?
Answer: The NoSqlTarget is using a KV store which is inherently for retrieving an individual value based on a given key. If you are looking to retrieve the entirety of your feature vector, you should use the offline store instead. More info in the MLRun docs here
Alternatively, you could use the offline store to get a list of keys and then query the online store using those:
import mlrun.feature_store as fstore

df = fstore.get_offline_features("my-vec").to_dataframe()
keys = list(resp.to_dataframe().index.unique())

svc = fstore.get_online_feature_service("my-vec")
svc.get([{"my-key" : key} for key in keys[:5]])

Comment: My recommendation is the same

```python
import mlrun.feature_store as fstore

df = fstore.get_offline_features("my-vec").to_dataframe()

keys = list(resp.to_dataframe().index.unique())

svc = fstore.get_online_feature_service("my-vec")
svc.get([{"my-key" : key} for key in keys[:5]])
```
Comment: I have to use unique items (based on items rewriting) and based on that offline store such as parquet is not useful.
Comment: The ParquetTarget includes all records that have been ingested. This includes all versions of the items that you are writing. You can pass in a given time range to specify what you are looking for

Alternatively you can use offline store to get your list of keys to iterate over
Comment: Is it possible to iterate cross NoSqlTarget also?
Comment: No the KV source allows you to get one value for a given key. You need to provide the key for an efficient lookup. If you want a list of all the keys you can use the ParquetTarget and use that to iterate over

Not entirely sure what you're trying to do - not much to go off of in your question. If you provide a little more information on your use case there may be a better solution
Comment: I updated original question for better clarification.
Comment: It is little overkill for me to keep whole history in parquet, but thanks for this useful solution. Please can you mention your solution directly in answer. If I understand it correctly, there is no chanse to iterate via NoSqlTarget (therefore Parquet).
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253039/discussion-between-nick-schenone-and-jzd).
Answer: I am using these solutions for iteration cross keys in KV storage:
1. NoSqlTarget
Iteration via v3io (it is not pure part of MLRun API, but it is part of MLRun distribution packages). More information about v3io python SDK see and iteration cross KV items (cursor usage) see sample
import v3io.dataplane

v3io_client = v3io.dataplane.Client(endpoint='https://v3io-webapi:8081', access_key='some_access_key')

# create a query, and use an items cursor to iterate the results
items_cursor = v3io_client.kv.new_cursor(container='users',
                                         table_path='/user-profile',
                                         attribute_names=['income'],
                                         filter_expression='income > 150')

# print the output
for item in items_cursor.all():
    print(item)

BTW: NoSqlTarget is available only for MLRun Enterprise edition
2. RedisTarget
You can use easy iteration cross KV items, it is part of Redis API
import redis

r = redis.StrictRedis(host='localhost', port=6379, db=0)
for key in r.keys('*'):
    r.delete(key)

It is possible to use commandline also via redis-cli see sample:
redis-cli keys users*

or remove from redis specific keys based on list of keys:
redis-cli keys users* | xargs redis-cli del

BTW: RedisTarget is available for MLRun CE and Enterprise editions
Comment: Looks great thanks! Want to note that the V3IO option is only for Iguazio enterprise whereas the Redis option works on MLRun Community Edition
Comment: Can you mention also redis-cli command?
Comment: @JzD, I added sample from Redis


Filter |raw doesn't show entity media image

Question: Using the following code for displaying content from a WYSIWYG field:
content.field_body|raw I only got the text without images added in a editor as media. When I remove filter raw I see <drupal-entity data-embed-button="media_browser" data-entity-embed.... is being displayed as a text but not as image.
Comment: This happens in paragraph, I forgot to mention.
Comment: probably the text filter for embedded media is missing, check if "Display embedded entities" is checked in the text format configuration
Comment: "Display embedded entities" is checked in rich text configuration.
Comment: then you need to make sure that the text format is applied to the field, it seems it is not
Comment: @4k4 how can I check that?
Comment: if you have a body field in another place, such as in a content type, that works normally, you could compare what is different
Comment: @4k4 If I use text long field in content type works correctly, if I use text long field in paragraph component it doesn't show.
Comment: why would you use |raw on content.field_body? I'd recommend you include the whole template. Are the paragraph and node using the same text format?
Comment: @Berdir I want to show the content from that field in a div, I don't want to show the whole paragraph because of my HTML structure.
Comment: This may be related with the issue. I have a paragraph Accordion that contains another paragraph for accordion items as a child. The content from that child elements is not displaying correctly.
Comment: `content.field_tabs[0]` returns the whole paragraphs with all fields, content body field is called **field_content** and when I try to access to its full content with the following `content.field_tabs[0]['#paragraph'].field_content` got only the text from the body field.
Answer: This output from the body field
<drupal-entity data-embed-button="media_browser" data-entity-embed....

is the raw data stored in the database. The tag <drupal-entity> gets replaced by the text filter "Display embedded entities" when the field is rendered.
If you access the field data directly from the entity #paragraph you bypass this rendering process.
Instead of the raw filter, which doesn't help in any way, it only makes your site vulnerable to the content of the body field, you can apply the text format in a render element processed_text:
  {% set paragraph =  content.field_tabs.0['#paragraph'] %}

  {% set body = {
    '#type':   'processed_text',
    '#text':    paragraph.field_body.value,
    '#format':  paragraph.field_body.format,
  } %}

  {{ body }}

Comment: Right/Perfect/Satisfying approach and the Drupal way!
Comment: If someone looking to implement in the module file

`$quiz_summary = $node->get('field_paragraph')->referencedEntities();`

Field_paragraph is the name of paragraph field. Then get a preferable body field from paragraph.

`$score_summary = [
    '#type' => 'processed_text',
    '#text' => $paragraph->get('field_summary_text')->getValue()[0]['value'],
    '#format' => $paragraph->get('field_summary_text')->getValue()[0]['format']
  ];`

Then use it like

`render($score_summary)`


Should we split each state/contract by each cordapp

Question: With the contract upgrade in mind, is it better to split each state and contract to its own cordapp/module so it exist in its own jar when deployed?
Hence during upgrade, we can upgrade only the affected states - instead of have N number of states in one shared-cordapp, and when only one state is upgraded, we have to upgraded all other states as well.
Answer: Suppose you initially deploy the following CorDapps on your node:

CorDapp 1, containing OldState1, OldState2, OldContract1, OldContract2
CorDapp 2, containing OldState3, OldState4, OldContract3, OldContract4

After running the node for a while, you need to upgrade OldState1 to NewState1, and OldContract1 to NewContract1.
To do so, you create a new CorDapp JAR containing NewState1 and NewContract1 and deploy this on your node. You can then initiate the upgrade process to transition the OldState1 instances using OldContract1 to instances of NewState1 using NewContract1.
In other words, although your original CorDapp contained two states and two contracts, the CorDapp containing the upgrades only needs to contain one state and one contract. There is therefore no advantage to splitting each contract and state into its own CorDapp as you describe.


Erro ao tentar efetuar um Relacionamento belongsToMany no Laravel

Question: Estou tentando efetuar um relacionamento belongsToMany em minha Model mas está trazendo o erro:

QueryException
      SQLSTATE[42S02]: Base table or view not found: 1146 Table 'api.codigo' doesn't exist (SQL: select sequenciaaprovacaos.*, codigo.sequencia as pivot_sequencia, codigo.sequenciaaprovacao_id as pivot_sequenciaaprovacao_id from sequenciaaprovacaos inner join codigo on sequenciaaprovacaos.id = codigo.sequenciaaprovacao_id where codigo.sequencia in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51))

Minhas Models
namespace App;

use Illuminate\Database\Eloquent\Model;

class SequenciaUsuario extends Model
{

    protected $table = 'sequencia_usuarios';

    protected $fillable = [

        'sequencia',
        'recnum',
        'usuario',
        'qtde_aprova',
        'ordem'
    ];

    public function grupos()
    {
     return $this->belongsToMany('App\Model\CadSequenciaAprovacao', 'codigo','sequencia');
    }

    public function cadastro()
    {
        return $this->belongsToMany('App\Model\CadUsuario','cod_adm');
    }
}

namespace App;

use Illuminate\Database\Eloquent\Model;

class CadSequenciaaprovacao extends Model
{
    protected $table = 'sequenciaaprovacaos';

    protected $fillable = [
        'codigo',
        'descricao',
    ];

    public function users()
    {
        return $this->hasMany('App\CadSequenciaUsuario','sequencia');
    }

}

Comment: `$this->belongsToMany('App\Model\CadSequenciaAprovacao', 'codigo','sequencia');` aqui te está errado, qual é o nome da tabela intermediária! tem como colocar o diagrama de banco?
Comment: Troquei de belongsToMany para hasMany parou de dar o erro!
Answer: Existem algumas opções que você pode testar. Como você não está usando o nome padrão da tabela (vide necessidade de informar o atributo $table), é interessante que você informe o relacionamento passando todos os parametros aceitos pelo Eloquent para esse método.
return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
no qual

App\Role coincide com seu model;
role_user coincide com a sua tabela; user_id coincide com a sua
fk na tabela e PK na sua tabela que relaciona;
role_id coincide com a sua fk na tabela que é PK na tabela com a qual você quer relacionar;

Para mais informações, veja aqui a documentação neste ponto.
Espero ter ajudado.


Showing $\gcd(n!i+1,n!j+1) = 1$ for $n \in \mathbb{N}$ if $i$ and $j$ are integers with $1\leq i < j \leq n$

Question: For $n$, a positive integer, and integers $i,j$ with $1\leq i < j \leq n$, I wish to show that $\gcd(n!i+1,n!j+1) = 1$.
I have shown that $\gcd(n!i+1,n!j+1) = \gcd(n!i+1, n(j-i)$), which I believe is important.  I have a hunch that the proof involves the fact that $b|a \implies b\not{|}(a+1)$, and have shown this as a lemma.  May I have a hint as to how to proceed?
Comment: Hint: $n(j-i) \mid n!i$, hence any factor of $n(j-1)$ divides $n!i$ as well. Now use the fact that you quoted (which holds for $b \neq 1$).
Comment: Why does $n(j-i) | n!i$?
Comment: By the assumptions on $i,j$, we have $1 \leq j-i <n$. So $i-j$ is a factor of $(n-1)!$. Thus $n(j-1)$ is a factor of $n!$.
Comment: This hint was enough for me to solve it.  Thanks!
Answer: $\begin{align}{\bf Hint}\ \ \ (im\!+\!1,jm\!+\!1) \,&=\, (im\!+\!1,(j\!-\!i)m)\ \ \, {\rm by}\ \  (x,y) = (x,\,y\!-\!x)\\[.3em]
 &=\, (im\!+\!1,\,j\!-\!i)\ \ \ \ \ \ \ \ \,{\rm by}\ \  (im\!+\!1,m) = 1\\[.3em]
 &=\, 1\ \ {\rm if}\ \ j\!-\!i\mid m \\
\end{align}$
Answer: Well you can subtract the smaller term for the larger.
$\gcd(n!i+1, n!j+1)= \gcd(n!i+1, (n!j+1)-(n!i+1))=\gcd(n!i + 1, n!(j-i))$
Now $n!|n!i$ so $n!i + 1$ and $n!$ are relatively prime.
So $\gcd(n!i+1, n!(j-i)) = \gcd(n!i + 1, j-i)$.
And $1 < j-i < n$ so $(j-i)|n!$ so $j-i$ is relatively prime to $n!i+1$
.....
Two lemmas:
1)  $\gcd(am + 1, a) = 1$.
2) If $\gcd(a,m) = 1$  then $\gcd(a, bm) = \gcd(a,b)$.
Comment: This is an exact dupe of the argument posted in my answer 33 minutes prior. Please don't duplicate prior answers.
Comment: You waited two *years* to respond to this?
Comment: I didn't wait. Rather, I only saw this later answer two years later when searching for dupes.
Answer: For any prime $p \leq n$ both $n!i + 1, n!j + 1$ are $\equiv 1 \ \text{mod} \  p$. On the other hand, for any prime $p>n$, it cannot be that $p$ divides both $n!i + 1, n!j + 1$, since then it would divide their difference $n!(j-i)$, which is impossible since $j-i \leq n$.


Is there a way to add a dynamic URL token to images so that the move from local host to live doesn't require code change?

Question: Is there a way to add a dynamic URL token to images so that the move from local host to live doesn't require code change?
When uploading images in content, the path changes form localhost to live, how can I have it consistent so the database can be exported without changes?

I would like to leave out the name of the site in the path
Answer: Handling of the links in the field having a WYSIWYG editor, or any other long text field is a common issue when the site moves from one server (say dev) to other (say stage or production).
The links should be saved in such a way that they do not break.
Pathologic module does that for you.

Pathologic is an input filter which can correct paths in links and images in your Drupal content in situations which would otherwise cause them to "break;" for example, if the URL of the site changes, or the content was moved to a different server. Pathologic can also solve the problem of missing images and broken links in your site's RSS feeds. See more example use cases on the documentation page.
Comment: True, I totally forgot thos module, the backend life :D.  If I remember it had some limitations, but it's very useful.
Answer: AFAIK there's no such token. Even if it exists it will be hard to use: think in all places that the token should be used and token must be processed: inside content texts, link fields, even variables.
You should to use the same path for all enviroments.
If you really need different paths (for example, to hide a name) I suggest you to use a symlink in one of the environments: For example, if your local path is /NAME/path and live path is /NAME-TO-HIDE/path you can create a symlink that points from /NAME/ to /NAME-TO-HIDE/ in the live site. Nobody will see the NAME-TO-HIDE string but webserver will reach the right files thanks to the symlink.
Comment: Ok, I will just name the site the same thing, what I have been doing in the past is naming sites like localhost/site1  localhost/testing  localhost/sandbox ect...I guess not good practice
Comment: Aquia Dev Desktop appends DD to every new site URL on local host ..I can't use the same path for all environments...or is there an Aquia setting?
Comment: I don't use Acquia Dev Desktop, but I'm not sure if I understand your problem. I don't know what you mean when you say 'appends DD to new site URL on localhost'.. and is strange that Acquia don't allow you to use the same internal path . You should edit your question given your setup with all details.
Comment: I'll email Acquia, I mean this with Acquai: when you create a new site, instead of having a URL path like localhost/example (XAMPP), it give you something like example:dd:8083, it puts :DD suffix on all new sites
Comment: It's seems that they change the domain, no the path part of the URL (the path inside the server).
Comment: When you upload images, the DD is appended. Adding images on live site URL is:  /testing/sites/default/files/content_images/left.png    adding the same image on Localhost URL is:  /sites/xuzo.dd/files/content_images/left.png , now I don't know why folder structure is not the same on live and local host...it's the same on both in FTP, sites/default/files/content_images


How to calculate days difference between two dates using struts2 tags in JSP

Question: I have two dates in my JSP. I want to calculate days difference between the dates using Struts2 tags. Can someone help me out?
Comment: Do the diff part inside your controller.
Comment: I did amount calculations  using struts2 tags but dates also is there , i am not getting
Comment: I do apologize, but I do not understand your comment.
Comment: sorry,In my  jsp page i am iterating list that contains date value .i want to compare that date with current date and that difference , i want to show
Comment: Could you post the relevant parts of your JSP and your controller?  Thank you.
Comment:
Comment: Don't post a code in comment, append it to the question.
Answer: You can use JSTL tags like that :
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

....

<fmt:parseNumber var="daysDifference"
                value="${( date1.time - date2.time ) / (1000*60*60*24) }"
                integerOnly="true" />


optimizing auc vs logloss in binary classification problems

Question: I am performing a binary classification task where the outcome probability is fair low (around 3 per cent). I am trying to decide whether to optimize by AUC or log-loss. As much as I have understood, AUC maximizes the model's ability to discriminate between classes whilst the logloss penalizes the divergency between actual and estimated probabilities. In my task is extremely important to calibrate the precision accuracy. So I would choose logloss, but I wonder whether the best log-loss model should also be the best AUC / Gini models.
Answer: As you mention, AUC is a rank statistic (i.e. scale invariant) & log loss is a calibration statistic. One may trivially construct a model which has the same AUC but fails to minimize log loss w.r.t. some other model by scaling the predicted values. Consider:
auc <-  function(prediction, actual) {
  mann_whit <- wilcox.test(prediction~actual)$statistic
  1 - mann_whit / (sum(actual)*as.double(sum(!actual)))
}

log_loss <- function (prediction, actual) {
  -1/length(prediction) * sum(actual * log(prediction) + (1-actual) * log(1-prediction))
}

sampled_data <- function(effect_size, positive_prior = .03, n_obs = 5e3) {
  y <- rbinom(n_obs, size = 1, prob = positive_prior)
  data.frame( y = y,
              x1 =rnorm(n_obs, mean = ifelse(y==1, effect_size, 0)))
}

train_data <- sampled_data(4)
m1 <- glm(y~x1, data = train_data, family = 'binomial')
m2 <- m1
m2$coefficients[2] <- 2 * m2$coefficients[2]

m1_predictions <- predict(m1, newdata = train_data, type= 'response')
m2_predictions <- predict(m2, newdata = train_data, type= 'response')

auc(m1_predictions, train_data$y)
#0.9925867
auc(m2_predictions, train_data$y)
#0.9925867

log_loss(m1_predictions, train_data$y)
#0.01985058
log_loss(m2_predictions, train_data$y)
#0.2355433

So, we cannot say that a model maximizing AUC means minimized log loss. Whether a model minimizing log loss corresponds to maximized AUC will rely heavily on the context; class separability, model bias, etc. In practice, one might consider a weak relationship, but in general they are simply different objectives. Consider the following example which grows the class separability (effect size of our predictor):
for (effect_size in 1:7) {
  results <- dplyr::bind_rows(lapply(1:100, function(trial) {
                                    train_data <- sampled_data(effect_size)
                                    m <- glm(y~x1, data = train_data, family = 'binomial')
                                    predictions <- predict(m, type = 'response')
                                    list(auc = auc(predictions, train_data$y),
                                         log_loss = log_loss(predictions, train_data$y),
                                         effect_size = effect_size)
                                  }))
  plot(results$auc, results$log_loss, main = paste("Effect size =", effect_size))
  readline()
}

Comment: What does your model estimate? Log loss only makes sense if you're producing posterior probabilities, which is unlikely for an AUC optimized model. Rank statistics like AUC only consider relative ordering of predictions, so the magnitude of gaps between predictions is irrelevant; clearly this is not the case for probabilities. Any scaling you perform on your AUC optimized predictions will have to address this issue. Furthermore, this only addresses calibration of your predictions towards a reasonable posterior estimate, not global minimization of LL, as outlined in this post.
Comment: You might be interested in [platt scaling](https://en.wikipedia.org/wiki/Platt_scaling) as a starting point.
Comment: Very informative answer. In you are answer there are two predictions, with same AUC but very different log loss.  So I come to this question : I have trained a model for optimizing AUC. But later I realized that I need to go for log loss. I cant afford to retrain the model for log loss (which should be ideal case). Can I apply any transformation on the predictions, so that it has the best log loss. (The log loss considred here is binary meaning, reference probability is either 1 or 0).
Comment: I understand its not global. I just want to setup a transformation which might have a parameter and then play around with it.
Comment: In the second plot, isn't it just showing a log curve limit? So why don't you rather plot log-log or lin-lin?
Answer: For imbalanced labels, area under precision-recall curve is preferable to AUC (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4349800/ or python scikit-learn docs)
Also, if your goal is to maximize precision, you can consider doing cross-validation to select the best model (algorithm + hyperparameters) using "precision" as the performance metric.
Comment: I downvoted for a few reasons here.  1) You should cite either a source or a reason for your first statement.  2) How exactly do you optimize for precision?  Wouldn't that create a degenerate model?
Comment: ok thanks for helping me make a better answer. I added 2 references. and how do you optimize for precision? just like any other metric. You just specify "precision" as your scoring function, for example in scikit-learn. Precision is a metric like accuracy, AUC, etc
Comment: Yep, I now see how "optimize" may be confusing. Not sure how OP meant it. For me, it's like you say: tune hyperparameters via CV so precision is maximized. And I think that's how you apply it in sklearn as well.
Comment: Sure, I think your answer would be improved here if you added that clarification.  A bit off topic, but I actually think sklearn is quite poor at supporting that, since it tunes based on a fixed classification threshold, which I would argue is quite bad practice.
Comment: I have an issue with F0.5,F1, and F2 measure because like accuracy its biased to the majority class when data is imbalanced.
Comment: Ahh, sure.  But I think when people read "optimize" they are going to assume it's during the training of your model.  I think sklearn gets this pretty wrong, since it uses a fixed classification threshold, and you should really be tuning that with cross validation.
Comment: downvote because auc is not appropriate when you have imbalanced data. F1 is the metric to go.
Comment: ... so why did you downvote me, when that is exactly what I say? I think you meant to downvote the question not my answer?? @user4581
Comment: In order to optimizer for precision, you need to choose a threshold or recall rate that you want. Otherwise it doesn't make sense what you meant by optimizing for precision. Thus, what you are looking to optimize is **Precision at recall**, not just precision.
Comment: @chancdn Do you have a reference? Can you write a full answer? How are you defining precision at recall?
Comment: @jtlz2 Check example API from tensorflow: https://www.tensorflow.org/api_docs/python/tf/keras/metrics/PrecisionAtRecall


Java repeat String with str.repeat

Question: I have a problem with a fairly simple question in Java. To repeat a string, you can use str.repeat from Java 11. However I have the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method repeat(int) is undefined for the type String

The code is as follows:
String rep = "-";
rep = rep.repeat(4);
System.out.println(rep);

With a command-line compilation, no problem, but Eclipse is problematic, it uses
java-11-openjdk-amd64

Thanks for your help
Comment: try running this in eclipse: `System.out.println(System.getProperty("java.vm.version"));`
Comment: Look at the 'Java Compiler' settings in the project Properties and make sure it is set to Java 11 or above. Look at the 'Java Build Path' in the Properties and make sure you are using a Java 11 or above JRE/JDK.
Comment: Are you sure you're using java 11 for the project your code is in?  Also, what version of eclipse are you using?  Older versions don't support newer versions of Java. Even if you've got eclipse pointing to the proper JDK, it might not have 11 available as a compiler compliance level.
Comment: Thanks for your answers, these were the configuration files that eclipse generated, it was not using the right version of Java, despite the message displayed.
Answer: repeat is Java 11 keyword. You are trying to use repeat from a lower version than Java 11.
modify the code with for loop as below.
 String rep = "";
 for(int i=0;i<4;i++) {
    rep += "-";
 }
 System.out.println(rep);

Java repeat String with str.repeat


File download not working on firefox

Question: I have written this piece of code which is working perfect on Google Chrome and Opera but not working on Firefox
function onSaveJPG(url,n){
    var save = document.createElement('a');
    save.href = url;
    save.target = '_blank';
    save.download = 'Image no '+n+'.jpeg' || url;
    var event = document.createEvent('Event');
    event.initEvent('click', true, true);
    save.dispatchEvent(event);
    (window.URL || window.webkitURL).revokeObjectURL(save.href);
}

What is wrong? please guide me.
Comment: Most of that is deprecated, most browsers now support the [event constructor](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Events/Creating_and_triggering_events)
Comment: You have to append the save element to body
Comment: As a sidenote, to trigger the download, all you really have to do is `save.onclick();`
Answer: This should work (I figured it out by looking through the FileSaver.js code):
function onSaveJPG(url,n){
    var save = document.createElement('a');
    save.href = url;
    save.download = 'Image no '+n+'.jpeg' || url;
    var event = document.createEvent("MouseEvents");
        event.initMouseEvent(
                "click", true, false, window, 0, 0, 0, 0, 0
                , false, false, false, false, 0, null
        );
    save.dispatchEvent(event);
}

(the main problem is that you need to use a MouseEvent type event for firefox rather than an Event. This code will also work on Chrome).

File download not working on firefox


Microsoft Exchange rejecting logins

Question: I have Microsoft Exchange 2010 running on Windows 2008R2 - 64 Bit.
I had Microsoft Outlook 2007 clients connected and logging in just fine.  Some time has passed and now my outlook clients are not able to connect.  I've tried removing and recreating outlook login and when I do so and open outlook and try to expand the Inbox related to the Exchange server I get the following error:
The set of folders cannot be opened.  Microsoft Exchange is not available.  Either there are network problems or the Exchange computer is down for maintenance.
There are no network problems.  Everthing is on same network switch and outlook can ping the Exchange server by hostname and FQDN.  Again, this was all working.  I do have Norton antivirus and tried disabling that.
I went down several rabbit holes on this one.  I read an article about chimney effect/bug, default gateway bug for outlook (registry fix) and none of these things corrected my issue.
Lastly, I tried setting up outlook without using offline files and without using Exchange cache mode.  Under the account set up under Control Panel -> Mail, under the General tab I changed the connection to "Manually control connection state" and from automatic and set it to "Connect with network".  Under same dialog box but under Advanced, I removed the check mark next to "Use cached Exchange mode" and clicked the offlinne folder file settings and disabled offline use.
I still get strange behavior.  Now when I run outlook and try to expand the Inbox related to exchange, I get:
The set of folders cannot be opened.  You must connect to Microsoft Exchange with the current profile before you can synchronize your offline folder file.
I also deleted all .OST and .PST files and restarted outlook with no luck.
Any help is greatly appreciated.
Comment: Is OWA working without issues?
Comment: Great question/test, Yes OWA works.  I'm able to login with a typical user account and see their Exchange Inbox.
Comment: Have you rebooted the server?
Comment: yes, tried rebooting several times.
Comment: Anything interesting in the event log? Also, how's Active Directory - stable and working? You didn't specify, but I assume by your referring to it as "the" server it's the only Exchange server - is that the case?
Answer: Run Test-ServiceHealth in the Exchange Management Shell to check whether all required services are started. If any are not started, start them and see if the problem has gone away.
Try logging onto Outlook Web App to see if the problem persists there, or whether it's just Outlook playing up.
Have you applied any patches recently (Exchange, Windows, Outlook) that might be causing problems (unlikely it is that a Windows Update will cause problems, it's always worth checking).
Comment: Thanks, that helped fix it.  The ExchangeRPC and ServiceHost were not running as were a couple others.  I guess I over looked this when I rebooted the box and didn't see any Event logs pertaining to failed services.  The Test-SErviceHealth help me identify required services that were not running.

Microsoft Exchange rejecting logins


AWS CloudFormation IAM policy and SQS policy seems to have similar settings, but not sure why?

Question: I'm new to AWS CloudFormation and I'm trying to figure out some pre-existing CloudFormation JSON. I've read through the documentation many times, but I seem to end up with more questions than answers.

Below is a modified version of my CloudFormation file. I've tried to strip out as much as I can from the CloudFormation example to hopefully reduce the visual noise. I appreciate that readers wont know the reasoning for why certain things are done, but I'm hoping my questions (below) will highlight a more fundamental issue with how the CF has been written.

The CF JSON creates an SQS Queue, an SQS Queue Policy and an IAM policy.
The SQS Queue Policy allows complete API access to the SQS Queue (it also restricts access to certain ip addresses via a Condition) and appears to assign those permissions to the Queue itself.
But we also have an IAM policy which appears to do a similar thing? It assigns specific SQS Queue permissions, but this time to a Role rather than to the Queue itself like the SQS Queue Policy seems to be doing.
My question is why the duplication? Could we not just have the access permissions applied to the SQS Queue Policy and just remove the "QueuesPolicy" IAM policy?
I can kind of understand why the IAM policy "QueuesPolicy" would set SQS Queue access permissions: because you could have multiple roles accessing a single queue and you want them to have a different set of permissions (rather than the Queue having a concrete set of permissions for all roles). But if that's the case then why have permissions set on the queue as well? Does that look like a mistake or used as some kind of "fallback"?
I would also assume we could keep the SQS Queue Policy but just remove the API permissions and keep the ability to restrict its own access to certain ip addresses. Would that work in this example?
Also, the AWS::IAM::Role confuses me because I'm not really sure I understand what it's doing. It seems to suggest that the EC2 instance will have the ability to assume the FooRole, is that correct? I guess as we have a single set of AWS credentials, that an application running on the EC2 instance may well use the credentials to gain access to our AWS account but as there is no individual "users" we would need to have a role created and the EC2 instance have access to that role for a request (such as sending a message to the queue from the application) to be authorised?
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "EC2ComponentPolicy": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "EC2ComponentPolicy",
        "PolicyDocument": {
          "Statement": [
            {
              "Action": [
                "cloudformation:Describe*"
              ],
              "Resource": [
                "*"
              ],
              "Effect": "Allow"
            },
            {
              "Action": [
                "ec2:Describe*"
              ],
              "Resource": [
                "*"
              ],
              "Effect": "Allow"
            }
          ]
        },
        "Roles": [
          {
            "Ref": "FooRole"
          }
        ]
      }
    },
    "SQSFooQueue": {
      "Type": "AWS::SQS::Queue",
      "Properties": {
        "MessageRetentionPeriod": 86400,
        "VisibilityTimeout": {
          "Ref": "VisibilityTimeout"
        }
      }
    },
    "ComponentInstanceProfile": {
      "Type": "AWS::IAM::InstanceProfile",
      "Properties": {
        "Path": "/",
        "Roles": [
          {
            "Ref": "FooRole"
          }
        ]
      }
    },
    "SQSFooQueuePolicy": {
      "Type": "AWS::SQS::QueuePolicy",
      "Properties": {
        "Queues": [
          {
            "Ref": "SQSFooQueue"
          }
        ],
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Id": "SQSFooQueuePolicy",
          "Statement": [
            {
              "Resource": [
                {
                  "Fn::GetAtt": [
                    "SQSFooQueue",
                    "Arn"
                  ]
                }
              ],
              "Effect": "Allow",
              "Sid": "Allow-User-SendMessage",
              "Action": [
                "sqs:*"
              ],
              "Condition": {
                "IpAddress": {
                  "aws:SourceIp": [
                    "xxx.xx.xxx.x/xx",
                    "xxx.xx.xxx.x/xx",
                    "xxx.xx.xxx.x/xx"
                  ]
                }
              },
              "Principal": {
                "AWS": "*"
              }
            }
          ]
        }
      }
    },
    "QueuesPolicy": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "QueuesPolicy",
        "PolicyDocument": {
          "Statement": [
            {
              "Action": [
                "sqs:AddPermission",
                "sqs:ChangeMessageVisibility",
                "sqs:ChangeMessageVisibilityBatch",
                "sqs:CreateQueue",
                "sqs:DeleteMessage",
                "sqs:DeleteMessageBatch",
                "sqs:DeleteQueue",
                "sqs:GetQueueAttributes",
                "sqs:GetQueueUrl",
                "sqs:ListQueues",
                "sqs:ListDeadLetterSourceQueues",
                "sqs:ReceiveMessage",
                "sqs:RemovePermission",
                "sqs:SendMessage",
                "sqs:SendMessageBatch",
                "sqs:SetQueueAttributes"
              ],
              "Resource": [
                {
                  "Fn::GetAtt": [
                    "SQSFooQueue",
                    "Arn"
                  ]
                }
              ],
              "Effect": "Allow"
            }
          ]
        },
        "Roles": [
          {
            "Ref": "FooRole"
          }
        ]
      }
    },
    "FooRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "Path": "/",
        "AssumeRolePolicyDocument": {
          "Statement": [
            {
              "Action": [
                "sts:AssumeRole"
              ],
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "ec2.amazonaws.com"
                ]
              }
            }
          ]
        }
      }
    }
  }
}

Comment: Seems the `Statement` block *must* have an `Action` key, which would explain why in the SQS Queue Policy it has included what seems to be a duplicate of the Actions. BUT someone explained to me yesterday that the `Condition` is effectively saying "restrict access to the SQS Queue resource to these set of ips and let these ips have full access"
Comment: I also had a similar question and found this documentation to be helpful: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-using-identity-based-policies.html
Answer: I'll try to take your questions in order, but first give my general interpretation of this snippet. This might be a template for a AWS-based application that's going to run under an instance profile tied to the role in the snippet, and will use SQS to integrate with some off-cloud system in known IP ranges. Presumably it'll be updated to use the SDK, but obviously can't get temp credentials quite as easily as via the metadata endpoint.

The queue policy and the IAM policy resource-reference the same queue, but they don't appear to be redundant. One's for anonymous users (which an IAM policy can't accomplish), the other for your EC2 instances.
You may expand the queue policy to cover both apparent needs, but I personally prefer the principal-centric IAM policies. Likewise I'd use legacy S3 control options, for example, only when you can't use an IAM policy. But it's mainly a preference except where you must use a resource-tied policy.


How to do a for loop of filenames with spaces using find

Question: I have the following script - its supposed to loop files names using find but it seams to break the files names up by a space? I need the file names to remain intact
#!/bin/bash

for file in `find -name "*.avi"`
do
./myscript $file
done

Can anyone help me?
Thanks
Comment: See [BashFAQ #020: How can I find and deal with file names containing newlines, spaces or both?](http://mywiki.wooledge.org/BashFAQ/020/).
Comment: possible duplicate of [Linux command to remove directories with white spaces](http://serverfault.com/questions/385474/linux-command-to-remove-directories-with-white-spaces)
Comment: I updated the question to be more accurate of what I want to do
Answer: Also: find -name "*.avi" -exec ./myscript '{}' \;
Womble's answer definitely works in the majority of cases, but Xargs isn't perfect with spaces across all implementations. I've never found find's 'exec' mode to break. Solaris was particularly cranky in that respect.
Obviously, the '-print' would be better for simply generating output for a list of files in a for loop. But if you're running a command on a list of files, it's more efficient to just run the '-exec' option.
Answer: find -name "*.avi" -print0 | xargs -0 -n 1 echo

You don't have to echo in xargs:
find -name "*.avi" -print0 | xargs -0 -n 1 ./myscript

Comment: This is another way to output the filename - I need to loop file names to another script (in the example above I replaced the command with echo)
Comment: So show us what you actually want to do then.  We can't help you do what you want to do if you don't tell us what you want to do.
Answer: If you don't need recursion:
#!/bin/bash

for file in *.avi
do
    ./myscript "$file"
done

Note that the quotes around the variable are essential.
Edit:
As kojiro points out, if you need recursion and you have Bash 4, you can add shopt -s globstar and change the glob to **/*.avi.
Comment: @hayalci: No, you are *completely* wrong. It works perfectly with such filenames and without any security risks. Globbing with a `for` loop is the *correct* way to do this when recursion is not needed.
Comment: If a file name has spaces, this method will break. If it has \n or other strange characters, it is also a security risk. Think of "/tmp/file\n/etc/shadow"
Comment: If he does need recursion and has bash 4, he can just use `shopt -s globstar; for file in **/*.avi`
Comment: @kojiro: Thanks, I should have mentioned that and I will add it to my answer.
Comment: @DennisWilliamson: You are right, I confused the issue with "for i in \`ls *.avi\`" case
Answer: In addition to the solutions based on xargs and the find -exec there is also the option of piping to while read. It has the benefit of allowing you to structure your script in a more classic loop manner. Let me apply it to your example.
find -name "*.avi" | while read avifile; do
  ./myscript "$avifile"
done

Comment: By default `read` will support escaping, so you need `-r`. And your solution will break with filenames containing newlines.
Comment: `-r` is not needed for spaces, but in case a backslash appears in the filename.
Comment: @Pierre: I have so far never encounted a regular space situation where I have needed the -r parameter (to read?), but thanks for the pointer, I'll look into it. Agree that this solution would break on newline. Assumed the original questioner only dealt with the regular space character. When expecting arbitrary whitespace, or any other really crazy characters I agree on xargs -0 being the better choice. Well, either that or doing something more robust in a language such as Perl, Python, etc.


Custom Field Plugin With Image Field

Question: I have been working on a custom field plugin with which the aim is to create a custom field to be used for content and will consist of a link field and a file field through which a thumb image is to be uploaded. I'm ok with the first part is i.e. the link field for which I have declared a uri data definition with a varchar storage schema and a url type for the form element. But I cannot (based on the available documentation) figure out the proper way to manage the file field. I have used a managed_field type as for the form element but cannot figure out what is the correct datatype and storage schema to use.
Edit: The documentation I have found deals with three parts,

The field class where the propertyDefinitions method is overridden and the properties are defined (ex. $properties['link'] = DataDefinition::create('uri')->setLabel(t('Video Link'));)
Also in the field class where the schema method is overridden and the storage properties are defined (ex. $columns['link'] = [ 'type' => 'varchar', 'length' => 255, ];)
the widget class where the formElement method is overridden to define the input form (ex. $element['link'] = [
        '#type' => 'url',
        '#title' => t('Link'),
        '#default_value' => isset($items[$delta]->link) ? $items[$delta]->link : null,
        '#empty_value' => '',
        '#placeholder' => t('Insert a url'),
    ];)

what should the equivalent parts for an image/file field property be defined?
any input would be appreciated, thanks
Comment: "To me, your new field is basically a image field with an extra link field", slightly modified quote from https://stackoverflow.com/questions/38996037/drupal-8-field-plugin-with-field-type-managed-file/39036241#39036241
Answer: I hope I understood correctly:
FileItem.php

Have a look at public static function defaultStorageSettings() and public static function schema(FieldStorageDefinitionInterface $field_definition)
Comment: Hi Dumitru, do you mean that I should be extending the FileItem instead of the  FieldItemBase class? I have edited my initial question in order to give more details  thanks for your answer
Comment: Considering the fact that you want to bundle the file field and the link field, perhaps you should consider the Paragraphs module which @Hudri mentioned, unless you need it done custom. If you need it to be custom-made, then think about providing a field which actually contains both of the fields. This would mean having code like `$properties['CUSTOM_LINK_FIELD']['link'] = DataDefinition::create('uri')->setLabel(t('Video Link'));`.
Answer: Why don't you try the Paragraphs module? This looks like a simple combination existing of field types, which is exactly Paragraphs' point.

Custom Field Plugin With Image Field


Suggestions on group SMS implementation on iOS 10+?

Question: I just found out that carriers limit number of recipients of SMS to 10. As a result in my app I am not able to SMS a group of people consisting of more than 10 people. At least, I can not do this easily and user friendly. I am aware of a couple of apps that do similar thing (GroupMe for example) do they go through their server to actually send out an SMS? Can anyone recommend a technical solution around this problem? Anyway to solve it in-app? In app I do have all the phone numbers that are to be messaged and user is not involved in selecting them individually.
Answer: Sounds like the only way to do this is through the use of SMS gateway either posting from the app or more probably a better idea through app->myserver->sms gateway.
Restriction is AT&T specific it seems as it seems to work fine on Verizon.


.Net Bitmap class constructor (int, int) and (int, int, PixelFormat) throws ArgumentException on perfectly good arguments

Question: I have some code that does something like this (Irrelevant bits snipped):
void foo(Bitmap bmp1, Bitmap bmp2)
{
    Bitmap bmp3;
    if(something)
        bmp3 = new Bitmap(bmp1.Width, bmp1.Height + bmp2.Height);
    else
        bmp3 = new Bitmap(bmp1.Width, 18000);
    (more stuff here that runs fine)
}

Anywho most of the time this ran fine. At first. As the project continued it started to fail on the new Bitmap line. The error it gives is: "ArgumentException was unhandled. Parameter is not valid." There's no mention of which parameter it has a problem with or anything. I'm stumped. Here's what I know for sure:

bmp1 and bmp2 have never been null
when this error is thrown.
The if statement's presence has
never made a difference; it dies
just as frequently without.
Both examples of the constructor use
have thrown this error.

I'm tempted to say this is a memory error, except it doesn't mention anything of the sort. The first dozen times or so this happened the heights totalled over 18000 (hence the magic number above). Figuring it was some kind of soft barrier to our system, we just limited the images at that height which made the exceptions go away after a while.
For some sample data, the exception I'm looking at right now has bmp1.Width at 2550, bmp1.Height at 6135 and bmp2.Height at 6285.
Anyone have any ideas?
Answer: GDI+ does not generate very good exception messages.  The exception you got is flaky, this one will generate it reliably on my machine:
    private void button1_Click(object sender, EventArgs e) {
        var bmp = new Bitmap(20000, 20000);
    }

What's really going on is that this bitmap requires too much contiguous unmanaged memory to store the bitmap bits, more than is available in your process.  On a 32-bit operating system, you can only ever hope to allocate a chunk of memory around 550 megabytes.  That goes quickly down-hill from there.
The issue is address space fragmentation, the virtual memory of your program stores a mix of code and data at various addresses.  Total memory space is around 2 gigabytes but the biggest hole is much smaller than that.  You can only consume all memory with lots of small allocations, big ones fail much quicker.
Long story short: it is trying to tell you that the size you requested cannot be supported.
A 64-bit operating system doesn't have this problem.  Be sure to take advantage of it with Project > Properties > Build tab, Platform target = AnyCPU and Prefer 32-bit = unticked.  Also, WPF relies on WIC, an imaging library that's a lot smarter about allocating buffers for bitmaps.
Comment: Actually, I am having this same problem debugging with VisualStudio on Widows7 64bit. So a 64bit OS does not entirely prevent it.
Comment: Change the Platform target setting of your EXE project to AnyCPU so you'll actually take advantage of a 64-bit address space.
Comment: This, coupled with Kyralessa's link, pretty much explained the problem. Now I just have to fix the memory leak. :( Thanks guys!
Answer: Check whether bmp1 or bmp2 has been Disposed (even if not null).  See here:
The Mysterious Parameter Is Not Valid Exception
Answer:
Anyone have any ideas?

Wrap the call that is throwing ArgumentException with a try-catch(Exception ex) and step into the exception block to see the raw exception.  It should give you more detail, like which argument is supposedly invalid.
try
{
    bmp3 = new Bitmap(bmp1.Width, bmp1.Height + bmp2.Height);
}
catch (Exception ex)
{
    throw;  // breakpoint here, examine "ex"
}

Comment: Right. Should've mentioned that I've tried this. It gives me almost no more information. There's no inner exception, the message (in it's entirety) is "Parameter is not valid.", source is labelled as "System.Drawing", and the stack trace has "at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)" at the top level. Not sure what else I could get from the exception.
Comment: That's interesting.  And by *interesting* I mean, "May you lead an interesting life"-interesting.  Two of the `ArgumentException` constructors accept a string parameterName, and it's *interesting* that the `Bitmap` constructor is not using it when clearly it's available.
Comment: Any solution for this problem?
Comment: @FabianoLothor, the accepted answer above seems to explain it pretty well.  I.e. the exception is poor, but the problem is set in stone: not enough contiguous memory available for the size of bitmap you're attempting to allocate.
Comment: I solved my problem by using a different class of iTextSharp. I am now using the wkhtmltopdf project.


Retorno completo do tipo Date

Question: Como faço para obter esse retorno?
2014-08-05 18:29:47.757
Utilizando o Date.
Date data = new java.sql.Date(new java.util.Date().getTime());

Utilizando isso meu retorno é apenas 2015-02-03 e não retorna as horas.
OBS.: preciso obter o valor assim para passar como timeStamp para o banco.
Answer: Você precisa pedir a informação toda explicitamente com Calendar e formatá-la com SimpleDateFormat:
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.S").format(Calendar.getInstance().getTime())

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Na edição da pergunta (que depois foi removida) diz que precisa do timestamp, aí tem outra solução mais simples pegando direto o timestamp:
System.currentTimeMillis()

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Dcoumentação.
Note que estamos falando de formatação da apresentação da data e hora. Internamente um tipo date armazena toda a informação sempre. Se vai usar um dado deste tipo, ele sempre terá toda precisão. O fato de você não ver é um problema de apresentação apenas.
Comment: Não acho que o comentário valha uma resposta em separado, porém gostaria de apontar que o Java 8 introduziu novas  classes como [`DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) e [`LocalDateTime`](http://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html). A ideia é que as pessoas aos poucos comecem a utilizar essas novas APIs.
Comment: eu tinha olhado isso, porém resta uma dúvida isso gera uma String eu consigo converter em Date?
Comment: É possível, claro, mas precisa ver o que você quer fazer com ela, porque pode não ser preciso. Ter um retorno com o formato que você deseja é diferente de ter a data como você quer. A representação interna do tipo `Date` não interessa para você quando está usando no seu programa e sim como usá-la da maneira que precisa. No código exemplo que você colocou você diz que tem um retorno, mas este código não retorna nada visível. A apresentação do valor é diferente do valor. Mas editei para responder o que você perguntou na edição. Acho que é o que realmente você quer. Ou a pergunta está toda errada
Comment: Acredito que eu possa ter confundido vocês ao editar minha pergunta. O que está acontecendo é eu preciso gravar nesse padrão no banco **2014-08-05 18:29:47.757**.
Comment: Obrigado pelo link @bigown.
Comment: Se você quer gravar no banco de dados em um formato específico, você fará isto em um tipo de texto, talvez um `varchar`, então você gravará uma *string* nele. O primeiro exemplo que eu coloquei revolverá para você. Se você quer gravar data, não pode especificar formato, mesmo no banco de dados a representação interna é problema do tipo de dado. Formato só faz sentido em apresentação, em em armazenamento. A não ser que você queria armazenar a apresentação. Mas se quer a data corrente no banco de dados mande ele fazer isto e não o Java.
Comment: Entendi, acredito que minha coluna ser um @dateTime resolva se eu passar o currentTimeMillis, como na documentação ele fala que retorna a data atual em millisegundos.
Comment: É uma possibilidade. Depende do que você quer. Mas se o seu problema é pegar a data atual para por no banco de dados, o mais correto é deixar o banco de dados colocar tal informação independente do Java. Se não sabe como fazer isto abra uma pergunta para o banco de dados que você usa. Novamente, não sei sua situação específica, então posso estar enganado.
Comment: Vamos [continuar esta discussão no bate-papo](http://chat.stackexchange.com/rooms/20802/discussion-between-wellington-avelino-and-bigown).
Comment: Finalmente entendi, fiz como você falou e deu certo!
debuguei e vi o que não estava explicito no retorno, mesmo sem ir para o **sysout** ele atribui o valor corretamente a variavel. Obrigado @bigown
Answer: Você pode usar o SimpleDateFormat:
String dataFormatada = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(new Date());

Para converter de volta para Date:
Date data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(dataFormatada);

Comment: Tentei utilizar dessa forma, estou utilizando uma propriedade InicioOcorrencia no meu BackBean do tipo Date, e seto **ocorrencia.setInicioOcorrencia(dataformatada)** quando o retorno acontece ele fala que não consegue trabalhar com String.
Comment: @WellingtonAvelino Qual o tipo de parâmetro que esse método aceita?
Comment: Tipo Date segue o erro **The method setInicioOcorrencia(Date) in the type Ocorrencia is not applicable for the arguments (String)**
Comment: @WellingtonAvelino Então você tem que passar o objeto `Date` mesmo. O método que tem que cuidar de formatar a saída da forma que ele precisar.
Comment: Uma dúvida eu devo utilizar **java.sql.Date** ou **java.util.Date** no meu BackingBean?
Sei que essas dúvidas são coisas simples, mas estou num momento critico, sou estágiario e estou sozinho na equipe :(
Comment: @WellingtonAvelino Dá uma olhada nessa resposta do SO em inglês: [java.util.Date vs java.sql.Date](http://stackoverflow.com/a/2306051/4443230)
Comment: Muito obrigado pela ajuda @André Ribeiro,me ajudou bastante entendi como funciona o Date, agora é só evoução!

Retorno completo do tipo Date


How to access custom config variable in codeigniter controller

Question: I have a codigniter project with a custom config file called
application/config/my_config_variables.php:

This contains
<?php
$config['days'] = 20;

in :
 application/config/autoload.php

I've added:
$autoload['config'] = array('my_config_variables');

when I try to access this in my controller using:
 echo $this->$config['new_daily_contacts'];

I get:
Fatal error: Cannot access empty property.

What am I doing wrong?
addendum:
In my controller I've added:
 // An alternate way to specify the same item:
            $my_config = $this->config->item('my_config_variables',true);
            var_dump($my_config);

this outputs FALSE -- why?
Comment: What version of Codeigniter are you using?
Comment: CI 2.1 -- Thanks for looking at it.
Comment: https://stackoverflow.com/a/62427655/9573341
Answer: Look at the config documentation on Codeigniter.
echo $this->config->item('new_daily_contacts');

That will try to grab $config['new_daily_contacts'] but from your post it looks like you only have $config['days'] in your config file. But this should get you pointed in the right direction.
Alternate Way
With the alternate way you're trying to load the config, but you're using the item method, not the load method. On the item method, the 2nd parameter is the index you want to reference in the config array. For instance. With your example above.
$my_config = $this->config->load('my_config_variables', true);
var_dump($my_config);
$days = $this->config->item('days', 'my_config_variables');

Because passing true as the 2nd parameter on the load method will create an index with the same name as the config file. This helps avoid any conflicts if there happen to be other config files that contain the same config name.
$config['days']; becomes $config['my_config_variables']['days']
In order to access that config variable you have to pass the index in the item method.
https://github.com/bcit-ci/CodeIgniter/blob/2.1-stable/system/core/Config.php#L189
Comment: I've tried to follow this but still receiving errors - please see above:
Comment: Updated the answer with your alternate way.
Comment: Thanks for your help, $this->config->item('new_daily_contacts'); worked. BTW, I wasn't sure bwhat is meant by index, is this a key in an associative array, like $config['key'] ?
Comment: Ya, Codeigniter calls it an index in their documentation but it's a key. Same thing.


Extract file using sftp based from a masterlist

Question: Is it possible to extract certain files based from a master list via sftp.
Example:
directory containts ff files.
aa.txt
bb.txt
cc.txt

masterlist.txt containts:
aa.txt
bb.txt

Files that should be extracted
aa.txt
bb.txt

Edit:
Thanks @shellter for your feedback.
I did try to write my own code but wasn't able to find samples that I could work on(I'm not a unix person btw).
Anyhow, as for your suggestion regarding using while-read-line, I've tried it but I am getting Invalid command error.
#!/bin/ksh
file=MasterList.txt

while IFS= read -r line
do
echo "fetching $line"
sftp firstname.lastname@example.com
cd /data/EP_files/balex
get "$line"
bye
done <"$file"

Lastly, if my masterfile containts 10k list of files, is this kind of approach ok performance wise?
Thank you
Comment: welcome to stackOverflow. But contrary to what you seem to believe, S.O. isn't a free coding service. You're expected to attempt to solve your problem with code. When you encounter a problem with you code, then you should post a Q in the form of http://stackoverflow.com/help/mcve.  Please learn to search S.O. with basic keywords. I know http://stackoverflow.com/questions/5554843/how-to-improve-this-ftp-shell-function is not the only Q that covers automation of `ftp` files. Search also for `while read line done` for examples for reading your master list and using that for your func. Good luck.
Answer: You need to create the entire sequence of SFTP commands - including the individual get commands for all files in the input list up front, and then invoke sftp only once, passing the command list via stdin (standard input):
#!/usr/bin/env ksh

file=MasterList.txt

sftp -b - firstname.lastname@example.com <<EOF
cd /data/EP_files/balex
$(sed -n 's/^file_.*/get "&"/p' "$file")
bye
EOF

The <<EOF ... EOF block is a a so-called here-document, which allows passing multiline strings (optionally with embedded variable references and commands) via stdin.
sed -n 's/^file_.*/get "&"/p' "$file" embeds a  get command for each filename in $file that starts with file_, ignoring any other lines (as requested by the OP in a comment).
The above assumes that your sftp utility accepts a list of commands in "batch" mode via the -b option via stdin (-).
Comment: @user2058738: Please see my update; please try to add such requirements to your question up front in the future.
Comment: This worked exactly as I needed. Thank you very much!
Comment: Thank you very much. This worked very well. One question, is there a way to add a pattern in sed? like I only need to extract the files in the list that starts with "file_".
Comment: Thank you for your update. I tried but it did not exclude the lines that does not start with "file_"
Comment: @user2058738: My bad: I neglected to exclude non-matching lines from the `sed` command - please see my update, which adds `sed` option `-n` - to suppress printing all lines by default - and the `p` flag to the `s` function - which only prints (the modified versions of) lines that start with `file_`.


How to calculate the a Probability with Z-Score.

Question: I have a $ \mu = -1  $ and $ \theta = 6 $. I am supposed to find the probability $P(5 < X < 11)$.
My Attempt:
$$P(5< X < 11) $$
$$P(\frac{5--1}{6} < \frac{X--1}{6} < \frac{11--1}{6}) $$
$$ P(1 < Z < 2) $$
Now what? How do I calculate the Z-score without a table? Do I just integrate the normal probability density function (I tried that... I'm not sure if I am doing it wrong or what)?
btw, the answer the professor gave was:
$$P(1 < Z < 2) = .1359 $$
Answer: Given a CDF $F_X$, $P(a< X \leq b)=F(b)-F(a)$. You'll need a table or a calculator or something. Trying to integrate the PDF of the standard normal distribution will only bring you sadness.
Comment: Welp, that explains a lot. I don't have a scientific or graphing calculator, or the textbook with the tables (can't afford them), so no wonder I'm having trouble with normal distributions and z-scores. Thanks for the help.
Comment: @Chase: You can find tables freely available on the internet. You can even find calculators that lets you calculate the CDF of any normal distribution at specific points.
Comment: @Chase although, I would recommend sticking with tables if you are using tables in your class. You want to get really efficient at using tables for exam purposes. Tables are, of course, freely available online.
Comment: For one table, [see this.](http://en.wikipedia.org/wiki/Standard_normal_table)
Comment: @crf Funny thing, I don't think she ever mentioned a table in class exams. I think she expects us to use a calculator. I don't have one, so I'm guessing that if I can show all the correct work (basically everything right up until a calculation) and show what's going on I will do fine. Well, I hope.
Comment: Also, if anyone is still here, how does one do the same for any other type of distribution? Just integrate? What if say, for Binomial distributions, I have n =64, p =.2 and x = 1.16?
Comment: @Chase This is a separate question and maybe you should pose it separately, but the answer that I gave you is general: if you have a CDF then you have everything. If the distribution is *continuous* with PDF $f_X$ then the CDF is $F_X(x)=\int_{-\infty}^xf_X(t)dt$. The binomial distribution is not continuous, so that won't help you in this case, although the idea is the same. For a discrete distribution with probability *mass* function $p_X$, $F_X(x)=\sum_{y\leq x}p_X(y)$. Sometimes (in the binomial case for instance), this can be tough to put into a nice closed form.


How can I loop through all article content types and get the content of each

Question: I created a new article content and am overriding that by creating a template for it (ie: node--4.html.twig).
Then inside this template, I want to loop through all nodes that belong to article content type; and to achieve that, I created my .theme file and added the following code based on the answer for this question here:
<?php
  function capoeira_preprocess_page(&$variables) {
  $articles = \Drupal::entityQuery('node')->condition('type', 'article')->execute();
  $nodes = \Drupal\node\Entity\Node::loadMultiple($articles);
  $variables['articles'] = $nodes;
}

Inside the template I do have:
{{ kint(articles) }}

inside my template and I go refresh the page I show article as NULL.
How do I loop through the articles?
EDITED:
None of the below work:
function capoeira_preprocess_page(&$variables) {
  $articles = \Drupal::entityQuery('node')->condition('type', 'article')->execute();
  $nodes = \Drupal\node\Entity\Node::loadMultiple($articles);
  $variables['name'] = $nodes;
}

function capoeira_preprocess_node(&$variables){
  $variables['name'] = 'My name comes here';
}

function capoeira_preprocess_node_article(&$variables){
  $variables['name'] = 'My name comes here';
}

Comment: Leaving out the fact a theme should not have code that queries entities, as that is code for a module, did you check the theme to which you added that code is the theme used for the page? Are you using the correct machine name in those hooks? The fact you show a function with a name starting with `scottishleader_` and two with a name starting with `capoeira_` makes me wonder you are prefixing the function names with the wrong theme machine name.
Comment: It's also not clear why, for every page Drupal shows with that theme, you would show a list of nodes or data obtained from that list of nodes.
Comment: @kiamlaluno , the themename is capoeira, inside it, I have a file named capoeira.theme, which has the preprocess functions. I want to add the functionality for my node--4.html.twig template, which is used to show all the news(news are from article content type). Hope I could explain better
Comment: You should also show the code you are using in the template file to loop through the list of nodes. If that code is wrong, we cannot tell you how to fix it without seeing it.
Comment: Then, as usual, if you are editing the code of an enabled theme, you need to first clear Drupal cache, or Drupal will not notice there are new hooks in the code. That, or first disable the theme, before adding new hooks.
Comment: Why do you need to loop through all of these in the 1st place? This is going to slow ur page load as the number of articles grow...
Comment: @NoSssweat it will only slow the page if one decides to show 100 articles
Comment: You could just create a block using views and either assign it to the page (via block layout and visibility settings) or embed the block entity as a reference field in the node itself. This eliminates much of the preprocessing and likely can eliminate a one-off node template.
Answer: you can use
function YOURTHEME_preprocess_node_article(&$variables){
  $node = $variables['node'];
   $nid = $node->id();
   // similarly you can fetch other fields
}

then override article content type as node--article.html.twig
and pass data to twig file
Comment: I initially had HOOK_preprocess_node and the result was the same. I now tried to modify the template name as per your answer but still no success.
Comment: @deSousa see updated answer
Comment: I just edited my question. My understanding is that you choose the name after "preprocess" based on the template name in usage, whether its a page, node or node type. If so is true, then using soccer_preprocess_node should work because my template name is node--4.html.twig, which redundantly speaking its node. Anyway, nothing is working for some odd reason.


sfc /scannow win 2003 R2

Question: I have win 2003 r2 when i'm trying to launch sfc /scannow an utility runs but closes in a couple of minutes. In event journal there are no errors. chkdsk shows no errors. I establish connection to this server via rdp with /admin /console keys.
Additionally on this server an utility mstsc has stopped to work, launch of this utility does nothing. (That's why i have encountered the problem with sfc)
Any suggestions?
Answer: Try running it in safe mode. can you let us know if that helps? its possible that something in the full boot is crashing it, especially if your OS is corrupt
Comment: Thanks. In the safe mode i got that sfc cannot be run due to inaccessibility of rpc server. I tried to start Remote Procedure Call (RPC) Locator service and got a notification that it's impossible to run this service in safe mode.
Comment: There is a workaround for that somewhere, but I dont know it personally. Honestly, I have always considered sfc /scannow to be the "I dont know the problem, maybe Ill get lucky fix". It may be  best to let us know why you have to run this command and we can try to help you that way
Comment: briefly i want to make mstsc utility works. Currently it just shows nothing when i'm trying to start it.
Answer: Boot the system with server 2003 CD and repair your existing Windows installation.   This method does not delete any configuration and applications.
https://decipherinfosys.wordpress.com/2007/02/16/effectively-repairing-windows-2003-server/

sfc /scannow win 2003 R2