问:
apply new theme without reloading the charts in highcharts
Can I apply theme without reloading the whole chart. Can I push the themes settings within the chart code? In highcharts site all examples are single theme based. Here is my code

```<code>$(function() {
            $.getJSON('http://api-sandbox.oanda.com/v1/candles?instrument=EUR_USD&candleFormat=midpoint&granularity=W', function(data) {
        // create the chart
        var onadata =[];
        var yData=[];
        var type='line';
        var datalen=data.candles.length;
        var all_points= [];
        var all_str="";

        for(var i=0; i<datalen;i++)
        {
            var each=[Date._parse(data.candles[i].time), data.candles[i].openMid, data.candles[i].highMid, data.candles[i].lowMid, data.candles[i].closeMid]
            onadata.push(each);
            yData.push(data.candles[i].closeMid);
        }

                $( "#change_theme" ).on("change", function() {
                var optionSelected = $("option:selected", this);
                var valueSelected = this.value;

                //alert(valueSelected);
                if(valueSelected=='default.js')
                {
                    location.reload();
                }
                else{                       $.getScript('js/themes/'+valueSelected, function() {
                    //alert('Load was performed.');
                    chart();
                });
                }

            });

        chart();

        function chart()
        {
            $('#container').highcharts('StockChart', {

                    credits: {
                        enabled : 0
                    },

                    rangeSelector : {
                        buttons: [{
                            type: 'month',
                            count: 1,
                            text: '1M'
                        }, {
                            type: 'month',
                            count: 3,
                            text: '3M'
                        },{
                            type: 'month',
                            count: 6,
                            text: '6M'
                        },{
                            type: 'all',
                            text: 'All'
                        }],
                        selected:3
                    },

                    legend: {
                        enabled: true,
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'middle',
                        borderWidth: 0
                    },

                    title : {
                        text : 'Stock Price'
                    },

                    xAxis :{
                        minRange : 3600000
                    },

                    yAxis : [{
                          offset: 0,
                          ordinal: false,
                          height:280,

                          labels: {
                            format: '{value:.5f}'
                                }

                     }],

                    chart: {
                        events: {
                            click: function(event) {
                                var x1=event.xAxis[0].value;
                                var x2 =this.xAxis[0].toPixels(x1);
                                var y1=event.yAxis[0].value;
                                var y2 =this.yAxis[0].toPixels(y1);

                                selected_point='['+x1+','+y1+']';
                                all_points.push(selected_point);
                                all_str=all_points.toString();
                                if(all_points.length>1)
                                {

                                    this.addSeries({
                                    type : 'line',
                                    name : 'Trendline',
                                    id: 'trend',
                                    data: JSON.parse("[" + all_str + "]"),
                                    color:'#'+(Math.random()*0xEEEEEE<<0).toString(16),
                                    marker:{enabled:true}
                                    });

                                }

                                if(all_points.length==2)
                                {
                                    all_points=[];
                                }

                            }

                        }
                    },

                    series : [{
                        //allowPointSelect : true,
                        type : type,
                        name : 'Stock Price',
                        id: 'primary',
                        data : onadata,
                        tooltip: {
                            valueDecimals: 5,
                            crosshairs: true,
                            shared: true
                        },
                        dataGrouping : {

                            units : [
                                [
                                    'hour',
                                    [1, 2, 3, 4, 6, 8, 12]
                                ], [
                                    'day',
                                    [1]
                                ], [
                                    'week',
                                    [1]
                                ], [
                                    'month',
                                    [1, 3, 6]
                                ], [
                                    'year',
                                    [1]
                                ]
                            ]
                        }
                    },
                    ]
                });
        }

    });
});
</code>```

and this is my js fiddle

Please help. Thanks in advance.

答:
This is possible if you're using modern browsers that support CSS variables.

```<code>Highcharts.theme = {
    colors: [
      'var(--color1)',
      'var(--color2)',
      'var(--color3)',
      'var(--color4)',
      'var(--color5)',
      'var(--color6)',
    ]
}

Highcharts.setOptions(Highcharts.theme);

function setTheme(themeName) {
  // remove theme-* classes from body
  removeClasses = Array.from(document.body.classList).filter(s => s.startsWith('theme-'));
  document.body.classList.remove(...removeClasses)

  if (themeName) {
    document.body.classList.add('theme-' + themeName);
  }
}
</code>```

CSS

```<code>body {
  --color1: #e00;
  --color2: #b00;
  --color3: #900;
  --color4: #600;
  --color5: #300;
  --color6: #000;
}

body.theme-dark {
  --color1: #555;
  --color2: #444;
  --color3: #333;
  --color4: #222;
  --color5: #111;
  --color6: #000;
}

body.theme-retro {
  --color1: #0f0;
  --color2: #ff0;
  --color3: #0ff;
  --color4: #0a0;
  --color5: #aa0;
  --color6: #00a;
}
</code>```

Body:
Can I apply theme without reloading the whole chart. Can I push the themes settings within the chart code? In highcharts site all examples are single theme based. Here is my code

$(function() {
            $.getJSON('http://api-sandbox.oanda.com/v1/candles?instrument=EUR_USD&candleFormat=midpoint&granularity=W', function(data) {
        // create the chart
        var onadata =[];
        var yData=[];
        var type='line';
        var datalen=data.candles.length;
        var all_points= [];
        var all_str="";

        for(var i=0; i<datalen;i++)
        {
            var each=[Date._parse(data.candles[i].time), data.candles[i].openMid, data.candles[i].highMid, data.candles[i].lowMid, data.candles[i].closeMid]
            onadata.push(each);
            yData.push(data.candles[i].closeMid);
        }

                $( "#change_theme" ).on("change", function() {
                var optionSelected = $("option:selected", this);
                var valueSelected = this.value;

                //alert(valueSelected);
                if(valueSelected=='default.js')
                {
                    location.reload();
                }
                else{                       $.getScript('js/themes/'+valueSelected, function() {
                    //alert('Load was performed.');
                    chart();
                });
                }

            });

        chart();

        function chart()
        {
            $('#container').highcharts('StockChart', {

                    credits: {
                        enabled : 0
                    },

                    rangeSelector : {
                        buttons: [{
                            type: 'month',
                            count: 1,
                            text: '1M'
                        }, {
                            type: 'month',
                            count: 3,
                            text: '3M'
                        },{
                            type: 'month',
                            count: 6,
                            text: '6M'
                        },{
                            type: 'all',
                            text: 'All'
                        }],
                        selected:3
                    },

                    legend: {
                        enabled: true,
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'middle',
                        borderWidth: 0
                    },

                    title : {
                        text : 'Stock Price'
                    },

                    xAxis :{
                        minRange : 3600000
                    },

                    yAxis : [{
                          offset: 0,
                          ordinal: false,
                          height:280,

                          labels: {
                            format: '{value:.5f}'
                                }

                     }],

                    chart: {
                        events: {
                            click: function(event) {
                                var x1=event.xAxis[0].value;
                                var x2 =this.xAxis[0].toPixels(x1);
                                var y1=event.yAxis[0].value;
                                var y2 =this.yAxis[0].toPixels(y1);

                                selected_point='['+x1+','+y1+']';
                                all_points.push(selected_point);
                                all_str=all_points.toString();
                                if(all_points.length>1)
                                {

                                    this.addSeries({
                                    type : 'line',
                                    name : 'Trendline',
                                    id: 'trend',
                                    data: JSON.parse("[" + all_str + "]"),
                                    color:'#'+(Math.random()*0xEEEEEE<<0).toString(16),
                                    marker:{enabled:true}
                                    });

                                }

                                if(all_points.length==2)
                                {
                                    all_points=[];
                                }

                            }

                        }
                    },

                    series : [{
                        //allowPointSelect : true,
                        type : type,
                        name : 'Stock Price',
                        id: 'primary',
                        data : onadata,
                        tooltip: {
                            valueDecimals: 5,
                            crosshairs: true,
                            shared: true
                        },
                        dataGrouping : {

                            units : [
                                [
                                    'hour',
                                    [1, 2, 3, 4, 6, 8, 12]
                                ], [
                                    'day',
                                    [1]
                                ], [
                                    'week',
                                    [1]
                                ], [
                                    'month',
                                    [1, 3, 6]
                                ], [
                                    'year',
                                    [1]
                                ]
                            ]
                        }
                    },
                    ]
                });
        }

    });
});

and this is my js fiddle

Please help. Thanks in advance.

Body:
This is possible if you're using modern browsers that support CSS variables.

Highcharts.theme = {
    colors: [
      'var(--color1)',
      'var(--color2)',
      'var(--color3)',
      'var(--color4)',
      'var(--color5)',
      'var(--color6)',
    ]
}

Highcharts.setOptions(Highcharts.theme);

function setTheme(themeName) {
  // remove theme-* classes from body
  removeClasses = Array.from(document.body.classList).filter(s => s.startsWith('theme-'));
  document.body.classList.remove(...removeClasses)

  if (themeName) {
    document.body.classList.add('theme-' + themeName);
  }
}

CSS

body {
  --color1: #e00;
  --color2: #b00;
  --color3: #900;
  --color4: #600;
  --color5: #300;
  --color6: #000;
}

body.theme-dark {
  --color1: #555;
  --color2: #444;
  --color3: #333;
  --color4: #222;
  --color5: #111;
  --color6: #000;
}

body.theme-retro {
  --color1: #0f0;
  --color2: #ff0;
  --color3: #0ff;
  --color4: #0a0;
  --color5: #aa0;
  --color6: #00a;
}


来源: stackexchange

问:
How I can index this query in Postgresql?
I'm trying to index my query to optimize the execution time. I tried some b-tree, hash, GIN and GISP index but none of them have been used by the Postgres planner. And even when I define enable_seqscan = OFF, to force the use of my indexes, the time of execution persists or get worse. How I can efficiently index this query?

Obs: English is not my main language, so sorry for any textual problems

I'm using three tables, with 10k, 100k and 100k entries each. See code below. And my PostgreSQL version is 9.6.

Some indexes that I tried:

```<code>-CREATE INDEX trgm_curso_index ON curso USING gin (nome_curso gin_trgm_ops);
-CREATE INDEX trgm_natureza_index ON instituicao USING gin (natureza_administrativa gin_trgm_ops);
-CREATE INDEX fk_curso ON curso(sigla, campus);
-CREATE INDEX fk_campus ON campus(sigla);
-CREATE INDEX curso_index ON curso (nome_curso);
</code>```

My tables:

```<code>CREATE TABLE instituicao (
sigla varchar(10),
nome_instituicao varchar(55) NOT NULL,
natureza_administrativa varchar(7) NOT NULL CHECK (natureza_administrativa IN ('Pública', 'Privada')),
nota_IGC smallint NOT NULL CHECK (nota_IGC >= 0 AND nota_IGC <= 5),

PRIMARY KEY(sigla)
);

CREATE TABLE campus(
nome_campus varchar(55) NOT NULL,
estado char(2) NOT NULL CHECK (estado IN ('AC', 'AL', 'AP', 'AM', 'BA', 'CE', 'DF', 'ES', 'GO', 'MA', 'MT', 'MS', 'MG', 'PA', 'PB', 'PR', 'PE', 'PI', 'RJ', 'RN', 'RS', 'RO', 'RR', 'SC', 'SP', 'SE', 'TO')),
cidade varchar(55) NOT NULL,
sigla varchar(10),

PRIMARY KEY(sigla, nome_campus),
FOREIGN KEY(sigla) REFERENCES instituicao(sigla) ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE curso(
nome_curso varchar(55) NOT NULL,
area varchar(30) NOT NULL CHECK (area IN ('Ciências Exatas e da Terra', 'Ciências Biológicas', 'Engenharias', 'Ciências da Saúde', 'Ciências Agrárias', 'Ciências Sociais Aplicadas', 'Ciências Humanas', 'Linguística, Letras e Artes', 'Outros')),
nota_enade smallint NOT NULL CHECK (nota_enade >= 0 AND nota_enade <= 5),
grau varchar(12) NOT NULL CHECK (grau IN ('Bacharelado', 'Licenciatura', 'Mestrado', 'Doutorado', 'Tecnólogo')),
turno varchar(10) NOT NULL CHECK (turno IN ('Matutino', 'Integral', 'Noturno', 'Vespertino')),
duracao smallint NOT NULL CHECK (duracao > 0 AND duracao <= 12),
ano_criacao smallint NOT NULL CHECK (ano_criacao <= DATE_PART('YEAR', CURRENT_DATE) AND ano_criacao > 1980),
campus varchar(55) NOT NULL,
sigla varchar(10) NOT NULL,

PRIMARY KEY(sigla, campus, nome_curso),
FOREIGN KEY(sigla, campus) REFERENCES campus(sigla, nome_campus) ON DELETE CASCADE ON UPDATE CASCADE
);
</code>```

My query:

```<code>SELECT curso.sigla, estado, campus, cidade, nome_curso, grau, turno,
duracao, nota_enade
FROM instituicao, campus, curso
WHERE instituicao.sigla = campus.sigla AND campus.nome_campus =
curso.campus AND campus.sigla = curso.sigla AND natureza_administrativa =
'Pública' AND nome_curso LIKE 'C%'
ORDER BY curso.sigla, estado, cidade;
</code>```

The actual execution time in my notebook is 3.6-5.7 seconds, but I need to reach a maximum of 1-2 seconds.

答:
You should think about compound indexes each covering the joins and the filter here as far as possible. Try if the following indexes help.</p>

```<code>CREATE INDEX curso_n_c_s
             ON curso
                (nome_curso,
                 campus,
                 sigla);
CREATE INDEX campus_n_s
             ON campus
                (nome_campus,
                 sigla);
CREATE INDEX instituicao_na_s
             ON instituicao
                (natureza_administrativa,
                 sigla);
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I'm trying to index my query to optimize the execution time. I tried some b-tree, hash, GIN and GISP index but none of them have been used by the Postgres planner. And even when I define enable_seqscan = OFF, to force the use of my indexes, the time of execution persists or get worse. How I can efficiently index this query?

Obs: English is not my main language, so sorry for any textual problems

I'm using three tables, with 10k, 100k and 100k entries each. See code below. And my PostgreSQL version is 9.6.

Some indexes that I tried:

-CREATE INDEX trgm_curso_index ON curso USING gin (nome_curso gin_trgm_ops);
-CREATE INDEX trgm_natureza_index ON instituicao USING gin (natureza_administrativa gin_trgm_ops);
-CREATE INDEX fk_curso ON curso(sigla, campus);
-CREATE INDEX fk_campus ON campus(sigla);
-CREATE INDEX curso_index ON curso (nome_curso);

My tables:

CREATE TABLE instituicao (
sigla varchar(10),
nome_instituicao varchar(55) NOT NULL,
natureza_administrativa varchar(7) NOT NULL CHECK (natureza_administrativa IN ('Pública', 'Privada')),
nota_IGC smallint NOT NULL CHECK (nota_IGC >= 0 AND nota_IGC <= 5),

PRIMARY KEY(sigla)
);

CREATE TABLE campus(
nome_campus varchar(55) NOT NULL,
estado char(2) NOT NULL CHECK (estado IN ('AC', 'AL', 'AP', 'AM', 'BA', 'CE', 'DF', 'ES', 'GO', 'MA', 'MT', 'MS', 'MG', 'PA', 'PB', 'PR', 'PE', 'PI', 'RJ', 'RN', 'RS', 'RO', 'RR', 'SC', 'SP', 'SE', 'TO')),
cidade varchar(55) NOT NULL,
sigla varchar(10),

PRIMARY KEY(sigla, nome_campus),
FOREIGN KEY(sigla) REFERENCES instituicao(sigla) ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE curso(
nome_curso varchar(55) NOT NULL,
area varchar(30) NOT NULL CHECK (area IN ('Ciências Exatas e da Terra', 'Ciências Biológicas', 'Engenharias', 'Ciências da Saúde', 'Ciências Agrárias', 'Ciências Sociais Aplicadas', 'Ciências Humanas', 'Linguística, Letras e Artes', 'Outros')),
nota_enade smallint NOT NULL CHECK (nota_enade >= 0 AND nota_enade <= 5),
grau varchar(12) NOT NULL CHECK (grau IN ('Bacharelado', 'Licenciatura', 'Mestrado', 'Doutorado', 'Tecnólogo')),
turno varchar(10) NOT NULL CHECK (turno IN ('Matutino', 'Integral', 'Noturno', 'Vespertino')),
duracao smallint NOT NULL CHECK (duracao > 0 AND duracao <= 12),
ano_criacao smallint NOT NULL CHECK (ano_criacao <= DATE_PART('YEAR', CURRENT_DATE) AND ano_criacao > 1980),
campus varchar(55) NOT NULL,
sigla varchar(10) NOT NULL,

PRIMARY KEY(sigla, campus, nome_curso),
FOREIGN KEY(sigla, campus) REFERENCES campus(sigla, nome_campus) ON DELETE CASCADE ON UPDATE CASCADE
);

My query:

SELECT curso.sigla, estado, campus, cidade, nome_curso, grau, turno,
duracao, nota_enade
FROM instituicao, campus, curso
WHERE instituicao.sigla = campus.sigla AND campus.nome_campus =
curso.campus AND campus.sigla = curso.sigla AND natureza_administrativa =
'Pública' AND nome_curso LIKE 'C%'
ORDER BY curso.sigla, estado, cidade;

The actual execution time in my notebook is 3.6-5.7 seconds, but I need to reach a maximum of 1-2 seconds.

Body:
You should think about compound indexes each covering the joins and the filter here as far as possible. Try if the following indexes help.

CREATE INDEX curso_n_c_s
             ON curso
                (nome_curso,
                 campus,
                 sigla);
CREATE INDEX campus_n_s
             ON campus
                (nome_campus,
                 sigla);
CREATE INDEX instituicao_na_s
             ON instituicao
                (natureza_administrativa,
                 sigla);


问:
lazyinitalizationexception with wildfly 14: could not initialize proxy - the owning session was closed
If I run my code with wildfly 14 server I get following excpetion. The same code worked with wildfly 13. Where could be the problem?</p>

```<code>Caused by: org.hibernate.LazyInitializationException: could not initialize proxy [ch.example.tool.entity.Benutzer#12] - the owning Session was closed
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:172)
    at org.hibernate.proxy.AbstractLazyInitializer.getIdentifier(AbstractLazyInitializer.java:89)
    at org.hibernate.internal.SessionImpl.getProxyIdentifier(SessionImpl.java:1752)
    at org.hibernate.internal.SessionImpl.getContextEntityIdentifier(SessionImpl.java:1743)
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:271)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:495)
    at org.hibernate.type.EntityType.nullSafeSet(EntityType.java:280)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2868)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3307)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3229)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3630)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:146)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
    ... 122 more
</code>```

答: From this article, setting hibernate.jpa.compliance.proxy=false in your configuration may be the solution (as Wildfly may be setting this to true).

Body:
If I run my code with wildfly 14 server I get following excpetion. The same code worked with wildfly 13. Where could be the problem?

Caused by: org.hibernate.LazyInitializationException: could not initialize proxy [ch.example.tool.entity.Benutzer#12] - the owning Session was closed
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:172)
    at org.hibernate.proxy.AbstractLazyInitializer.getIdentifier(AbstractLazyInitializer.java:89)
    at org.hibernate.internal.SessionImpl.getProxyIdentifier(SessionImpl.java:1752)
    at org.hibernate.internal.SessionImpl.getContextEntityIdentifier(SessionImpl.java:1743)
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:271)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:495)
    at org.hibernate.type.EntityType.nullSafeSet(EntityType.java:280)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2868)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3307)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3229)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3630)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:146)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
    ... 122 more


问:
Reference previous row in a data.frame
I have a very simple problem. I'm am trying to set the value of column X to 0 if column Y[n,] does not equal column Y[n-1,]. My issue is that I do not know how to reference a previous row value in R, and then use that value to set the value of another column.

As an example:

```<code>Y X
1 5
1 1
2 0
2 2
</code>```

X[3,2] is 0 because Y[3,1] does not equal Y[2,1].

I need to basically find all instance of this in a large data-set and set the corresponding X value to 0.

data$X <- 0 if data$Y[n] =! data$Y[n-1]

Is there a simple solution to this in R? It really feels as though there should be.

Thank you

答:
Another base R option

```<code>with(df, X * c(TRUE, !(Y[-1] - Y[-length(Y)])))
#[1] 5 1 0 2
</code>```

Or using dplyr

```<code>library(dplyr)
df %>%
   mutate(X = c(X[1], ((duplicated(Y) * X)[-1])))
#  Y X
#1 1 5
#2 1 1
#3 2 0
#4 2 2
</code>```

data

```<code>df <- structure(list(Y = c(1L, 1L, 2L, 2L), X = c(5L, 1L, 0L, 2L)),
  class = "data.frame", row.names = c(NA, -4L))
</code>```

Title: Reference previous row in a data.frame

Body:
I have a very simple problem. I'm am trying to set the value of column X to 0 if column Y[n,] does not equal column Y[n-1,]. My issue is that I do not know how to reference a previous row value in R, and then use that value to set the value of another column.

As an example:

Y X
1 5
1 1
2 0
2 2

X[3,2] is 0 because Y[3,1] does not equal Y[2,1].

I need to basically find all instance of this in a large data-set and set the corresponding X value to 0.

data$X <- 0 if data$Y[n] =! data$Y[n-1]

Is there a simple solution to this in R? It really feels as though there should be.

Thank you

Body:
Another base R option

with(df, X * c(TRUE, !(Y[-1] - Y[-length(Y)])))
#[1] 5 1 0 2

Or using dplyr

library(dplyr)
df %>%
   mutate(X = c(X[1], ((duplicated(Y) * X)[-1])))
#  Y X
#1 1 5
#2 1 1
#3 2 0
#4 2 2

data

df <- structure(list(Y = c(1L, 1L, 2L, 2L), X = c(5L, 1L, 0L, 2L)),
  class = "data.frame", row.names = c(NA, -4L))


问:
xsl:call-template by name stored in string
I have this xsl template:

```<code><xsl:template name="test">
I'm test
</xsl:template>
</code>```

and I can call this template by calling

```<code><xsl:call-template name="test"/>
</code>```

QUESTION:
How to call xsl template by name stored in variable?

```<code><xsl:variable name="var1" select="'test'"/>
<xsl:call-template name="$var1"/>
</code>```

答:
Instead of using xsl:call-template, try xsl:apply-templates.

```<code><xsl:template name="test" match="test">
I'm test
</xsl:template>

<xsl:variable name="var1"><test/></xsl:variable>

<xsl:apply-templates select="$var1/*"/>
</code>```

But in XSLT 3.0 you can do this much more cleanly using higher-order functions; functions become first-class values and can be bound to variables.

Body:
I have this xsl template:

<xsl:template name="test">
I'm test
</xsl:template>

and I can call this template by calling

<xsl:call-template name="test"/>

QUESTION:
How to call xsl template by name stored in variable?

<xsl:variable name="var1" select="'test'"/>
<xsl:call-template name="$var1"/>

Body:
Instead of using xsl:call-template, try xsl:apply-templates.

<xsl:template name="test" match="test">
I'm test
</xsl:template>

<xsl:variable name="var1"><test/></xsl:variable>

<xsl:apply-templates select="$var1/*"/>

But in XSLT 3.0 you can do this much more cleanly using higher-order functions; functions become first-class values and can be bound to variables.


问:
TypeError: 'Entry' object is not callable
I am building an Python with Tkinter application, and one of the things it does is take input (from Entry boxes) and saves it. However, recently it has started creating this error code in multiple places:

```<code>Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Adam\Desktop\paintersinventory.py", line 2773, in grabpaintingname
saveedit()
File "C:\Users\Adam\Desktop\paintersinventory.py", line 2736, in saveedit
w = str(title[pnttoedit]) + " (" + str(copyat) + ")"
TypeError: 'Entry' object is not callable
</code>```

Here is the related code, excluding the Tkinter box that pops up to ask for the information, which exists and correctly, after a button press sends the code to the grabpaintingname def:

```<code>painter = {}
title = {}
names = []
pnttoedit = ''
copyat = 1

def saveedit():
    w = str(title[pnttoedit]) + " (" + str(copyat) + ")"
    names.append(w)
    v = painter[pnttoedit]
    painter[w] = v
    messagebox.showinfo("Painter's Inventory", "Copy of " + str(pnttoedit) + " created.")
    print(str(pnttoedit))

def grabpaintingname():
     global pnttoedit
     pnttoedit = tvkare.get()
     saveedit()

tvkare = StringVar(editers)
tvkare.set(names[0])
e2 = OptionMenu(mainframe, tvkare, *names)
e2.grid(row=3, column=1)
def change_dropdown(*args):
    pnttoedit = tvkare.get()
</code>```

Any help with how to solve this problem or information on why it might be occurring would be very much appreciated. Thank you in advance!

答:
Just wanted to mark that it was solved by Bryan Oakley.

I had variables called str accidentally.

Body:
I am building an Python with Tkinter application, and one of the things it does is take input (from Entry boxes) and saves it. However, recently it has started creating this error code in multiple places:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Adam\Desktop\paintersinventory.py", line 2773, in grabpaintingname
saveedit()
File "C:\Users\Adam\Desktop\paintersinventory.py", line 2736, in saveedit
w = str(title[pnttoedit]) + " (" + str(copyat) + ")"
TypeError: 'Entry' object is not callable

Here is the related code, excluding the Tkinter box that pops up to ask for the information, which exists and correctly, after a button press sends the code to the grabpaintingname def:

painter = {}
title = {}
names = []
pnttoedit = ''
copyat = 1

def saveedit():
    w = str(title[pnttoedit]) + " (" + str(copyat) + ")"
    names.append(w)
    v = painter[pnttoedit]
    painter[w] = v
    messagebox.showinfo("Painter's Inventory", "Copy of " + str(pnttoedit) + " created.")
    print(str(pnttoedit))

def grabpaintingname():
     global pnttoedit
     pnttoedit = tvkare.get()
     saveedit()

tvkare = StringVar(editers)
tvkare.set(names[0])
e2 = OptionMenu(mainframe, tvkare, *names)
e2.grid(row=3, column=1)
def change_dropdown(*args):
    pnttoedit = tvkare.get()

Any help with how to solve this problem or information on why it might be occurring would be very much appreciated. Thank you in advance!


问:
Laravel - How to Convert MySQL query to Laravel Eloquent
I have MySQL query I want to convert to Laravel Eloquent

I have written the query in MySQL

```<code>SELECT a.transaction_number a.date, a.item_number, b.desc, a.variant_code, sum(a.quantity) AS quantity, a.cost
FROM `items_details` AS a
JOIN `items` AS b ON b.id = a.item_number
WHERE a.item_number = 0101010
GROUP BY a.variant_code
ORDER BY transaction_number, variant_code
</code>```

答:
Seems pretty simple.

```<code>DB::table('items_details as a')
    ->join('items b', 'b.id', '=', 'a.item_number')
    ->select([
        'a.transaction_number',
        'a.date',
        'a.item_number',
        'b.desc',
        'a.variant_code',
        DB::raw('sum(a.quantity) AS quantity'),
        'a.cost'
    ])
    ->where('a.item_number', '=', 10101010)
    ->groupBy('a.variant_code')
    ->orderBy('transaction_number')
    ->orderBy('variant_code');
</code>```

Note: Not Tested

Body:
I have MySQL query I want to convert to Laravel Eloquent

I have written the query in MySQL

SELECT a.transaction_number a.date, a.item_number, b.desc, a.variant_code, sum(a.quantity) AS quantity, a.cost
FROM `items_details` AS a
JOIN `items` AS b ON b.id = a.item_number
WHERE a.item_number = 0101010
GROUP BY a.variant_code
ORDER BY transaction_number, variant_code

Body:
Seems pretty simple.

DB::table('items_details as a')
    ->join('items b', 'b.id', '=', 'a.item_number')
    ->select([
        'a.transaction_number',
        'a.date',
        'a.item_number',
        'b.desc',
        'a.variant_code',
        DB::raw('sum(a.quantity) AS quantity'),
        'a.cost'
    ])
    ->where('a.item_number', '=', 10101010)
    ->groupBy('a.variant_code')
    ->orderBy('transaction_number')
    ->orderBy('variant_code');

Note: Not Tested


问:
Moving an object with a file descriptor
I have a class, say Foo, defined like this:

class Foo {
public:
  Foo(int i) {
    valid = false;
    char devname[] = "/dev/device0";
    sprintf(devname, "/dev/device%d", i);
    fd = open(devname, O_RDWR);
    if (fd > 0) {
      valid = true;
    }

  ~Foo() {
    if (valid) {
      close(fd);
    }
  }

  bool valid;
private:
  int fd;
};
```

<p>I have another class, say <code>Bar</code>, defined like this:</p>

<pre class="lang-cpp prettyprint-override"><code>class Bar {
public:
  Bar() {
    for (int i = 0; i < 4; i++) {
      Foo foo(i);
      if (foo.valid) {
        vector_of_foos.push_back(foo);
      }
    }
  }

  std::vector<Foo> vector_of_foos;
};
</code>```

The problem with this is that push_back makes a copy of the Foo object, which copies the fd property. Then the destructor for the original Foo object is called, which closes the file that fd points to, rendering fd invalid.

Unfortunately I cannot use emplace_back since I need to instantiate my Foo object before adding it to the vector_of_foos vector so that I can check the valid property.

I have also tried using std::move but this still calls the destructor for the original Foo object once it goes out of scope which closes the file.

What is the recommended way of managing a resource like this? Should I use an array of smart pointers? Should my vector_of_foos be instead a std::vector<Foo *> where I maintain a vector of pointers to Foo which I allocate dynamically?

答:
Foo needs a copy constructor and copy assignment operator so it can dup() the fd of the copied-from object (or, you need to delete them so Foo objects can't be copied at all, only moved).

When implementing move semantics, after moving the fd value from the moved-from object to the moved-to object, you need to update the moved-from object so its fd no longer refers to a valid file descriptor.  Simply set it to -1, which is what open() and dup() return on error.

You don't need your valid member at all.  That is a source of error waiting to happen if it ever gets out of sync with your fd.

Try something more like this:

class Foo {
public:
  Foo(int i) {
    std::string devname = "/dev/device" + std::to_string(i);
    fd = open(devname.c_str(), O_RDWR);
  }

  Foo(const Foo &src) {
    fd = dup(src.fd);
  }
  // or: Foo(const Foo &) = delete;

  Foo(Foo &&src) : fd(-1) {
    src.swap(*this);
  }

  ~Foo() {
    if (fd != -1) {
      close(fd);
    }
  }

  bool valid() const {
    return (fd != -1);
  }

  Foo& operator=(Foo rhs) {
    rhs.swap(*this);
    return *this;
  }
  // optional: Foo& operator=(const Foo &) = delete;

  void swap(Foo &other) {
    std::swap(fd, other.fd);
  }

private:
  int fd;
};
```

Title: Moving an object with a file descriptor

Body:
I have a class, say Foo, defined like this:

class Foo {
public:
  Foo(int i) {
    valid = false;
    char devname[] = "/dev/device0";
    sprintf(devname, "/dev/device%d", i);
    fd = open(devname, O_RDWR);
    if (fd > 0) {
      valid = true;
    }

  ~Foo() {
    if (valid) {
      close(fd);
    }
  }

  bool valid;
private:
  int fd;
};

I have another class, say Bar, defined like this:

class Bar {
public:
  Bar() {
    for (int i = 0; i < 4; i++) {
      Foo foo(i);
      if (foo.valid) {
        vector_of_foos.push_back(foo);
      }
    }
  }

  std::vector<Foo> vector_of_foos;
};

The problem with this is that push_back makes a copy of the Foo object, which copies the fd property. Then the destructor for the original Foo object is called, which closes the file that fd points to, rendering fd invalid.

Unfortunately I cannot use emplace_back since I need to instantiate my Foo object before adding it to the vector_of_foos vector so that I can check the valid property.

I have also tried using std::move but this still calls the destructor for the original Foo object once it goes out of scope which closes the file.

What is the recommended way of managing a resource like this? Should I use an array of smart pointers? Should my vector_of_foos be instead a std::vector<Foo *> where I maintain a vector of pointers to Foo which I allocate dynamically?


问:
Git error: fatal: unable to connect a socket (Invalid argument)
I've got msysGit (Git on Windows) working just fine on my home machine, but at work, where we are behind a Microsoft ISA proxy, I get the following error when I do a git clone:

```<code>H:\>git clone git://github.com/akitaonrails/vimfiles.git
Initialized empty Git repository in H:/vimfiles/.git/
github.com[0: 65.74.177.129]: errno=Invalid argument
fatal: unable to connect a socket (Invalid argument)
</code>```

I've tried setting the http_proxy environment variable to:

```<code>http://our-proxy-server:8088
</code>```

I've set the git http.proxy configuration directive:

```<code>git config --global http.proxy http://our-proxy-server:8088
</code>```

Neither of the above makes a difference.

Doing a git clone with http:// instead of git:// yields this:

```<code>H:\>git clone http://github.com/akitaonrails/vimfiles.git
Initialized empty Git repository in H:/vimfiles/.git/
fatal: http://github.com/akitaonrails/vimfiles.git/info/refs download error - The    requested URL returned error: 407
</code>```

407 is of course an authentication error.

So my question is: has anyone managed to get git working from behind a proxy, specifically an ISA proxy? I don't know if it's going to be worth pursing this. Any help very much appreciated.

Thanks!

答:
Python 2.7 and ntlmaps worked for me after getting 407 errors and other junk from GitHub. </p>

<p>.gitconfig :  </p>

```<code>[http]
      proxy = http://127.0.0.1:5865
</code>```

OwnerDisplayName: Charles Roper

ContentLicense: CC BY-SA 2.5

Body:
I've got msysGit (Git on Windows) working just fine on my home machine, but at work, where we are behind a Microsoft ISA proxy, I get the following error when I do a git clone:

H:\>git clone git://github.com/akitaonrails/vimfiles.git
Initialized empty Git repository in H:/vimfiles/.git/
github.com[0: 65.74.177.129]: errno=Invalid argument
fatal: unable to connect a socket (Invalid argument)

I've tried setting the http_proxy environment variable to:

http://our-proxy-server:8088

I've set the git http.proxy configuration directive:

git config --global http.proxy http://our-proxy-server:8088

Neither of the above makes a difference.

Doing a git clone with http:// instead of git:// yields this:

H:\>git clone http://github.com/akitaonrails/vimfiles.git
Initialized empty Git repository in H:/vimfiles/.git/
fatal: http://github.com/akitaonrails/vimfiles.git/info/refs download error - The    requested URL returned error: 407

407 is of course an authentication error.

So my question is: has anyone managed to get git working from behind a proxy, specifically an ISA proxy? I don't know if it's going to be worth pursing this. Any help very much appreciated.

Thanks!

Body:
Python 2.7 and ntlmaps worked for me after getting 407 errors and other junk from GitHub.

.gitconfig :

[http]
      proxy = http://127.0.0.1:5865

ContentLicense: CC BY-SA 3.0


问:
Add custom map style to Mapbox.js
I just created my personal map style with Mapbox Studio and i'm try to use it with Mapbox.js. (NOT Mapbox Gl js!!)
As written in the docu

You can load and display styles from the API using Mapbox GL JS or the Mapbox Mobile SDKs or you can request styles directly from the API. This request requires an access token with the styles:read scope.

I added on my  a link to:
https://api.mapbox.com/styles/v1/myusername/mystyleid?access_token=mypublicaccesstoken

I can see the JSON response but  i still see basic map style.

How can i use my custom style in mapbox.js?

答:
You can use styles made with Mapbox Studio using the styleLayer method:
https://docs.mapbox.com/mapbox.js/example/v1.0.0/stylelayer/. Though, I'd highly recommend moving over to Mapbox GL JS when possible to leverage all the functionality of WebGL and vector tiles.

Title: Add custom map style to Mapbox.js


问:
Flatten included files in msbuild project output
I have an MSBuild project using the new .Net SDK project format.

I'm trying to include files from outside the solution, which have their own folder structures and flatten that structure upon going to the build directory.

I.e.

```<code>Solution\
  Lib\
    lib1\
      file1.dll
      file2.dll
  lib2\
      file3.dll
      file3.dll
  Project\
     project.csproj
</code>```

I want the entire structure under \Lib to be represented in my project in Visual Studio. I can do that with the following:

```<code><ItemGroup>
  <None Include="$(SolutionDir)\Lib\**\*">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup>
</code>```

The problem is, the output directory contains the same folder structure. I would like all the files flattened to simply exist in my \bin directory instead of \bin\lib1\file1.dll, \bin\lib2\file3.dll, etc.

Is there a way to handle this in the project file?

答:
You should be able to change it to

```<code><None Include="$(SolutionDir)\Lib\**\*" TargetPath="%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
</code>```

The updated target path will then no longer contain the subdirectories.

Body:
I have an MSBuild project using the new .Net SDK project format.

I'm trying to include files from outside the solution, which have their own folder structures and flatten that structure upon going to the build directory.

I.e.

Solution\
  Lib\
    lib1\
      file1.dll
      file2.dll
  lib2\
      file3.dll
      file3.dll
  Project\
     project.csproj

I want the entire structure under \Lib to be represented in my project in Visual Studio. I can do that with the following:

<ItemGroup>
  <None Include="$(SolutionDir)\Lib\**\*">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup>

The problem is, the output directory contains the same folder structure. I would like all the files flattened to simply exist in my \bin directory instead of \bin\lib1\file1.dll, \bin\lib2\file3.dll, etc.

Is there a way to handle this in the project file?

Body:
You should be able to change it to

<None Include="$(SolutionDir)\Lib\**\*" TargetPath="%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />

The updated target path will then no longer contain the subdirectories.


问:
Difference between branches and streams in ClearCase?
What is difference between branches and streams in ClearCase?</p>

答:
A branch is a classic versioning way to parallelize the history of versions for a given file: See "When should you branch"

A Stream is not a branch: it is just a metadata able to memorize what baseline any view referencing that Stream will see.

When you create a Stream, nothing happen (no branch is created).

But a Stream name will be used when a file is checked out: any view will set its config spec in order to create a branch named after the Stream in order to isolate the development effort in said branch.

(See "How do I create a snapshot view of some project or stream in ClearCase?")

This is why it is important to adequately name a Stream: If I create a Stream named "VonC", you will eventually see (in the version tree for any modified file) a branch named "VonC": what is the purpose of a branch "VonC"?

If I create a Stream named "REL2.2_FIX", you will see branches named "REL2.2_FIX" and will infer that any view referencing that Stream is there to produce fixes on the release 2.2: a much more useful name. (This is why I don't like the "one stream per developer model")

So if you have any writable component, a Stream could be considered as a template for branches:

You declare what you need in a stream (what baseline you want to see)

You create a view on that stream

Any checkout will create a branch named after the Stream.

(And that is why so many UCM users mix or equate "Stream" with "branch")

But if you have only non-writable components in your project, then a Stream is just the list of baselines (labels on components) that you want to see in any view you will create on said Stream.

That becomes a visualization mechanism, useful for testing environment where you only need to access precise versions of a set of components in order to test your system.

In that case, no branches will ever be created, since no checkout will ever be made on any file: the component are declared non-writable in the UCM project.

The other major difference between a Stream and a branch is the organization of Stream in a hierarchy (parent Stream / sub-Streams).

That hierarchy simply don't exist for branches: when you have 3 branches A, B, C:

you don't know where to merge from branch A once you have finish your work on it.

any merge you do has the same meaning: A->B, or C->A, or B->C, or ...

With Stream, you would have:

```<code>MyProject_Int
 |
 --MyProject_Dev
    |
    -- MyProject_Feature1
</code>```

The hierarchy of Streams is there to:

introduce a possible workflow of merges (you know where you should merge from one Stream to another: namely its parent. It is not mandatory, but at least you have a visual way of knowing that:

Feature1, once fully developed, will get back (be merged to) MyProject_Dev (its parent Stream), and that:

MyProject_Dev, once a stable state is reached, can be merged into its parent Stream MyProject_Int, where integration tests can be conducted while development go on uninterrupted in MyProject_Dev.

add a meaning to those merges:

merging from a sub-stream to its parent or any other parent stream (for instance, you can merge directly from MyProject_Feature1 to MyProject_Int if you have to) is called a deliver.

merging from a parent Stream (like MyProject_Dev) to an immediate sub-Stream (like (MyProject_Feature1) is called a rebase.

Its purpose is to ensure that Feature1 is developed with the latest changes of Dev, in order to make the final deliver as painless as possible: with regular rebases, the common set of code would not have diverged too much between the two parallelized histories of those two branches derived from those two Streams.

Keep in mind that those two UCM operations deliver and rebase are, at their core, no more than simple merges between two branches A and B.

However, because of their names, you know that you don't merge just between any two branches, but between a sub-Stream and a parent Stream (deliver), or between a parent Stream and a sub-Stream (rebase).

Body:
A branch is a classic versioning way to parallelize the history of versions for a given file: See "When should you branch"

A Stream is not a branch: it is just a metadata able to memorize what baseline any view referencing that Stream will see.

When you create a Stream, nothing happen (no branch is created).

But a Stream name will be used when a file is checked out: any view will set its config spec in order to create a branch named after the Stream in order to isolate the development effort in said branch.

(See "How do I create a snapshot view of some project or stream in ClearCase?")

This is why it is important to adequately name a Stream: If I create a Stream named "VonC", you will eventually see (in the version tree for any modified file) a branch named "VonC": what is the purpose of a branch "VonC"?

If I create a Stream named "REL2.2_FIX", you will see branches named "REL2.2_FIX" and will infer that any view referencing that Stream is there to produce fixes on the release 2.2: a much more useful name. (This is why I don't like the "one stream per developer model")

So if you have any writable component, a Stream could be considered as a template for branches:

You declare what you need in a stream (what baseline you want to see)

You create a view on that stream

Any checkout will create a branch named after the Stream.

(And that is why so many UCM users mix or equate "Stream" with "branch")

But if you have only non-writable components in your project, then a Stream is just the list of baselines (labels on components) that you want to see in any view you will create on said Stream.

That becomes a visualization mechanism, useful for testing environment where you only need to access precise versions of a set of components in order to test your system.

In that case, no branches will ever be created, since no checkout will ever be made on any file: the component are declared non-writable in the UCM project.

The other major difference between a Stream and a branch is the organization of Stream in a hierarchy (parent Stream / sub-Streams).

That hierarchy simply don't exist for branches: when you have 3 branches A, B, C:

you don't know where to merge from branch A once you have finish your work on it.

any merge you do has the same meaning: A->B, or C->A, or B->C, or ...

With Stream, you would have:

MyProject_Int
 |
 --MyProject_Dev
    |
    -- MyProject_Feature1

The hierarchy of Streams is there to:

introduce a possible workflow of merges (you know where you should merge from one Stream to another: namely its parent. It is not mandatory, but at least you have a visual way of knowing that:

Feature1, once fully developed, will get back (be merged to) MyProject_Dev (its parent Stream), and that:

MyProject_Dev, once a stable state is reached, can be merged into its parent Stream MyProject_Int, where integration tests can be conducted while development go on uninterrupted in MyProject_Dev.

add a meaning to those merges:

merging from a sub-stream to its parent or any other parent stream (for instance, you can merge directly from MyProject_Feature1 to MyProject_Int if you have to) is called a deliver.

merging from a parent Stream (like MyProject_Dev) to an immediate sub-Stream (like (MyProject_Feature1) is called a rebase.

Its purpose is to ensure that Feature1 is developed with the latest changes of Dev, in order to make the final deliver as painless as possible: with regular rebases, the common set of code would not have diverged too much between the two parallelized histories of those two branches derived from those two Streams.

Keep in mind that those two UCM operations deliver and rebase are, at their core, no more than simple merges between two branches A and B.

However, because of their names, you know that you don't merge just between any two branches, but between a sub-Stream and a parent Stream (deliver), or between a parent Stream and a sub-Stream (rebase).


问:
Ionic 4 - check if can go back
I’m looking for a way to redirect a user back after a click on a button if said back exists, and to a specific route otherwise.

I need to add a condition to my code, but I don’t seem to find a way.

```<code>  goback() {
    if("something") {
      this.navCtlr.setDirection('back');
      this.location.back();
    }
    else {
      this.router.navigate(['/']);
    }
  }
</code>```

In Ionic 3 we had canGoBack() 1 function… Any idea for Ionic 4? Since it's using Angular router...

Thanks.

答:
The only idea that comes to my mind is using an Angular route guard to allow or restrict going back, even a resolve function would be somewhat helpful.

That and using a navigation history service (RoutingStateService) such as the mentioned in https://blog.hackages.io/our-solution-to-get-a-previous-route-with-angular-5-601c16621cf0
In the cases I've used the RoutingStateService to handle my navigation history, it has been far more reliable than directly trusting NavigationEnd events (since the RouterStateService uses it inside).

Hope it helps. Best regards.

Title: Ionic 4 - check if can go back

Body:
I’m looking for a way to redirect a user back after a click on a button if said back exists, and to a specific route otherwise.

I need to add a condition to my code, but I don’t seem to find a way.

  goback() {
    if("something") {
      this.navCtlr.setDirection('back');
      this.location.back();
    }
    else {
      this.router.navigate(['/']);
    }
  }

In Ionic 3 we had canGoBack() 1 function… Any idea for Ionic 4? Since it's using Angular router...

Thanks.


问:
Odoo 9.0 email notifications are not automatically fired
Odoo 9.0 email notifications are not automatically fired. I had to manually send one by one. Please see attachment. Could anyone guide me to what need to changed in order to fire these pending emails automatically?

答: Just go to settings -> Automation and Check if Email Queue Manager is active and you can run it manually or update Next Execution Date


问:
Forcing form text to be lower-case
How could I force the text in the "username" text input to be lower-case regardless of what user types?

```<code><div class="register">
   <label for="username">Select username:</label>
</div>
<div class="registerform">
    <input name="username" class="registerfont" type="text"
           id="username" maxlength="15" style="height:35px; width:300px">
</div>
</code>```

答:
I use this simple code :</p>

```<code><input type="text" onkeyup="this.value = this.value.toUpperCase();">
</code>```

Title: Forcing form text to be lower-case

Body:
How could I force the text in the "username" text input to be lower-case regardless of what user types?

   Select username:

    <input name="username" class="registerfont" type="text"
           id="username" maxlength="15" style="height:35px; width:300px">

Body:
I use this simple code :

<input type="text" onkeyup="this.value = this.value.toUpperCase();">


问:
R - group by and create new columns for grouped result
I have the test data as follows

```<code>> date <- c('11:00', '12:00', '13:00', '11:00', '13:00', '13:00', '15:00')
> zone <- c('a', 'a', 'a', 'b', 'b', 'c', 'c')
> val <- c(1,2,3,4,5,6,7)
> test.data <- data.frame(date, zone, val)
> test.data
   date zone val
1 11:00    a   1
2 12:00    a   2
3 13:00    a   3
4 11:00    b   4
5 13:00    b   5
6 13:00    c   6
7 15:00    c   7
</code>```

I need the result to be first grouped by date. And then for each unique zone a new column be added. The value of that column is picked from the column val, and if missing it's 0.

Desired Result:

```<code>result
   date zone_a zone_b zone_c
1 11:00      1      4      0
2 12:00      2      0      0
3 13:00      3      5      6
4 15:00      0      0      7
</code>```

答:
An option is to mutate the 'zone' column by concatenating 'zone' and then spread to 'wide'

```<code>library(tidyverse)
test.data %>%
    mutate(zone = str_c("zone_", zone)) %>%
    spread(zone, val, fill = 0)
</code>```

Body:
I have the test data as follows

> date <- c('11:00', '12:00', '13:00', '11:00', '13:00', '13:00', '15:00')
> zone <- c('a', 'a', 'a', 'b', 'b', 'c', 'c')
> val <- c(1,2,3,4,5,6,7)
> test.data <- data.frame(date, zone, val)
> test.data
   date zone val
1 11:00    a   1
2 12:00    a   2
3 13:00    a   3
4 11:00    b   4
5 13:00    b   5
6 13:00    c   6
7 15:00    c   7

I need the result to be first grouped by date. And then for each unique zone a new column be added. The value of that column is picked from the column val, and if missing it's 0.

Desired Result:

result
   date zone_a zone_b zone_c
1 11:00      1      4      0
2 12:00      2      0      0
3 13:00      3      5      6
4 15:00      0      0      7

Body:
An option is to mutate the 'zone' column by concatenating 'zone' and then spread to 'wide'

library(tidyverse)
test.data %>%
    mutate(zone = str_c("zone_", zone)) %>%
    spread(zone, val, fill = 0)


问:
How can I convert the sum of minutes, hours in the 'timelite.js' or 'moment.js'?
I have array let times = ['04:20:10', '21:15:10']. I use method add:

let time1 = add([times]). Next I use method str: str([time1]). I kept
25:35:30. I would like to receive the following result: 25h 35 min 30 sec. If the sum would come in at 30:00:00 I would like to receive the result 30h. In the case of the sum of 00:30:00 I would like to receive a result of 30 min. In the case of 00:00:20 - the result 20 sec. How to get such a result in the 'timelite.js' or 'moment.js' library?

答:
Maybe could be easier if you just format the time.

```<code>const formatTime = (time) => {
  const [hour, minute, sec] = time.split(':')
  return `${hour} h ${minute} min ${sec} sec`
}

const time = '25:35:30'

console.log(formatTime(time))

// 25 h 35 min 30 sec
</code>```

This is just a simple example. To achieve what you want you need to put some more logic, but yeah it's a good start.

Body:
Maybe could be easier if you just format the time.

const formatTime = (time) => {
  const [hour, minute, sec] = time.split(':')
  return `${hour} h ${minute} min ${sec} sec`
}

const time = '25:35:30'

console.log(formatTime(time))

// 25 h 35 min 30 sec

This is just a simple example. To achieve what you want you need to put some more logic, but yeah it's a good start.


问:
Mapper fails to locate foreign keys on secondary table (Many to many relationship)
I'm trying to describe a simple  "many to many" relationship using flask-SQLAlchemy, however, the mapper does'nt seem to find the foreign keys on my secondary table, even though they are correctly defined.

I'm using the latest version os flask-SQLAlchemy (2.4) and SQLite. I've tried many code variations and still, nothing. Doumentation is not helping either =/

user_permit = db.table('userpermit',
    db.Column('permit_id', db.Integer, db.ForeignKey('permit.permit_id'), primary_key=True),
    db.Column('user_mail', db.String(128), db.ForeignKey('user.user_mail'), primary_key=True)
)

class User(db.Model):
    user_mail = db.Column(db.String(128), primary_key=True)

[...]
    permits = db.relationship('Permit', secondary=user_permit, backref='users_permitted')

class Permit(db.Model):
    permit_id = db.Column(db.Integer, primary_key=True)
```

I always get the same error message: NoForeignKeysError: Could not determine join condition between parent/child tables on relationship User.permits - there are no foreign keys linking these tables via secondary table ''.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions.

It seems it does not recognize my secondary table, since it says '', instead of 'userpermit'.

答:
It may be because you're defining db.table, not db.Table. Otherwise, a few other things to check:

Because you're not specifying __tablename__ = for each table, check the names of the tables are what you're expecting, e.g. permit & user.

You can manually specify the join condition using the primaryjoin parameter of relationship(), which allows you to provide the SQL to determine the join

Body:
I'm trying to describe a simple  "many to many" relationship using flask-SQLAlchemy, however, the mapper does'nt seem to find the foreign keys on my secondary table, even though they are correctly defined.

I'm using the latest version os flask-SQLAlchemy (2.4) and SQLite. I've tried many code variations and still, nothing. Doumentation is not helping either =/

user_permit = db.table('userpermit',
    db.Column('permit_id', db.Integer, db.ForeignKey('permit.permit_id'), primary_key=True),
    db.Column('user_mail', db.String(128), db.ForeignKey('user.user_mail'), primary_key=True)
)

class User(db.Model):
    user_mail = db.Column(db.String(128), primary_key=True)

[...]
    permits = db.relationship('Permit', secondary=user_permit, backref='users_permitted')

class Permit(db.Model):
    permit_id = db.Column(db.Integer, primary_key=True)

I always get the same error message: NoForeignKeysError: Could not determine join condition between parent/child tables on relationship User.permits - there are no foreign keys linking these tables via secondary table ''.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions.

It seems it does not recognize my secondary table, since it says '', instead of 'userpermit'.


问:
Azure WebJob running old code
We have a bug in our WebJob running in our live environment, I have identified the bug and fixed it, this I can verify in our Dev-environment. I published my WebJob as a "Azure WebJob" to our live environment but the bug i still present. To add to the confusion the bug now just occurs sometimes. So for some reason the old code is running somewhere sometimes.

Can someone please help me understand this?

答:
In our case the web app was published to physical path /site/www instead of the default /site/wwwroot, because of this the Azure web portal interface adds the WebJobs to folder /site/jobs, but the webdeploy via VS or Azure are still trying to publish the webjobs inside the /site/www.

More details at Publishing WebJobs with Azure Pipelines

Title: Azure WebJob running old code


问:
How do I get an array/list filled with all the image paths I loaded as assets in Flutter?
I want to know if there is a way (with or without pubspec.yaml asset list) to read a directory where images will be stored, and create an array with all the paths to each image, so I can go through the array (say, with a FOR ) and create a gallery of photos. This with the purpose of being able to change, remove or add images from the directory without worrying about the paths being coded in assets.

I've read that I can use AssetBundle or RootBundle for this but I cant find any example doing so. Any help on this, even pointing me in a different approach, would be greatly appreciated.

Thanks!

EDIT: This got marked as a duplicate, but the question referenced as a duplicate is different in the sense that I need to scan a directory automatically , So I dont need to know any of the names of the files on the directory, I just want a way to read the directory (who's name I can know) and retrieve an array populated with the assets inside the directory.

答: There is (or at least used to be - beware, it's an implementation detail) an asset called AssetManifest.json. You can load that asset first, and it contains details of the actual assets.


问:
What solutions are there for circular references?
When using reference counting, what are possible solutions/techniques to deal with circular references?

The most well-known solution is using weak references, however many articles about the subject imply that there are other methods as well, but keep repeating the weak-referencing example. Which makes me wonder, what are these other methods?

I am not asking what are alternatives to reference counting, rather what are solutions to circular references when using reference counting.

This question isn't about any specific problem/implementation/language rather a general question.

答:
I have always redesigned to avoid the issue.  One of the common cases where this comes up is the parent child relationship where the child needs to know about the parent.  There are 2 solutions to this

Convert the parent to a service, the parent then does not know about the children and the parent dies when there are no more children or the main program drops the parent reference.

If the parent must have access to the children, then have a register method on the parent which accepts a pointer that is not reference counted, such as an object pointer, and a corresponding unregister method.  The child will need to call the register and unregister method.  When the parent needs to access a child then it type casts the object pointer to the reference counted interface.


问:
Replace capital letters in an entire string with lowercase letters and hyphens
I'm trying to replace the capital letters in an entire string with their lower case counter parts while adding hyphen after it (unless its the last letter). So that Sunny&Cloudy would be come sunny-&-cloudy

```<code>var name = 'Sunny&Cloudy';
name.replace(/([A-Z])(.*)/, '\L$1');
</code>```

I've tried this this myself but it only reaches the first capital letter adds a hyphen and stops. Leaving me with -S

答:
You can use word boundaries \b to split the string into tokens, map everything to lowercase (except the last letter) and join the result together with a - dash

function dashed(str, sep) {
  let words = str.split(sep);
  return words.map(word => {
    let left = word.slice(0, -1).toLowerCase();
    let lastLetter = word.slice(-1);
    return `${left}${lastLetter}`
  }).join('-')

}

console.log(
  dashed("Sunny&CloudY&Rainy", /\b/)
)```

Body:
I'm trying to replace the capital letters in an entire string with their lower case counter parts while adding hyphen after it (unless its the last letter). So that Sunny&Cloudy would be come sunny-&-cloudy

var name = 'Sunny&Cloudy';
name.replace(/([A-Z])(.*)/, '\L$1');

I've tried this this myself but it only reaches the first capital letter adds a hyphen and stops. Leaving me with -S


问:
Gensim Word2Vec lacks vectors for input words
Gensim Word2Vec that I've trained lacks vectors for some words. That is, although I have a word "yuval" as an input, the model lacks a vector for it. What is the cause?</p>

答:
You either didn't supply 'yuval' as a token with a properly-formatted corpus, or the number of occurrences was below the model's min_count. (It's generally helpful for a Word2Vec model to discard low-frequency words – more data isn't automatically better if there are only a few examples of a word.)

Double-check that 'yuval' appears in the corpus, and how many times, and whether that's sufficient for the word to survive min_count trimming.


问:
2 different Firestore interfaces
I've got 2 different firestore interfaces: both using the same firestore project.

I'm finding this abit confusing - which one am I meant to operate in?

How come the 2nd doesn't have access to other settings such as Rules?

答:
They are both meant for you to operate in.  Which one you use depends on whatever your preference is.  If you prefer to stay in the Firebase ecosystem, then use the Firebase console.  If you prefer to stay in the Google Cloud ecosystem, then use the Cloud console.

Read more about the differences with Cloud Firestore between Firebase and Google Cloud.

Title: 2 different Firestore interfaces


问:
How to solve DataConversion Warning with MinMax normalization
Can anybody tell me how i can remove the below warning? I want to normalize a set of integer values by min-max normalization technique but i am getting this warning and don't know how to solve it? (X is a column of integer values starting from 0 to 127)

Here is the code:

```<code>X = df.iloc[:,0]
mms = MinMaxScaler()
a=X.reshape(-1, 1)
b=mms.fit_transform(a)
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
ax=sns.distplot(b);
ax.set(xlabel='frequency', ylabel='Probability')
plt.show()
</code>```

And here is the warning:

DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler. warnings.warn(msg, DataConversionWarning)
```

答:
MinMaxScaler() works using float numbers, so it will automatically convert your np.array of type np.int to np.float and inform you about it. If you don't want to see this warning, do the conversion explicitly beforehand:

```<code>b = mms.fit_transform(a.astype(np.float))
</code>```

Body:
Can anybody tell me how i can remove the below warning? I want to normalize a set of integer values by min-max normalization technique but i am getting this warning and don't know how to solve it? (X is a column of integer values starting from 0 to 127)

Here is the code:

X = df.iloc[:,0]
mms = MinMaxScaler()
a=X.reshape(-1, 1)
b=mms.fit_transform(a)
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
ax=sns.distplot(b);
ax.set(xlabel='frequency', ylabel='Probability')
plt.show()

And here is the warning:

DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler. warnings.warn(msg, DataConversionWarning)

Body:
MinMaxScaler() works using float numbers, so it will automatically convert your np.array of type np.int to np.float and inform you about it. If you don't want to see this warning, do the conversion explicitly beforehand:

b = mms.fit_transform(a.astype(np.float))


问:
Hide element when click on button and pageload
I cannot get my head around this problem I am having, I hope that my writing is not to complicated to read.

I am making a product with 3 variables. A customer can pickup the products at our place, or we can deliver the products. Because of practical things, we choose to sell products there should be delivered as packages with 30, 60 or 90 products in.

Before a price can be shown, all 3 variables need to get selected. So here comes the problem. If a person wants to pickup the products at our place only 2 variables need to be selected: Length in m and Pickup or Delivery. But that will not give a price. Therefore I need to insert "Pickup" in "Packages" again to get a price.

But I would like to hide "pickup" in packages, so I do not confuse the visitor to much. The image describes what I am looking for:

A demo of the page can be seen here

I was looking at the CSS. Is that what I have to use together with javascript, or how/if it is possible?

```<code><ul class="variable-items-wrapper button-variable-wrapper" data-attribute_name="attribute_pa_packages">
    <li data-wvstooltip="pickup" class="variable-item button-variable-item button-variable-item-pickup" title="pickup" data-value="pickup">
        <span class="variable-item-span variable-item-span-button">pickup</span>
    </li>
</ul>

<ul class="variable-items-wrapper button-variable-wrapper" data-attribute_name="attribute_pa_packages">
    <li data-wvstooltip="pickup" class="variable-item button-variable-item button-variable-item-pickup selected" title="pickup" data-value="pickup">
        <span class="variable-item-span variable-item-span-button">pickup</span></li>
    </li>
</ul>
</code>```

Best regards.

答:
You can listen for every time user clicks on one of the "Pickup or delivery" options, if the selected option is "pickup", select the "pickup" option in "Packages" with JQuery. This is the implementation of this algorithm.</p>

```<code>jQuery("ul[data-attribute_name=attribute_pa_pickup-og-delivery] li").click(function(){
    var selectedDelivery = jQuery("ul[data-attribute_name=attribute_pa_pickup-og-delivery]").find("li.selected").attr("data-value");
    if (selectedDelivery == "pickup"){
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=pickup] span").click();
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=pickup]").hide(); // hide the "pickup" option in "Packages" if you like
    }
});
</code>```

Body:
I cannot get my head around this problem I am having, I hope that my writing is not to complicated to read.

I am making a product with 3 variables. A customer can pickup the products at our place, or we can deliver the products. Because of practical things, we choose to sell products there should be delivered as packages with 30, 60 or 90 products in.

Before a price can be shown, all 3 variables need to get selected. So here comes the problem. If a person wants to pickup the products at our place only 2 variables need to be selected: Length in m and Pickup or Delivery. But that will not give a price. Therefore I need to insert "Pickup" in "Packages" again to get a price.

But I would like to hide "pickup" in packages, so I do not confuse the visitor to much. The image describes what I am looking for:

A demo of the page can be seen here

I was looking at the CSS. Is that what I have to use together with javascript, or how/if it is possible?

        pickup

        pickup

Best regards.

Body:
You can listen for every time user clicks on one of the "Pickup or delivery" options, if the selected option is "pickup", select the "pickup" option in "Packages" with JQuery. This is the implementation of this algorithm.

jQuery("ul[data-attribute_name=attribute_pa_pickup-og-delivery] li").click(function(){
    var selectedDelivery = jQuery("ul[data-attribute_name=attribute_pa_pickup-og-delivery]").find("li.selected").attr("data-value");
    if (selectedDelivery == "pickup"){
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=pickup] span").click();
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=pickup]").hide(); // hide the "pickup" option in "Packages" if you like
    }
});


问:
Spark Caused by: java.lang.StackOverflowError Window Function?
Running into an error I think being caused by the Window Function.

When I apply this script and persist just a few sample rows it works fine however when I apply it to my whole dataset (only a few GB)
it fails with this bizarre error on the last step when trying to persist to hdfs ... the script works when I persist w/o the Window Function so the problem must be from that (I have around 325 feature columns running through the for loop).

Any idea what could be causing the problem?  My goal is to just impute time series data via forward fill method on every variable in my dataframe.

from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql import types as T
from pyspark.sql import Window
import sys
print(spark.version)
'2.3.0'

# sample data
df = spark.createDataFrame([('2019-05-10 7:30:05', '1', '10', '0.5', 'FALSE'),\
                            ('2019-05-10 7:30:10', '2', 'UNKNOWN', '0.24', 'FALSE'),\
                            ('2019-05-10 7:30:15', '3', '6', 'UNKNOWN', 'TRUE'),\
                            ('2019-05-10 7:30:20', '4', '7', 'UNKNOWN', 'UNKNOWN'),\
                            ('2019-05-10 7:30:25', '5', '10', '1.1', 'UNKNOWN'),\
                            ('2019-05-10 7:30:30', '6', 'UNKNOWN', '1.1', 'NULL'),\
                            ('2019-05-10 7:30:35', '7', 'UNKNOWN', 'UNKNOWN', 'TRUE'),\
                            ('2019-05-10 7:30:49', '8', '50', 'UNKNOWN', 'UNKNOWN')], ["date", "id", "v1", "v2", "v3"])

df = df.withColumn("date", F.col("date").cast("timestamp"))

# imputer process / all cols that need filled are strings
def stringReplacer(x, y):
    return F.when(x != y, x).otherwise(F.lit(None)) # replace with NULL

def forwardFillImputer(df, cols=[], partitioner="date", value="UNKNOWN"):
  for i in cols:
    window = Window\
    .partitionBy(F.month(partitioner))\
    .orderBy(partitioner)\
    .rowsBetween(-sys.maxsize, 0)

    df = df\
    .withColumn(i, stringReplacer(F.col(i), value))
    fill = F.last(df[i], ignorenulls=True).over(window)
    df = df.withColumn(i,  fill)
  return df
df2 = forwardFillImputer(df, cols=[i for i in df.columns])

# errors here
df2\
.write\
.format("csv")\
.mode("overwrite")\
.option("header", "true")\
.save("test_window_func.csv")
```

<pre class="lang-py prettyprint-override"><code>Py4JJavaError: An error occurred while calling o13504.save.
: org.apache.spark.SparkException: Job aborted.
    at org.apache.spark.sql.execution.datasources.FileFormatWriter$.write(FileFormatWriter.scala:224)
    at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand.run(InsertIntoHadoopFsRelationCommand.scala:154)
    at org.apache.spark.sql.execution.command.DataWritingCommandExec.sideEffectResult$lzycompute(commands.scala:104)
    at org.apache.spark.sql.execution.command.DataWritingCommandExec.sideEffectResult(commands.scala:102)
    at org.apache.spark.sql.execution.command.DataWritingCommandExec.doExecute(commands.scala:122)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:131)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:127)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:155)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:152)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:127)
    at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:80)
    at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:80)
    at org.apache.spark.sql.DataFrameWriter$$anonfun$runCommand$1.apply(DataFrameWriter.scala:654)
    at org.apache.spark.sql.DataFrameWriter$$anonfun$runCommand$1.apply(DataFrameWriter.scala:654)
    at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:77)
    at org.apache.spark.sql.DataFrameWriter.runCommand(DataFrameWriter.scala:654)
    at org.apache.spark.sql.DataFrameWriter.saveToV1Source(DataFrameWriter.scala:273)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:267)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:225)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.StackOverflowError
    at org.apache.spark.sql.execution.SparkPlan.prepare(SparkPlan.scala:200)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$prepare$1.apply(SparkPlan.scala:200)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$prepare$1.apply(SparkPlan.scala:200)
    at scala.collection.immutable.List.foreach(List.scala:381)
</code>```

possible working solution

```<code>def forwardFillImputer(df, cols=[], partitioner="date", value="UNKNOWN"):
    window = Window \
     .partitionBy(F.month(partitioner)) \
     .orderBy(partitioner) \
     .rowsBetween(-sys.maxsize, 0)
    imputed_cols = [F.last(stringReplacer(F.col(i), value), ignorenulls=True).over(window).alias(i)
                    for i in cols]
    missing_cols = [i for i in df.columns if i not in cols]
    return df.select(missing_cols+imputed_cols)

df2 = forwardFillImputer(df, cols=[i for i in df.columns[1:]])

df2.printSchema()
root
 |-- date: timestamp (nullable = true)
 |-- id: string (nullable = true)
 |-- v1: string (nullable = true)
 |-- v2: string (nullable = true)
 |-- v3: string (nullable = true)

df2.show()
+-------------------+---+---+----+-----+
|               date| id| v1|  v2|   v3|
+-------------------+---+---+----+-----+
|2019-05-10 07:30:05|  1| 10| 0.5|FALSE|
|2019-05-10 07:30:10|  2| 10|0.24|FALSE|
|2019-05-10 07:30:15|  3|  6|0.24| TRUE|
|2019-05-10 07:30:20|  4|  7|0.24| TRUE|
|2019-05-10 07:30:25|  5| 10| 1.1| TRUE|
|2019-05-10 07:30:30|  6| 10| 1.1| NULL|
|2019-05-10 07:30:35|  7| 10| 1.1| TRUE|
|2019-05-10 07:30:49|  8| 50| 1.1| TRUE|
+-------------------+---+---+----+-----+
</code>```

答:
By the stacktrace provided I believe the error comes from preparation of the execution plan, as it says:

```<code>Caused by: java.lang.StackOverflowError
    at org.apache.spark.sql.execution.SparkPlan.prepare(SparkPlan.scala:200)
</code>```

I believe that the reason for that is because you call the method .withColumn twice in the loop. What .withColumn does in the Spark execution plan is basically a select statement of all columns with 1 column changed as specified in the method. If you have 325 columns, then for single iteration this will call select on 325 columns twice -> 650 columns passed into the planner. Doing this 325 times you can see how it can create an overhead.

However it is very interesting though that you do not receive this error for a small sample, I'd expect otherwise.

Anyway you can try replacing your forwardFillImputer like this:

```<code>def forwardFillImputer(df, cols=[], partitioner="date", value="UNKNOWN"):
    window = Window \
     .partitionBy(F.month(partitioner)) \
     .orderBy(partitioner) \
     .rowsBetween(-sys.maxsize, 0)

    imputed_cols = [F.last(stringReplacer(F.col(i), value), ignorenulls=True).over(window).alias(i)
                    for i in cols]

    missing_cols = [F.col(i) for i in df.columns if i not in cols]

    return df.select(missing_cols + imputed_cols)
</code>```

This way you basically just parse into planner a single select statement, which should be easier to handle.

Just as a warning, generally Spark doesn't do well with high number of columns, so you might see other strange issues along the way.

Body:
Running into an error I think being caused by the Window Function.

When I apply this script and persist just a few sample rows it works fine however when I apply it to my whole dataset (only a few GB)
it fails with this bizarre error on the last step when trying to persist to hdfs ... the script works when I persist w/o the Window Function so the problem must be from that (I have around 325 feature columns running through the for loop).

Any idea what could be causing the problem?  My goal is to just impute time series data via forward fill method on every variable in my dataframe.

from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql import types as T
from pyspark.sql import Window
import sys
print(spark.version)
'2.3.0'

# sample data
df = spark.createDataFrame([('2019-05-10 7:30:05', '1', '10', '0.5', 'FALSE'),\
                            ('2019-05-10 7:30:10', '2', 'UNKNOWN', '0.24', 'FALSE'),\
                            ('2019-05-10 7:30:15', '3', '6', 'UNKNOWN', 'TRUE'),\
                            ('2019-05-10 7:30:20', '4', '7', 'UNKNOWN', 'UNKNOWN'),\
                            ('2019-05-10 7:30:25', '5', '10', '1.1', 'UNKNOWN'),\
                            ('2019-05-10 7:30:30', '6', 'UNKNOWN', '1.1', 'NULL'),\
                            ('2019-05-10 7:30:35', '7', 'UNKNOWN', 'UNKNOWN', 'TRUE'),\
                            ('2019-05-10 7:30:49', '8', '50', 'UNKNOWN', 'UNKNOWN')], ["date", "id", "v1", "v2", "v3"])

df = df.withColumn("date", F.col("date").cast("timestamp"))

# imputer process / all cols that need filled are strings
def stringReplacer(x, y):
    return F.when(x != y, x).otherwise(F.lit(None)) # replace with NULL

def forwardFillImputer(df, cols=[], partitioner="date", value="UNKNOWN"):
  for i in cols:
    window = Window\
    .partitionBy(F.month(partitioner))\
    .orderBy(partitioner)\
    .rowsBetween(-sys.maxsize, 0)

    df = df\
    .withColumn(i, stringReplacer(F.col(i), value))
    fill = F.last(df[i], ignorenulls=True).over(window)
    df = df.withColumn(i,  fill)
  return df
df2 = forwardFillImputer(df, cols=[i for i in df.columns])

# errors here
df2\
.write\
.format("csv")\
.mode("overwrite")\
.option("header", "true")\
.save("test_window_func.csv")

Py4JJavaError: An error occurred while calling o13504.save.
: org.apache.spark.SparkException: Job aborted.
    at org.apache.spark.sql.execution.datasources.FileFormatWriter$.write(FileFormatWriter.scala:224)
    at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand.run(InsertIntoHadoopFsRelationCommand.scala:154)
    at org.apache.spark.sql.execution.command.DataWritingCommandExec.sideEffectResult$lzycompute(commands.scala:104)
    at org.apache.spark.sql.execution.command.DataWritingCommandExec.sideEffectResult(commands.scala:102)
    at org.apache.spark.sql.execution.command.DataWritingCommandExec.doExecute(commands.scala:122)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:131)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:127)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:155)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:152)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:127)
    at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:80)
    at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:80)
    at org.apache.spark.sql.DataFrameWriter$$anonfun$runCommand$1.apply(DataFrameWriter.scala:654)
    at org.apache.spark.sql.DataFrameWriter$$anonfun$runCommand$1.apply(DataFrameWriter.scala:654)
    at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:77)
    at org.apache.spark.sql.DataFrameWriter.runCommand(DataFrameWriter.scala:654)
    at org.apache.spark.sql.DataFrameWriter.saveToV1Source(DataFrameWriter.scala:273)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:267)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:225)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.StackOverflowError
    at org.apache.spark.sql.execution.SparkPlan.prepare(SparkPlan.scala:200)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$prepare$1.apply(SparkPlan.scala:200)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$prepare$1.apply(SparkPlan.scala:200)
    at scala.collection.immutable.List.foreach(List.scala:381)

possible working solution

def forwardFillImputer(df, cols=[], partitioner="date", value="UNKNOWN"):
    window = Window \
     .partitionBy(F.month(partitioner)) \
     .orderBy(partitioner) \
     .rowsBetween(-sys.maxsize, 0)
    imputed_cols = [F.last(stringReplacer(F.col(i), value), ignorenulls=True).over(window).alias(i)
                    for i in cols]
    missing_cols = [i for i in df.columns if i not in cols]
    return df.select(missing_cols+imputed_cols)

df2 = forwardFillImputer(df, cols=[i for i in df.columns[1:]])

df2.printSchema()
root
 |-- date: timestamp (nullable = true)
 |-- id: string (nullable = true)
 |-- v1: string (nullable = true)
 |-- v2: string (nullable = true)
 |-- v3: string (nullable = true)

df2.show()
+-------------------+---+---+----+-----+
|               date| id| v1|  v2|   v3|
+-------------------+---+---+----+-----+
|2019-05-10 07:30:05|  1| 10| 0.5|FALSE|
|2019-05-10 07:30:10|  2| 10|0.24|FALSE|
|2019-05-10 07:30:15|  3|  6|0.24| TRUE|
|2019-05-10 07:30:20|  4|  7|0.24| TRUE|
|2019-05-10 07:30:25|  5| 10| 1.1| TRUE|
|2019-05-10 07:30:30|  6| 10| 1.1| NULL|
|2019-05-10 07:30:35|  7| 10| 1.1| TRUE|
|2019-05-10 07:30:49|  8| 50| 1.1| TRUE|
+-------------------+---+---+----+-----+

Body:
By the stacktrace provided I believe the error comes from preparation of the execution plan, as it says:

Caused by: java.lang.StackOverflowError
    at org.apache.spark.sql.execution.SparkPlan.prepare(SparkPlan.scala:200)

I believe that the reason for that is because you call the method .withColumn twice in the loop. What .withColumn does in the Spark execution plan is basically a select statement of all columns with 1 column changed as specified in the method. If you have 325 columns, then for single iteration this will call select on 325 columns twice -> 650 columns passed into the planner. Doing this 325 times you can see how it can create an overhead.

However it is very interesting though that you do not receive this error for a small sample, I'd expect otherwise.

Anyway you can try replacing your forwardFillImputer like this:

def forwardFillImputer(df, cols=[], partitioner="date", value="UNKNOWN"):
    window = Window \
     .partitionBy(F.month(partitioner)) \
     .orderBy(partitioner) \
     .rowsBetween(-sys.maxsize, 0)

    imputed_cols = [F.last(stringReplacer(F.col(i), value), ignorenulls=True).over(window).alias(i)
                    for i in cols]

    missing_cols = [F.col(i) for i in df.columns if i not in cols]

    return df.select(missing_cols + imputed_cols)

This way you basically just parse into planner a single select statement, which should be easier to handle.

Just as a warning, generally Spark doesn't do well with high number of columns, so you might see other strange issues along the way.


问:
How do I find mat-select using selenium webdriver
I am using selenium web driver to automate an angular website. I couldn't able to use select commands. because the error shown as
Element should have been "select" but was "mat-select"
enter image description here

enter image description here

```<code><div class="mat-input-infix mat-form-field-infix"> <mat-select _ngcontent-c2="" class="mat-select ng-tns-c6-1 ng-pristine ng-valid ng-touched" role="listbox" ng-reflect-is-disabled="false" ng-reflect-model="award" ng-reflect-disabled="false" id="mat-select-0" tabindex="0" aria-required="false" aria-disabled="false" aria-invalid="false" aria-owns="mat-option-1 mat-option-2" aria-multiselectable="false"><div class="mat-select-trigger" aria-hidden="true" cdk-overlay-origin=""><div class="mat-select-value"><!--bindings={
  "ng-reflect-ng-if": "false"
}--><!--bindings={
  "ng-reflect-ng-if": "true"
}--><span class="mat-select-value-text ng-tns-c6-1" ng-reflect-ng-switch="false"><!----><span class="ng-tns-c6-1">Award</span><!--bindings={
  "ng-reflect-ng-switch-case": "true"
}--></span></div><div class="mat-select-arrow-wrapper"><div class="mat-select-arrow"></div></div></div><!--bindings={
  "ng-reflect-_deprecated-origin": "[object Object]",
  "ng-reflect-_deprecated-positions": "[object Object],[object Object",
  "ng-reflect-_deprecated-offset-y": "-15",
  "ng-reflect-_deprecated-min-width": "206",
  "ng-reflect-_deprecated-backdrop-class": "cdk-overlay-transparent-backdr",
  "ng-reflect-_deprecated-scroll-strategy": "[object Object]",
  "ng-reflect-_deprecated-open": "false",
  "ng-reflect-_deprecated-has-backdrop": ""
}--></mat-select> <span class="mat-input-placeholder-wrapper mat-form-field-placeholder-wrapper"><!--bindings={
  "ng-reflect-ng-if": "false"
}--></span></div>
<span class="mat-option-text">Institute Proposal</span>
</code>```

答:
I have solved the problem like this:

In the component's html code, i have created the id of each of the elements dynamically:

<mat-select [(ngModel)]="idRoom" id="roomSelected" name="roomSelected">
  <mat-option [value]="null">-- Select a room --</mat-option>
  <mat-option *ngFor="let room of rooms; let i = index" [value]="room.id" [id]="'room' + i">
    {{room.identifier}} - {{room.type}}
  </mat-option>
</mat-select>
```

<p>Then I have created the next method in my class Selenium:</p>

<pre class="lang-java prettyprint-override"><code>private WebDriver driver;

public TestUtil(WebDriver driver) {
    this.driver = driver;
}

public void matSelectInput(String id, String value) {
    driver.findElement(By.id(id)).click();
    driver.findElement(By.id(value)).click();
}
</code>```

Finally, to make the test I add the following call:

@Test
public void addOnSuccess() {
    ...
    testUtil.matSelectInput("roomSelected", "room0");
    ...
}
```

Body:
I am using selenium web driver to automate an angular website. I couldn't able to use select commands. because the error shown as
Element should have been "select" but was "mat-select"
enter image description here

enter image description here

 <mat-select _ngcontent-c2="" class="mat-select ng-tns-c6-1 ng-pristine ng-valid ng-touched" role="listbox" ng-reflect-is-disabled="false" ng-reflect-model="award" ng-reflect-disabled="false" id="mat-select-0" tabindex="0" aria-required="false" aria-disabled="false" aria-invalid="false" aria-owns="mat-option-1 mat-option-2" aria-multiselectable="false">

Award

</mat-select>

Institute Proposal

Body:
I have solved the problem like this:

In the component's html code, i have created the id of each of the elements dynamically:

<mat-select [(ngModel)]="idRoom" id="roomSelected" name="roomSelected">
  <mat-option [value]="null">-- Select a room --</mat-option>
  <mat-option *ngFor="let room of rooms; let i = index" [value]="room.id" [id]="'room' + i">
    {{room.identifier}} - {{room.type}}
  </mat-option>
</mat-select>

Then I have created the next method in my class Selenium:

private WebDriver driver;

public TestUtil(WebDriver driver) {
    this.driver = driver;
}

public void matSelectInput(String id, String value) {
    driver.findElement(By.id(id)).click();
    driver.findElement(By.id(value)).click();
}

Finally, to make the test I add the following call:

@Test
public void addOnSuccess() {
    ...
    testUtil.matSelectInput("roomSelected", "room0");
    ...
}


问:
Data is being sent via $http to php file but JSON decodes blank or undefined?
I am using $http through AngularJS to send data to a PHP document which is intended to save the data in a MySQL database. However, the data is being decoded blank or undefined. The JSON makes it to the PHP file, as I can see the request headers, but the response is blank.

I have tried testing different variations of the code to make sure that the JSON-encoded data makes it to the PHP document, and it does, but when attempting to json_decode() it does not pull anything from the JSON.

PHP File

```<code>$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$user = $request->Username;
echo $user;
</code>```

AngularJS

```<code>$scope.submit = function() {
        $http({
            url: "http://www.walkermediadesign.com/planner3/src/ceremony.php",
            method: "POST",
            data: this.ceremony
        }).then(function successCallback(response) {
            console.log(response.data);
        }, function errorCallback(response) {
            $scope.error = response.statusText;
    })};
</code>```

This is the post data:

```<code>$postdata =
(2) [{…}, {…}]
0: {Username: "redphyre@gmail.com"}
1: {opening: "Friends and Family of BRIDE and GROOM, welcome and…d
falling in love with each other all over again."}
length: 2
__proto__: Array(0)
</code>```

There are no error messages or 500 errors, just blank data being returned.

答:
I think you were expecting JSON data that looked like this:

```<code>{
    "Username": "redphyre@gmail.com",
    "opening": "Friends and Family..."
}
</code>```

Where you have a single object with all the expected properties.

What you are actually getting, however, is this:

```<code>[
    { "Username": "redphyre@gmail.com" },
    { "opening": "Friends and Family..." }
]
</code>```

That creates an array of objects, each with only one property, which is not nearly as easy to work with. To convert the data to a single object with multiple properties, you can loop though your result set:

```<code>$responseData = new stdClass();

foreach ($response as $propertyObject) {
    $properties = get_object_vars($propertyObject);

    // Just in case some objects have more than one property after all
    foreach($properties as $name => $value) {
        $responseData->$name = $value;
    }
}
</code>```

That will copy the individual properties of the objects in the response array into a single object.

Body:
I am using $http through AngularJS to send data to a PHP document which is intended to save the data in a MySQL database. However, the data is being decoded blank or undefined. The JSON makes it to the PHP file, as I can see the request headers, but the response is blank.

I have tried testing different variations of the code to make sure that the JSON-encoded data makes it to the PHP document, and it does, but when attempting to json_decode() it does not pull anything from the JSON.

PHP File

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$user = $request->Username;
echo $user;

AngularJS

$scope.submit = function() {
        $http({
            url: "http://www.walkermediadesign.com/planner3/src/ceremony.php",
            method: "POST",
            data: this.ceremony
        }).then(function successCallback(response) {
            console.log(response.data);
        }, function errorCallback(response) {
            $scope.error = response.statusText;
    })};

This is the post data:

$postdata =
(2) [{…}, {…}]
0: {Username: "redphyre@gmail.com"}
1: {opening: "Friends and Family of BRIDE and GROOM, welcome and…d
falling in love with each other all over again."}
length: 2
__proto__: Array(0)

There are no error messages or 500 errors, just blank data being returned.

Body:
I think you were expecting JSON data that looked like this:

{
    "Username": "redphyre@gmail.com",
    "opening": "Friends and Family..."
}

Where you have a single object with all the expected properties.

What you are actually getting, however, is this:

[
    { "Username": "redphyre@gmail.com" },
    { "opening": "Friends and Family..." }
]

That creates an array of objects, each with only one property, which is not nearly as easy to work with. To convert the data to a single object with multiple properties, you can loop though your result set:

$responseData = new stdClass();

foreach ($response as $propertyObject) {
    $properties = get_object_vars($propertyObject);

    // Just in case some objects have more than one property after all
    foreach($properties as $name => $value) {
        $responseData->$name = $value;
    }
}

That will copy the individual properties of the objects in the response array into a single object.