问:
How to send such complicated hex, binary protocol data accurately using java byte[]?
I am very confused with this kind of stuffs, What should I send as final command, get always confused 8 bits to 1 byte but how do we make it? Is it only the command packet [hex] as shown in screen shot? or Is it header + command packet[hex] shown in two screen shots?

Confusion details:

In such diagram header block shows mostly like "Bit 7, Bit 6,..,Bit 0" instead of "Bit 0, Bit 1, Bit 2, ... Bit 7", i always wondering why?.

But when I apply in code what is following order for byte st[0] = Bit 7 or Bit 1?

Also according to this diagram, does it mean every command I send will have Header fixed always?

This is the code I was trying by taking Bit 1 as st[0], Bit 2 as st1 instead of Bit 7 as st[0]. To apply Power off/on test.

```<code>import java.io.*;
import java.net.*;
public class test
{

public static void main(String[] args) throws UnknownHostException, IOException
{

    byte st[]=new byte[256];
    st[0]=0x01; // get
    st[1]=0x02; // header 2 byte
    st[2]=0x02; // header length
    st[3]=0;    // command byte
    st[4]=0;    // reserved
    st[5]=0;
    st[6]=0;
    st[7]=0;
    st[8]=0x01; // power off
    st[9]=0x30;
    st[10]=0x01;
    st[11]=0x00;
    System.out.println(st); // Should this work, am i correct based on diagram?

    Socket s           = new Socket("192.168.1.2", 49137);
    DataInputStream input         = new DataInputStream(s.getInputStream());
    DataOutputStream outToServer  = new DataOutputStream(s.getOutputStream());
    BufferedReader i   = new BufferedReader(new InputStreamReader(s.getInputStream()));
    outToServer.write(st);
    String get;
    get = i.readLine();
    System.out.println("FROM SERVER: " + get);
    s.close();
}
</code>```

}

P.S: How would you do this really? Every hex command make hand by hand, this PDF file has almost 100 commands, it would take lot of time. Or you manage them in a different way?

答:
You don't need to care about bit order because IP packets contain bytes and the lower level components make sure every byte is transferred correctly.

Instead of System.out.println(st) I'd make a small method that prints the command buffer nicely in hexadeciaml.

Body:
I am very confused with this kind of stuffs, What should I send as final command, get always confused 8 bits to 1 byte but how do we make it? Is it only the command packet [hex] as shown in screen shot? or Is it header + command packet[hex] shown in two screen shots?

Confusion details:

In such diagram header block shows mostly like "Bit 7, Bit 6,..,Bit 0" instead of "Bit 0, Bit 1, Bit 2, ... Bit 7", i always wondering why?.

But when I apply in code what is following order for byte st[0] = Bit 7 or Bit 1?

Also according to this diagram, does it mean every command I send will have Header fixed always?

This is the code I was trying by taking Bit 1 as st[0], Bit 2 as st1 instead of Bit 7 as st[0]. To apply Power off/on test.

import java.io.*;
import java.net.*;
public class test
{

public static void main(String[] args) throws UnknownHostException, IOException
{

    byte st[]=new byte[256];
    st[0]=0x01; // get
    st[1]=0x02; // header 2 byte
    st[2]=0x02; // header length
    st[3]=0;    // command byte
    st[4]=0;    // reserved
    st[5]=0;
    st[6]=0;
    st[7]=0;
    st[8]=0x01; // power off
    st[9]=0x30;
    st[10]=0x01;
    st[11]=0x00;
    System.out.println(st); // Should this work, am i correct based on diagram?

    Socket s           = new Socket("192.168.1.2", 49137);
    DataInputStream input         = new DataInputStream(s.getInputStream());
    DataOutputStream outToServer  = new DataOutputStream(s.getOutputStream());
    BufferedReader i   = new BufferedReader(new InputStreamReader(s.getInputStream()));
    outToServer.write(st);
    String get;
    get = i.readLine();
    System.out.println("FROM SERVER: " + get);
    s.close();
}

}

P.S: How would you do this really? Every hex command make hand by hand, this PDF file has almost 100 commands, it would take lot of time. Or you manage them in a different way?


来源: stackexchange

问:
Apache Thrift 0.15.0 compatibility with 0.13.0
I'm upgrading my Apache Thrift from version 0.13.0 to 0.15.0 because of vulnerability CVE-2020-13949. But I need to comunicate with another system using Thrift that I'm not managing and couldn't upgrade to that new version. My question: Does the new version 0.15.0 have backwards compatibility with 0.13.0? And how to know about that, where could I ask for compatibility?</p>

答:
There's two answers to your first question

Wire format: Yes it is backwards compatible, has not changed.

Source code: There may be breaking changes depending on the target language(s) you are using.

And for the second part:

Apache Thrift mailing lists

Change log 0.15.0

Change log 0.14.2

Change log 0.14.1

Change log 0.14.0

ContentLicense: CC BY-SA 4.0


问:
Apache NiFi closes immediately with error, org.apache.nifi.StdErr Failed to start web server
I have recently installed NiFi, and it was working fine for few days. But suddenly today when i try to open it using run-nifi.bat, the NiFi window is getting closed in few seconds stating the below error:

```<code>    2019-04-11 23:07:40,146 WARN [NiFi Bootstrap Command Listener] org.apache.nifi.bootstrap.RunNiFi Failed to set permissions so that only the owner can read status file C:\Users\DOWNLO~1\NIFI-1~1.1-B\NIFI-1~1.1\bin\..\run\nifi.status; this may allows others to have access to the key needed to communicate with NiFi. Permissions should be changed so that only the owner can read this file
    2019-04-11 23:07:40,149 INFO [NiFi Bootstrap Command Listener] org.apache.nifi.bootstrap.RunNiFi Apache NiFi now running and listening for Bootstrap requests on port 54149
    2019-04-11 23:08:00,352 ERROR [NiFi logging handler] org.apache.nifi.StdErr Failed to start web server: Must configure HTTP or HTTPS connector
    2019-04-11 23:08:00,352 ERROR [NiFi logging handler] org.apache.nifi.StdErr Shutting down...
    2019-04-11 23:08:00,419 INFO [main] org.apache.nifi.bootstrap.RunNiFi NiFi never started. Will not restart NiFi
</code>```

I do looked out for the org.apache.nifi.StdErr Failed to start web server: Must configure HTTP or HTTPS connector error, but unfortunately I cant find a similar one. I'm sure that no settings or properties has been changed since installation. Any suggestion guys?

答:
Keep one property enable like https or http. in my case https:127.0.0.1:8443/nifi/

it works fine me.

Body:
I have recently installed NiFi, and it was working fine for few days. But suddenly today when i try to open it using run-nifi.bat, the NiFi window is getting closed in few seconds stating the below error:

    2019-04-11 23:07:40,146 WARN [NiFi Bootstrap Command Listener] org.apache.nifi.bootstrap.RunNiFi Failed to set permissions so that only the owner can read status file C:\Users\DOWNLO~1\NIFI-1~1.1-B\NIFI-1~1.1\bin\..\run\nifi.status; this may allows others to have access to the key needed to communicate with NiFi. Permissions should be changed so that only the owner can read this file
    2019-04-11 23:07:40,149 INFO [NiFi Bootstrap Command Listener] org.apache.nifi.bootstrap.RunNiFi Apache NiFi now running and listening for Bootstrap requests on port 54149
    2019-04-11 23:08:00,352 ERROR [NiFi logging handler] org.apache.nifi.StdErr Failed to start web server: Must configure HTTP or HTTPS connector
    2019-04-11 23:08:00,352 ERROR [NiFi logging handler] org.apache.nifi.StdErr Shutting down...
    2019-04-11 23:08:00,419 INFO [main] org.apache.nifi.bootstrap.RunNiFi NiFi never started. Will not restart NiFi

I do looked out for the org.apache.nifi.StdErr Failed to start web server: Must configure HTTP or HTTPS connector error, but unfortunately I cant find a similar one. I'm sure that no settings or properties has been changed since installation. Any suggestion guys?


问:
livelossplot keras plot per epoch

I'm trying to plot a cost function and accurancy function in Keras.
However, when I want to play with my times of epoch, for instance 100 times, I will get 100 >plots
in phyton, which I have to delete the n epoch plot before the  n + 1 epoch plot will be presented.
I would like to have one figure where I can see all the 100 epoch times once.
my code:

```<code>import pandas as pd
forecast_demo = pd.read_csv('forcastdemo.csv')
forecast_demo.head()

X_train = forecast_demo[['index', 'quarter']]
y_train = forecast_demo.revenue
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Model
from tensorflow.keras import models

from tensorflow.keras.utils import plot_model
from tensorflow.keras.layers import Input, Dense, BatchNormalization
from IPython.core.display import Image

inputs = Input(shape=(2,))
x = BatchNormalization()(inputs)
x = Dense(512, activation='relu')(x)
x = Dense(128, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(32, activation='relu')(x)
outputs = Dense(1)(x)

model = Model(inputs, outputs)
model.summary()
keras.utils.plot_model(model,to_file='images/oefening2.png', show_shapes=True)
Image('images/oefening2.png')

model.compile(
    loss=keras.losses.mean_squared_error, # of keras.losses.mean_absolute_percentage_error, keras.losses.mean_absolute_error (voor regressie)
    optimizer=keras.optimizers.Adam(),
    metrics=keras.metrics.mean_absolute_percentage_error # of keras.metrics.mean_absolute_error, keras.metrics.mean_squared_error
)

from livelossplot import PlotLossesKeras

## here I'll get 100 plots.
history = model.fit(X_train, y_train,
                    batch_size=20,
                    epochs=100,
                    callbacks=[PlotLossesKeras()],
                    verbose=False)

</code>```

答:
It gives the same result what you are expecting (one figure with 100  epoch times plotted).
I ran the above code with abalone dataset in Google colab using Tensorflow version==2.7.

Please check here:

```<code>from livelossplot import PlotLossesKeras

history = model.fit(abalone_features, abalone_labels,
                    batch_size=20,
                    epochs=100,
                    verbose=2,
                    callbacks=[PlotLossesKeras()])
                    #verbose=False)
</code>```

Output:

```<code>Loss
    loss                 (min:    4.342, max:   16.475, cur:    4.358)
mean_absolute_percentage_error
    mean_absolute_percentage_error   (min:   15.522, max:   29.032, cur:   15.898)
166/166 - 1s - loss: 4.3578 - mean_absolute_percentage_error: 15.8985 - 694ms/epoch - 4ms/step
</code>```

Please specify the TensorFlow version and environment details where you are getting this issue.

Title: livelossplot keras plot per epoch

Body:
I'm trying to plot a cost function and accurancy function in Keras.
However, when I want to play with my times of epoch, for instance 100 times, I will get 100 >plots
in phyton, which I have to delete the n epoch plot before the  n + 1 epoch plot will be presented.
I would like to have one figure where I can see all the 100 epoch times once.
my code:

import pandas as pd
forecast_demo = pd.read_csv('forcastdemo.csv')
forecast_demo.head()

X_train = forecast_demo[['index', 'quarter']]
y_train = forecast_demo.revenue
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Model
from tensorflow.keras import models

from tensorflow.keras.utils import plot_model
from tensorflow.keras.layers import Input, Dense, BatchNormalization
from IPython.core.display import Image

inputs = Input(shape=(2,))
x = BatchNormalization()(inputs)
x = Dense(512, activation='relu')(x)
x = Dense(128, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(32, activation='relu')(x)
outputs = Dense(1)(x)

model = Model(inputs, outputs)
model.summary()
keras.utils.plot_model(model,to_file='images/oefening2.png', show_shapes=True)
Image('images/oefening2.png')

model.compile(
    loss=keras.losses.mean_squared_error, # of keras.losses.mean_absolute_percentage_error, keras.losses.mean_absolute_error (voor regressie)
    optimizer=keras.optimizers.Adam(),
    metrics=keras.metrics.mean_absolute_percentage_error # of keras.metrics.mean_absolute_error, keras.metrics.mean_squared_error
)

from livelossplot import PlotLossesKeras

## here I'll get 100 plots.
history = model.fit(X_train, y_train,
                    batch_size=20,
                    epochs=100,
                    callbacks=[PlotLossesKeras()],
                    verbose=False)

Body:
It gives the same result what you are expecting (one figure with 100  epoch times plotted).
I ran the above code with abalone dataset in Google colab using Tensorflow version==2.7.

Please check here:

from livelossplot import PlotLossesKeras

history = model.fit(abalone_features, abalone_labels,
                    batch_size=20,
                    epochs=100,
                    verbose=2,
                    callbacks=[PlotLossesKeras()])
                    #verbose=False)

Output:

Loss
    loss                 (min:    4.342, max:   16.475, cur:    4.358)
mean_absolute_percentage_error
    mean_absolute_percentage_error   (min:   15.522, max:   29.032, cur:   15.898)
166/166 - 1s - loss: 4.3578 - mean_absolute_percentage_error: 15.8985 - 694ms/epoch - 4ms/step

Please specify the TensorFlow version and environment details where you are getting this issue.


问:
How to execute XPath one-liners from shell?
Is there a package out there, for Ubuntu and/or CentOS, that has a command-line tool that can execute an XPath one-liner like foo //element@attribute filename.xml or foo //element@attribute < filename.xml and return the results line by line?

I'm looking for something that would allow me to just apt-get install foo or yum install foo and then just works out-of-the-box, no wrappers or other adaptation necessary.

Here are some examples of things that come close:

Nokogiri. If I write this wrapper I could call the wrapper in the way described above:

```<code>#!/usr/bin/ruby

require 'nokogiri'

Nokogiri::XML(STDIN).xpath(ARGV[0]).each do |row|
  puts row
end
</code>```

XML::XPath. Would work with this wrapper:

```<code>#!/usr/bin/perl

use strict;
use warnings;
use XML::XPath;

my $root = XML::XPath->new(ioref => 'STDIN');
for my $node ($root->find($ARGV[0])->get_nodelist) {
  print($node->getData, "\n");
}
</code>```

xpath from XML::XPath returns too much noise, -- NODE -- and attribute = "value".

xml_grep from XML::Twig cannot handle expressions that do not return elements, so cannot be used to extract attribute values without further processing.

EDIT:

echo cat //element/@attribute | xmllint --shell filename.xml returns noise similar to xpath.

xmllint --xpath //element/@attribute filename.xml returns attribute = "value".

xmllint --xpath 'string(//element/@attribute)' filename.xml returns what I want, but only for the first match.

For another solution almost satisfying the question, here is an XSLT that can be used to evaluate arbitrary XPath expressions (requires dyn:evaluate support in the XSLT processor):

```<code><?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">
  <xsl:output omit-xml-declaration="yes" indent="no" method="text"/>
  <xsl:template match="/">
    <xsl:for-each select="dyn:evaluate($pattern)">
      <xsl:value-of select="dyn:evaluate($value)"/>
      <xsl:value-of select="'
'"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
</code>```

Run with xsltproc --stringparam pattern //element/@attribute --stringparam value . arbitrary-xpath.xslt filename.xml.

答:
A solution that works even when namespace declarations exist on top:

Most of the commands proposed in the answers do not work out of the box if the xml has a namespace declared on top. Consider this:

input xml:

<elem1 xmlns="urn:x" xmlns:prefix="urn:y">
    <elem2 attr1="false" attr2="value2">
        elem2 value
    </elem2>
    <elem2 attr1="true" attr2="value2.1">
        elem2.1 value
    </elem2>
    <prefix:elem3>
        elem3 value
    </prefix:elem3>
</elem1>
```
<p><strong>Does not work:</strong></p>
<pre class="lang-sh prettyprint-override"><code>xmlstarlet sel -t -v "/elem1" input.xml
# nothing printed
xmllint -xpath "/elem1" input.xml
# XPath set is empty
</code>```

Solution:

# Requires >=java11 to run like below (but the code requires >=java17 for case syntax to be recognized)

# Prints the whole document
java ExtractXpath.java "/" example-inputs/input.xml

# Prints the contents and self of "elem1"
java ExtractXpath.java "/elem1" input.xml

# Prints the contents and self of "elem2" whose attr2 value is: 'value2'
java ExtractXpath.java "//elem2[@attr2='value2']" input.xml

# Prints the value of the attribute 'attr2': "value2", "value2.1"
java ExtractXpath.java "/elem1/elem2/@attr2" input.xml

# Prints the text inside elem3: "elem3 value"
java ExtractXpath.java "/elem1/elem3/text()" input.xml

# Prints the name of the matched element: "prefix:elem3"
java ExtractXpath.java "name(/elem1/elem3)" input.xml
# Same as above: "prefix:elem3"
java ExtractXpath.java "name(*/elem3)" input.xml

# Prints the count of the matched elements: 2.0
java ExtractXpath.java "count(/elem2)" input.xml

# known issue: while "//elem2" works. "//elem3" does not (it works only with: '*/elem3' )

```
<p>ExtractXpath.java:</p>
<pre class="lang-java prettyprint-override"><code>
import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathEvaluationResult;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ExtractXpath {

    public static void main(String[] args) throws Exception {
        assertThat(args.length==2, "Wrong number of args");
        String xpath = args[0];
        File file = new File(args[1]);

        assertThat(file.isFile(), file.getAbsolutePath()+" is not a file.");
        FileInputStream fileIS = new FileInputStream(file);
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document xmlDocument = builder.parse(fileIS);
        XPath xPath = XPathFactory.newInstance().newXPath();
        String expression = xpath;
        XPathExpression xpathExpression =  xPath.compile(expression);

        XPathEvaluationResult xpathEvalResult =  xpathExpression.evaluateExpression(xmlDocument);
        System.out.println(applyXpathExpression(xmlDocument, xpathExpression, xpathEvalResult.type().name()));
    }

    private static String applyXpathExpression(Document xmlDocument, XPathExpression expr, String xpathTypeName) throws TransformerConfigurationException, TransformerException, XPathExpressionException {

        // see: https://www.w3.org/TR/1999/REC-xpath-19991116/#corelib
        List<String> retVal = new ArrayList();
        if(xpathTypeName.equals(XPathConstants.NODESET.getLocalPart())){ //e.g. xpath: /elem1/*
            NodeList nodeList = (NodeList)expr.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                retVal.add(convertNodeToString(nodeList.item(i)));
            }
        }else if(xpathTypeName.equals(XPathConstants.STRING.getLocalPart())){ //e.g. xpath: name(/elem1/*)
            retVal.add((String)expr.evaluate(xmlDocument, XPathConstants.STRING));
        }else if(xpathTypeName.equals(XPathConstants.NUMBER.getLocalPart())){ //e.g. xpath: count(/elem1/*)
            retVal.add(((Number)expr.evaluate(xmlDocument, XPathConstants.NUMBER)).toString());
        }else if(xpathTypeName.equals(XPathConstants.BOOLEAN.getLocalPart())){ //e.g. xpath: contains(elem1, 'sth')
            retVal.add(((Boolean)expr.evaluate(xmlDocument, XPathConstants.BOOLEAN)).toString());
        }else if(xpathTypeName.equals(XPathConstants.NODE.getLocalPart())){ //e.g. xpath: fixme: find one
            System.err.println("WARNING found xpathTypeName=NODE");
            retVal.add(convertNodeToString((Node)expr.evaluate(xmlDocument, XPathConstants.NODE)));
        }else{
            throw new RuntimeException("Unexpected xpath type name: "+xpathTypeName+". This should normally not happen");
        }
        return retVal.stream().map(str->"==MATCH_START==\n"+str+"\n==MATCH_END==").collect(Collectors.joining ("\n"));

    }

    private static String convertNodeToString(Node node) throws TransformerConfigurationException, TransformerException {
            short nType = node.getNodeType();
        switch (nType) {
            case Node.ATTRIBUTE_NODE , Node.TEXT_NODE -> {
                return node.getNodeValue();
            }
            case Node.ELEMENT_NODE, Node.DOCUMENT_NODE -> {
                StringWriter writer = new StringWriter();
                Transformer trans = TransformerFactory.newInstance().newTransformer();
                trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                trans.setOutputProperty(OutputKeys.INDENT, "yes");
                trans.transform(new DOMSource(node), new StreamResult(writer));
                return writer.toString();
            }
            default -> {
                System.err.println("WARNING: FIXME: Node type:"+nType+" could possibly be handled in a better way.");
                return node.getNodeValue();
            }

        }
    }

    private static void assertThat(boolean b, String msg) {
        if(!b){
            System.err.println(msg+"\n\nUSAGE: program xpath xmlFile");
            System.exit(-1);
        }
    }
}

@SuppressWarnings("unchecked")
class NamespaceResolver implements NamespaceContext {
    //Store the source document to search the namespaces
    private final Document sourceDocument;
    public NamespaceResolver(Document document) {
        sourceDocument = document;
    }

    //The lookup for the namespace uris is delegated to the stored document.
    @Override
    public String getNamespaceURI(String prefix) {
        if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
            return sourceDocument.lookupNamespaceURI(null);
        } else {
            return sourceDocument.lookupNamespaceURI(prefix);
        }
    }

    @Override
    public String getPrefix(String namespaceURI) {
        return sourceDocument.lookupPrefix(namespaceURI);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Iterator getPrefixes(String namespaceURI) {
        return null;
    }
}

</code>```

and for simplicity:

xpath-extract command:

#!/bin/bash
java ExtractXpath.java "$1" "$2"

```

ContentLicense: CC BY-SA 3.0

Body:
Is there a package out there, for Ubuntu and/or CentOS, that has a command-line tool that can execute an XPath one-liner like foo //element@attribute filename.xml or foo //element@attribute < filename.xml and return the results line by line?

I'm looking for something that would allow me to just apt-get install foo or yum install foo and then just works out-of-the-box, no wrappers or other adaptation necessary.

Here are some examples of things that come close:

Nokogiri. If I write this wrapper I could call the wrapper in the way described above:

#!/usr/bin/ruby

require 'nokogiri'

Nokogiri::XML(STDIN).xpath(ARGV[0]).each do |row|
  puts row
end

XML::XPath. Would work with this wrapper:

#!/usr/bin/perl

use strict;
use warnings;
use XML::XPath;

my $root = XML::XPath->new(ioref => 'STDIN');
for my $node ($root->find($ARGV[0])->get_nodelist) {
  print($node->getData, "\n");
}

xpath from XML::XPath returns too much noise, -- NODE -- and attribute = "value".

xml_grep from XML::Twig cannot handle expressions that do not return elements, so cannot be used to extract attribute values without further processing.

EDIT:

echo cat //element/@attribute | xmllint --shell filename.xml returns noise similar to xpath.

xmllint --xpath //element/@attribute filename.xml returns attribute = "value".

xmllint --xpath 'string(//element/@attribute)' filename.xml returns what I want, but only for the first match.

For another solution almost satisfying the question, here is an XSLT that can be used to evaluate arbitrary XPath expressions (requires dyn:evaluate support in the XSLT processor):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">
  <xsl:output omit-xml-declaration="yes" indent="no" method="text"/>
  <xsl:template match="/">
    <xsl:for-each select="dyn:evaluate($pattern)">
      <xsl:value-of select="dyn:evaluate($value)"/>
      <xsl:value-of select="'
'"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Run with xsltproc --stringparam pattern //element/@attribute --stringparam value . arbitrary-xpath.xslt filename.xml.

Body:
A solution that works even when namespace declarations exist on top:

Most of the commands proposed in the answers do not work out of the box if the xml has a namespace declared on top. Consider this:

input xml:

<elem1 xmlns="urn:x" xmlns:prefix="urn:y">
    <elem2 attr1="false" attr2="value2">
        elem2 value
    </elem2>
    <elem2 attr1="true" attr2="value2.1">
        elem2.1 value
    </elem2>
    <prefix:elem3>
        elem3 value
    </prefix:elem3>
</elem1>

Does not work:

xmlstarlet sel -t -v "/elem1" input.xml
# nothing printed
xmllint -xpath "/elem1" input.xml
# XPath set is empty

Solution:

# Requires >=java11 to run like below (but the code requires >=java17 for case syntax to be recognized)

# Prints the whole document
java ExtractXpath.java "/" example-inputs/input.xml

# Prints the contents and self of "elem1"
java ExtractXpath.java "/elem1" input.xml

# Prints the contents and self of "elem2" whose attr2 value is: 'value2'
java ExtractXpath.java "//elem2[@attr2='value2']" input.xml

# Prints the value of the attribute 'attr2': "value2", "value2.1"
java ExtractXpath.java "/elem1/elem2/@attr2" input.xml

# Prints the text inside elem3: "elem3 value"
java ExtractXpath.java "/elem1/elem3/text()" input.xml

# Prints the name of the matched element: "prefix:elem3"
java ExtractXpath.java "name(/elem1/elem3)" input.xml
# Same as above: "prefix:elem3"
java ExtractXpath.java "name(*/elem3)" input.xml

# Prints the count of the matched elements: 2.0
java ExtractXpath.java "count(/elem2)" input.xml

# known issue: while "//elem2" works. "//elem3" does not (it works only with: '*/elem3' )

ExtractXpath.java:

import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathEvaluationResult;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ExtractXpath {

    public static void main(String[] args) throws Exception {
        assertThat(args.length==2, "Wrong number of args");
        String xpath = args[0];
        File file = new File(args[1]);

        assertThat(file.isFile(), file.getAbsolutePath()+" is not a file.");
        FileInputStream fileIS = new FileInputStream(file);
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document xmlDocument = builder.parse(fileIS);
        XPath xPath = XPathFactory.newInstance().newXPath();
        String expression = xpath;
        XPathExpression xpathExpression =  xPath.compile(expression);

        XPathEvaluationResult xpathEvalResult =  xpathExpression.evaluateExpression(xmlDocument);
        System.out.println(applyXpathExpression(xmlDocument, xpathExpression, xpathEvalResult.type().name()));
    }

    private static String applyXpathExpression(Document xmlDocument, XPathExpression expr, String xpathTypeName) throws TransformerConfigurationException, TransformerException, XPathExpressionException {

        // see: https://www.w3.org/TR/1999/REC-xpath-19991116/#corelib
        List<String> retVal = new ArrayList();
        if(xpathTypeName.equals(XPathConstants.NODESET.getLocalPart())){ //e.g. xpath: /elem1/*
            NodeList nodeList = (NodeList)expr.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                retVal.add(convertNodeToString(nodeList.item(i)));
            }
        }else if(xpathTypeName.equals(XPathConstants.STRING.getLocalPart())){ //e.g. xpath: name(/elem1/*)
            retVal.add((String)expr.evaluate(xmlDocument, XPathConstants.STRING));
        }else if(xpathTypeName.equals(XPathConstants.NUMBER.getLocalPart())){ //e.g. xpath: count(/elem1/*)
            retVal.add(((Number)expr.evaluate(xmlDocument, XPathConstants.NUMBER)).toString());
        }else if(xpathTypeName.equals(XPathConstants.BOOLEAN.getLocalPart())){ //e.g. xpath: contains(elem1, 'sth')
            retVal.add(((Boolean)expr.evaluate(xmlDocument, XPathConstants.BOOLEAN)).toString());
        }else if(xpathTypeName.equals(XPathConstants.NODE.getLocalPart())){ //e.g. xpath: fixme: find one
            System.err.println("WARNING found xpathTypeName=NODE");
            retVal.add(convertNodeToString((Node)expr.evaluate(xmlDocument, XPathConstants.NODE)));
        }else{
            throw new RuntimeException("Unexpected xpath type name: "+xpathTypeName+". This should normally not happen");
        }
        return retVal.stream().map(str->"==MATCH_START==\n"+str+"\n==MATCH_END==").collect(Collectors.joining ("\n"));

    }

    private static String convertNodeToString(Node node) throws TransformerConfigurationException, TransformerException {
            short nType = node.getNodeType();
        switch (nType) {
            case Node.ATTRIBUTE_NODE , Node.TEXT_NODE -> {
                return node.getNodeValue();
            }
            case Node.ELEMENT_NODE, Node.DOCUMENT_NODE -> {
                StringWriter writer = new StringWriter();
                Transformer trans = TransformerFactory.newInstance().newTransformer();
                trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                trans.setOutputProperty(OutputKeys.INDENT, "yes");
                trans.transform(new DOMSource(node), new StreamResult(writer));
                return writer.toString();
            }
            default -> {
                System.err.println("WARNING: FIXME: Node type:"+nType+" could possibly be handled in a better way.");
                return node.getNodeValue();
            }

        }
    }

    private static void assertThat(boolean b, String msg) {
        if(!b){
            System.err.println(msg+"\n\nUSAGE: program xpath xmlFile");
            System.exit(-1);
        }
    }
}

@SuppressWarnings("unchecked")
class NamespaceResolver implements NamespaceContext {
    //Store the source document to search the namespaces
    private final Document sourceDocument;
    public NamespaceResolver(Document document) {
        sourceDocument = document;
    }

    //The lookup for the namespace uris is delegated to the stored document.
    @Override
    public String getNamespaceURI(String prefix) {
        if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
            return sourceDocument.lookupNamespaceURI(null);
        } else {
            return sourceDocument.lookupNamespaceURI(prefix);
        }
    }

    @Override
    public String getPrefix(String namespaceURI) {
        return sourceDocument.lookupPrefix(namespaceURI);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Iterator getPrefixes(String namespaceURI) {
        return null;
    }
}

and for simplicity:

xpath-extract command:

#!/bin/bash
java ExtractXpath.java "$1" "$2"


问:
Rails 7 missing partial
I am upgrading to Rails 7.1 and get a weird error in a view using a JS partial:

Missing partial account/_stripe.js.erb, application/_stripe.js.erb
with {:locale=>[:fr], :formats=>[:html], :variants=>[],
:handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

Here is my code to render the partial (it works well with Rails 6):

```<code><%= render "stripe.js.erb" %>
</code>```

The partial is located in the same folder as the view and is named "_stripe.js.erb".

I tried to replace the name of the partial, as suggested, by account/stripe, account/stripe.js, account/stripe.js.erb, stripe, stripe.js, none works.

Any idea how to solve this with Rails 7? Thanks.

答:
Having an extension in the partial name was deprecated in Rails 6.1 and removed in Rails 7. You should use the following syntax:</p>
```<code><%= render partial: 'stripe', formats: :js %>
</code>```

Title: Rails 7 missing partial

Body:
I am upgrading to Rails 7.1 and get a weird error in a view using a JS partial:

Missing partial account/_stripe.js.erb, application/_stripe.js.erb
with {:locale=>[:fr], :formats=>[:html], :variants=>[],
:handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

Here is my code to render the partial (it works well with Rails 6):

<%= render "stripe.js.erb" %>

The partial is located in the same folder as the view and is named "_stripe.js.erb".

I tried to replace the name of the partial, as suggested, by account/stripe, account/stripe.js, account/stripe.js.erb, stripe, stripe.js, none works.

Any idea how to solve this with Rails 7? Thanks.

Body:
Having an extension in the partial name was deprecated in Rails 6.1 and removed in Rails 7. You should use the following syntax:

<%= render partial: 'stripe', formats: :js %>


问:
How can I filter specific emails from an array?
I'm trying to get from an array of multiple emails string this effect
if there is more than one email that ends with the same domain name of the email do not show me that filter for example:

const db = ['example@example.com', 'example2@example.com'];

const check = ['example3@example.com', 'another@anotherdomain.com'];

function findNotSimilar(check, db) {

```<code>const arr = check.filter((item) => db.indexOf(item) === -1);
return arr;
</code>```

}

this returns ['example3@example.com', 'another@anotherdomain.com'], but I would like to return just  ['another@anotherdomain.com'] array because the email ending with the same domain name

答:
Here is the solution. But regarding the domains.. you have to consider domains with 2 parts like '.co.uk' in comparison with simple '.com'.</p>
```<code>function Test() {
    const db = ['example@example.com', 'example2@example.com'];
    const check = ['example3@example.eu', 'another@anotherdomain.com'];
    const dbFiltered = db.map(x => x.split('@')[1].split('.').slice(0, -1).join('.')).filter((x, y, z) => z.indexOf(x) === y);
    const arr = check.filter((item) => dbFiltered.map(x => item.indexOf(x) === -1).every(x => x === true));
    return arr;
}
</code>```

Body:
I'm trying to get from an array of multiple emails string this effect
if there is more than one email that ends with the same domain name of the email do not show me that filter for example:

const db = ['example@example.com', 'example2@example.com'];

const check = ['example3@example.com', 'another@anotherdomain.com'];

function findNotSimilar(check, db) {

const arr = check.filter((item) => db.indexOf(item) === -1);
return arr;

}

this returns ['example3@example.com', 'another@anotherdomain.com'], but I would like to return just  ['another@anotherdomain.com'] array because the email ending with the same domain name

Body:
Here is the solution. But regarding the domains.. you have to consider domains with 2 parts like '.co.uk' in comparison with simple '.com'.

function Test() {
    const db = ['example@example.com', 'example2@example.com'];
    const check = ['example3@example.eu', 'another@anotherdomain.com'];
    const dbFiltered = db.map(x => x.split('@')[1].split('.').slice(0, -1).join('.')).filter((x, y, z) => z.indexOf(x) === y);
    const arr = check.filter((item) => dbFiltered.map(x => item.indexOf(x) === -1).every(x => x === true));
    return arr;
}


问:
docker not found inside jenkins pipeline
I have a jenkins installed on my linux machine and connected it to my kubernetes cluster.

I've installed docker and docker-pipeline plugin inside jenkins and they can be found under /var/lib/jenkins/plugins:

```<code>docker-build-step
docker-build-step.jpi
docker-commons
docker-commons.jpi
docker-java-api
docker-java-api.jpi
docker-plugin
docker-plugin.jpi
docker-workflow
docker-workflow.jpi
</code>```

and configured in Global Tool Configuration:

However when I run docker.build or even sh 'docker ps' inside my pipeline it throws:

```<code>/home/jenkins/agent/workspace/nodejs@tmp/durable-492ddc4c/script.sh: 1: docker: not found
</code>```

EDIT:

I've built my own image with docker and when I exec into it I can run docker command but pipeline still throws docker not found command and found out it is because jnlp doesn't have docker installed. Any ideas how to fix it inside jenkins-inbound-agent?

答:
I've fixed it by adding:

```<code>tools {
  <...>
  'org.jenkinsci.plugins.docker.commons.tools.DockerTool' 'docker'
}
</code>```

in environment part of my pipeline. And it had appeared in my $PATH while checking inside the pod.

Then I've had issues while invoking docker.withRegistry() and solved it by using docker.withTool() as following:

```<code>docker.withTool('docker'){
    docker.withRegistry('repo','credentials') {
        <rest of the pipeline here>
    }
}
</code>```

It might not be the prettiest solution but it worked for my homelab use-case.

Body:
I have a jenkins installed on my linux machine and connected it to my kubernetes cluster.

I've installed docker and docker-pipeline plugin inside jenkins and they can be found under /var/lib/jenkins/plugins:

docker-build-step
docker-build-step.jpi
docker-commons
docker-commons.jpi
docker-java-api
docker-java-api.jpi
docker-plugin
docker-plugin.jpi
docker-workflow
docker-workflow.jpi

and configured in Global Tool Configuration:

However when I run docker.build or even sh 'docker ps' inside my pipeline it throws:

/home/jenkins/agent/workspace/nodejs@tmp/durable-492ddc4c/script.sh: 1: docker: not found

EDIT:

I've built my own image with docker and when I exec into it I can run docker command but pipeline still throws docker not found command and found out it is because jnlp doesn't have docker installed. Any ideas how to fix it inside jenkins-inbound-agent?

Body:
I've fixed it by adding:

tools {
  <...>
  'org.jenkinsci.plugins.docker.commons.tools.DockerTool' 'docker'
}

in environment part of my pipeline. And it had appeared in my $PATH while checking inside the pod.

Then I've had issues while invoking docker.withRegistry() and solved it by using docker.withTool() as following:

docker.withTool('docker'){
    docker.withRegistry('repo','credentials') {
        <rest of the pipeline here>
    }
}

It might not be the prettiest solution but it worked for my homelab use-case.


问:
Moving a rectangle up down left and right
I wanted to make a rectangle move in 4 directions with a click of a button on JavaScript. Only the Right and Down works and the other two does not work. I tried finding it on internet and so far had not have any luck.

var currentXpos = 0;

function moveRectRight() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginLeft = currentXpos + 'px'; // re-draw rectangle
}

function moveRectLeft() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginRight = currentXpos + 'px'; // re-draw rectangle
}

function moveRectUp() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginBottom = currentXpos + 'px'; // re-draw rectangle
}

function moveRectDown() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginTop = currentXpos + 'px'; // re-draw rectangle
}```
<pre class="snippet-code-css lang-css prettyprint-override"><code>#rectangle {
  background-color: red;
  width: 200px;
  height: 100px;
  margin-left: 0px;
}</code>```

<input type="button" value="Right" onclick="moveRectRight()" />
<input type="button" value="Left" onclick="moveRectLeft()" />
<input type="button" value="Up" onclick="moveRectUp()" />
<input type="button" value="Down" onclick="moveRectDown()" />```

答:
Thremulant gave a very good solution but there was something missed something the "current position" variable should be different for X and Y axis. This way it will not show abnormal behaviour.</p>
```<code><html>
<head>
<style>
#rectangle {
  background-color: red;
  width: 200px;
  height: 100px;
  margin-left: 0px;
}
</style>
<script>
var currentXpos = 0;
var currentYpos = 0;

function moveRectRight() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginLeft = currentXpos + 'px'; // re-draw rectangle
}

function moveRectLeft() {
  var rect = document.getElementById('rectangle');
  currentXpos -= 100; // move by 100 px to the right
  rect.style.marginLeft = currentXpos + 'px'; // re-draw rectangle
}

function moveRectUp() {
  var rect = document.getElementById('rectangle');
  currentYpos -= 100; // move by 100 px to the right
  rect.style.marginTop = currentYpos + 'px'; // re-draw rectangle
}

function moveRectDown() {
  var rect = document.getElementById('rectangle');
  currentYpos += 100; // move by 100 px to the right
  rect.style.marginTop = currentYpos + 'px'; // re-draw rectangle
}
</script>
</head>
<body>
<div id='rectangle'></div>
<input type="button" value="Right" onclick="moveRectRight()" />
<input type="button" value="Left" onclick="moveRectLeft()" />
<input type="button" value="Up" onclick="moveRectUp()" />
<input type="button" value="Down" onclick="moveRectDown()" />
</body>
</html>
</code>```

Body:
I wanted to make a rectangle move in 4 directions with a click of a button on JavaScript. Only the Right and Down works and the other two does not work. I tried finding it on internet and so far had not have any luck.

var currentXpos = 0;

function moveRectRight() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginLeft = currentXpos + 'px'; // re-draw rectangle
}

function moveRectLeft() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginRight = currentXpos + 'px'; // re-draw rectangle
}

function moveRectUp() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginBottom = currentXpos + 'px'; // re-draw rectangle
}

function moveRectDown() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginTop = currentXpos + 'px'; // re-draw rectangle
}

#rectangle {
  background-color: red;
  width: 200px;
  height: 100px;
  margin-left: 0px;
}

<input type="button" value="Right" onclick="moveRectRight()" />
<input type="button" value="Left" onclick="moveRectLeft()" />
<input type="button" value="Up" onclick="moveRectUp()" />
<input type="button" value="Down" onclick="moveRectDown()" />

Body:
Thremulant gave a very good solution but there was something missed something the "current position" variable should be different for X and Y axis. This way it will not show abnormal behaviour.

<html>
<head>
<style>
#rectangle {
  background-color: red;
  width: 200px;
  height: 100px;
  margin-left: 0px;
}
</style>
<script>
var currentXpos = 0;
var currentYpos = 0;

function moveRectRight() {
  var rect = document.getElementById('rectangle');
  currentXpos += 100; // move by 100 px to the right
  rect.style.marginLeft = currentXpos + 'px'; // re-draw rectangle
}

function moveRectLeft() {
  var rect = document.getElementById('rectangle');
  currentXpos -= 100; // move by 100 px to the right
  rect.style.marginLeft = currentXpos + 'px'; // re-draw rectangle
}

function moveRectUp() {
  var rect = document.getElementById('rectangle');
  currentYpos -= 100; // move by 100 px to the right
  rect.style.marginTop = currentYpos + 'px'; // re-draw rectangle
}

function moveRectDown() {
  var rect = document.getElementById('rectangle');
  currentYpos += 100; // move by 100 px to the right
  rect.style.marginTop = currentYpos + 'px'; // re-draw rectangle
}
</script>
</head>
<body>
<div id='rectangle'></div>
<input type="button" value="Right" onclick="moveRectRight()" />
<input type="button" value="Left" onclick="moveRectLeft()" />
<input type="button" value="Up" onclick="moveRectUp()" />
<input type="button" value="Down" onclick="moveRectDown()" />
</body>
</html>


问:
Partaly "free_y" Facet Wrap with ggplot
my goal is to produce a column graph showing different element concentrations.
there is a very wide range so I want to customise the scale of my faceted graph into 3 groups.
that way the graphs are able to show the variation in samples for each element and still be comparable between elements,

so idealy I would have 3 different scales for Groups 1,2,and 3 in the graph below.

this is the code to make the above graph

```<code>ggplot(binded)+
  aes(y=mean,
      x=sample,
      group=id)+
  geom_col(aes(fill=element))+
  geom_errorbar(aes(ymin = mean - sd,
                    ymax = mean + sd))+
  facet_wrap(rang~element)+
  scale_x_continuous(breaks = seq(1,15,by=1),
                     name = "Sample ID")+
  scale_y_continuous(name="Elemental Conc. (mg/kg)",labels = comma)+
  theme(legend.position = "none")
</code>```

and the data used is below

if i swich the facting to  facet_wrap(rang~element,scales = "free_y") then i get

is there any way to mage the scales only free within each group of rang?

i suspect im going to have to just create 3 seperat graphs.

答:
Thanks to Danlooo for the suggestion of patchwork that package and creating 3 separate graphs + plus another one for the y axis label proved successful.

I produced several graphs with the original code and a data frame filters for different concentrations. and the following patchwork code to produce the following graph

```<code> p5<-(p1 | p2) / p3+ plot_layout(heights=c(1,2))
(p4+p5)+plot_layout(widths = c(1, 25))
</code>```

Title: Partaly "free_y" Facet Wrap with ggplot

Body:
my goal is to produce a column graph showing different element concentrations.
there is a very wide range so I want to customise the scale of my faceted graph into 3 groups.
that way the graphs are able to show the variation in samples for each element and still be comparable between elements,

so idealy I would have 3 different scales for Groups 1,2,and 3 in the graph below.

this is the code to make the above graph

ggplot(binded)+
  aes(y=mean,
      x=sample,
      group=id)+
  geom_col(aes(fill=element))+
  geom_errorbar(aes(ymin = mean - sd,
                    ymax = mean + sd))+
  facet_wrap(rang~element)+
  scale_x_continuous(breaks = seq(1,15,by=1),
                     name = "Sample ID")+
  scale_y_continuous(name="Elemental Conc. (mg/kg)",labels = comma)+
  theme(legend.position = "none")

and the data used is below

if i swich the facting to  facet_wrap(rang~element,scales = "free_y") then i get

is there any way to mage the scales only free within each group of rang?

i suspect im going to have to just create 3 seperat graphs.

Body:
Thanks to Danlooo for the suggestion of patchwork that package and creating 3 separate graphs + plus another one for the y axis label proved successful.

I produced several graphs with the original code and a data frame filters for different concentrations. and the following patchwork code to produce the following graph

 p5<-(p1 | p2) / p3+ plot_layout(heights=c(1,2))
(p4+p5)+plot_layout(widths = c(1, 25))


问:
Where does the downloaded file go when execute robot framework using Jenkins
I need one help,
I am Executing a script of robot framework using Jenkins, as per the flow the script should download one XLS and validate the data using Pandas.

When I am executing the script using Jenkins and searching the downloaded file in a hardcoded path i.e. "C:\Users\Administrator\Downloads" the file is not found.
but when executing it manually the file downloaded on the same path

Can anyone please help me by telling me where do Jenkins stores all the downloaded files?

PS : I am using Google Chrome as web browser

答:
For each job Jenkins runs, it creates a workspace directory, usually at:
$JENKINS_HOME/workspace/(Name of the Job)

The scripts (and robotframework's in this case) are run there, as the root directory. Most probably the downloaded file will be in a path from there (or an error should have occurred because the Jenkins user does not have access to the C:\Users... path).

There is a step in the Jenkins job, which is to archive (download) files from workspace, to Jenkins database, to be shown as "artifacts". Even the screenshots images or videos recording, must be registered in that step, so they can be viewed on the Report and Log in Jenkins (Robot Framework Plugin).


问:
Asp.net Core CORS works with GET but not wit POST
I am using Angular 4 app with Asp core web api which I test on the locahost with different ports. My WebApi requires Windows Authentication(need to get logon user name). All calls using GET work, but unfortunately, I cannot get POST to work. I have WebApi setup for  CORS:

```<code>        public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);
        services.AddDbContext<CatalogContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAll", builder =>
            {
                builder.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
            });

        });
        services.AddMvc();
    }
</code>```

And angular 4 client is trying to post file

```<code>fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
  let file: File = fileList[0];
  let formData: FormData = new FormData();
  formData.append('uploadFile', file, file.name);
  let headers = new Headers();
  /** No need to include Content-Type in Angular 4 */
  headers.append('Content-Type', 'multipart/form-data');
  headers.append('Accept', 'application/json');

  let options = new RequestOptions({ headers: headers, withCredentials: true });
  this.http.post("http://localhost:495/api//upload",formData, options)
    .map(res => res.json())
    .catch(error => Observable.throw(error))
    .subscribe(
    data => console.log('success'),
    error => console.log(error)
    )
}
</code>```

My error:

XMLHttpRequest cannot load http://localhost:495/api//upload. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.

And Response headers

Content-Length:0
Date:Sun, 13 Aug 2017 13:13:09 GMT
Persistent-Auth:true
Server:Kestrel
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcU291cmNlIENvZGVcUGFydG5lcnNoaXBDYXRhbG9nXFBhcnRuZXJzaGlwQ2F0YWxvZ1xQYXJ0bmVyc2hpcENhdGFsb2cuV2ViQXBpXGFwaVx1cGxvYWQ=?=

Any help would be appreciated!

答:
In my case I had to slighlty modified POST/Put method

From

```<code>public IHttpActionResult PostTest(Test objTest)
</code>```

To

```<code>public IHttpActionResult PostTest([FromBody]Test objTest)
</code>```

Body:
I am using Angular 4 app with Asp core web api which I test on the locahost with different ports. My WebApi requires Windows Authentication(need to get logon user name). All calls using GET work, but unfortunately, I cannot get POST to work. I have WebApi setup for  CORS:

        public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);
        services.AddDbContext<CatalogContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAll", builder =>
            {
                builder.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
            });

        });
        services.AddMvc();
    }

And angular 4 client is trying to post file

fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
  let file: File = fileList[0];
  let formData: FormData = new FormData();
  formData.append('uploadFile', file, file.name);
  let headers = new Headers();
  /** No need to include Content-Type in Angular 4 */
  headers.append('Content-Type', 'multipart/form-data');
  headers.append('Accept', 'application/json');

  let options = new RequestOptions({ headers: headers, withCredentials: true });
  this.http.post("http://localhost:495/api//upload",formData, options)
    .map(res => res.json())
    .catch(error => Observable.throw(error))
    .subscribe(
    data => console.log('success'),
    error => console.log(error)
    )
}

My error:

XMLHttpRequest cannot load http://localhost:495/api//upload. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.

And Response headers

Content-Length:0
Date:Sun, 13 Aug 2017 13:13:09 GMT
Persistent-Auth:true
Server:Kestrel
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcU291cmNlIENvZGVcUGFydG5lcnNoaXBDYXRhbG9nXFBhcnRuZXJzaGlwQ2F0YWxvZ1xQYXJ0bmVyc2hpcENhdGFsb2cuV2ViQXBpXGFwaVx1cGxvYWQ=?=

Any help would be appreciated!

Body:
In my case I had to slighlty modified POST/Put method

From

public IHttpActionResult PostTest(Test objTest)

To

public IHttpActionResult PostTest([FromBody]Test objTest)


问:
mysql error SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)
I am relatively new to php and have been following a tutorial for a beginner project. This project includes a database using the DBO object with mysql. I am using MAMP to run php. When I try to run my project it gives me the error in the title. Here is my code for database setup:

```<code><?php
  $dsn = 'mysql:host=localhost;dbname=assignment_tracker';
  $username = 'root';

  try {
    $db = new PDO($dsn, $username);
  } catch (PDOException $e) {
    $error = "Database Error: ";
    $error .= $e->getMessage();
    include('view/error.php');
    exit();
  }
</code>```

I am able to enter the phpmyadmin page. Looking at the user table in the mysql database shows this:

My port in the php.ini file is 8889. (My apache port is 8888 but my mysql is 8889 not sure which one it should be set to.) Looking at other answers on this website have been really confusing for me so any help would be greatly appreciated.

答:
Try adding mysql's port to the $dns and add the password (if it's not null) as part of the connection. Try this

```<code><?php

$dsn      = 'mysql:host=127.0.0.1;port=8889;dbname=assignment_tracker';
$username = 'root';
$password = '';

try{
    $db = new PDO($dsn, $username, $password);
}catch (PDOException $e){
    $error = "Database Error: ";
    $error .= $e->getMessage();
    include('view/error.php');
    exit();
}
</code>```

Body:
I am relatively new to php and have been following a tutorial for a beginner project. This project includes a database using the DBO object with mysql. I am using MAMP to run php. When I try to run my project it gives me the error in the title. Here is my code for database setup:

<?php
  $dsn = 'mysql:host=localhost;dbname=assignment_tracker';
  $username = 'root';

  try {
    $db = new PDO($dsn, $username);
  } catch (PDOException $e) {
    $error = "Database Error: ";
    $error .= $e->getMessage();
    include('view/error.php');
    exit();
  }

I am able to enter the phpmyadmin page. Looking at the user table in the mysql database shows this:

My port in the php.ini file is 8889. (My apache port is 8888 but my mysql is 8889 not sure which one it should be set to.) Looking at other answers on this website have been really confusing for me so any help would be greatly appreciated.

Body:
Try adding mysql's port to the $dns and add the password (if it's not null) as part of the connection. Try this

<?php

$dsn      = 'mysql:host=127.0.0.1;port=8889;dbname=assignment_tracker';
$username = 'root';
$password = '';

try{
    $db = new PDO($dsn, $username, $password);
}catch (PDOException $e){
    $error = "Database Error: ";
    $error .= $e->getMessage();
    include('view/error.php');
    exit();
}


问:
Generic virtual method and concrete Implementation?
I'm currently working on a piece of code, where Generics are driving me crazy. Maybe somebody can point out the issue.

All our DatabaseEntities have common Interfaces, one for example is IDatabaseEntityWithId.

let's say there is EntityA : IDatabaseEntityWithId. We have different, non-generic Forms / Controls. We also have Controls that should be shared, and do their Job independent of the concrete Type.

Now, coming from Java this is quite easy there, cause every control could just use for example List<?> and pass / Process the list without actually caring what is inside.

So, our Search-Control should also be able to "deal" with List<T> and IQueryable<T> without caring what T actually is.

The base implementation for an EntityList has the following methods, which our Search should utilize to generate the results, do pagination, etc.

```<code>public class EntityListControl{
  public virtual IQueryable<T> BaseQuery<T>(DBContext dbContext) where T : IDBEntityWithId => throw new NotImplementedException();
  public virtual List<string> AutocompleteSuggestionsQuickSearch() => throw new NotImplementedException();
  public virtual List<IDBEntityWithId> DoQuickSearch(string reference) => throw new NotImplementedException();
  public virtual IQueryable<T> DoTaggedSearchExtensions<T>(IQueryable<T> query, DbContext dbContext) where T : IDBEntityWithId  => throw new NotImplementedException();
}
</code>```

The SearchControl has a reference to the currently loaded EntityListControl and basically should just act as orchestrator for the concrete methods, i.e. shortened it looks like:

```<code>class SearchControl{
   public void DoSearch(){
      String[] searchTags = this.SearchField.Text.Split(' ');
      using (DbContext db = DbContext.Factory()){
          var query = this.EntityListControl.BaseQuery<IDBEntityWithId>(db);

          //do other stuff.

          query = this.EntityListControl.DoTaggedSearchExtension<IDBEntityWithID>(query, db);

         //do other stuff

          List<IDBEntityWithId> result = query.Take(X).ToList();

         //do other stuff

          this.EntityListView.FactoryRows(result);
      }
</code>```

You'll get the idea.

Now, for one of the EntityListControl-implementations, these methods might look like:

```<code>public class EntityList: UserControl{
    ...
}
...

public class EntityListA : EntityList{

   public override IQueryable<EntityA> BaseQuery<EntityA>(DbContext db){
       return db.EntityA.Include("EntityB").Include("EntityC");
   }

   public IQueryable<EntityA> DoTaggedSearchExtension<EntityA>(IQueryable<EntityA> query, DbContext db){
      //Problem is now here...
      return query.Where(x => x.PropertyOfEntityA == true);
   }
}
</code>```

The Problem is now in the concrete implementation of DoTaggedSearchExtension. Visual Studio here clearly lines out, that T is EntityA and the query is IQueryable<EntityA> - but refuses to accept any properties that don't belong to IDBEntityWithId. Why is that?

The error Message even reads "EntityA does not contain a definition for 'property'...." - which is wrong, it is there... IntelliSense just proposes properties of the common interface.

Obviously in the generic EntityListControl, I can not be more concrete on the type of T, IBaseEntityWithId is one of the common interfaces I can ensure for all entities.

Ofc, I could leave away the whole generic-part and JUST work with interfaces, casting them to the actual type in the implementations, but that's not the point, isn't it?

Any suggestion on how to fix this particular example?

答:
You want the base class to be generic, not the methods!

```<code>public class EntityList<T> where T : IDBEntityWithId
{
  public virtual IQueryable<T> BaseQuery(DBContext dbContext) => throw new NotImplementedException();
  public virtual List<string> AutocompleteSuggestionsQuickSearch() => throw new NotImplementedException();
  public virtual List<IDBEntityWithId> DoQuickSearch(string reference) => throw new NotImplementedException();
  public virtual IQueryable<T> DoTaggedSearchExtensions(IQueryable<T> query, DbContext dbContext) => throw new NotImplementedException();
}
</code>```

When you then implement this, you get the concrete types you expect

```<code>public class EntityListA : EntityList<EntityA>{

   public override IQueryable<EntityA> BaseQuery(DbContext db){
       return db.EntityA.Include("EntityB").Include("EntityC");
   }

   public IQueryable<EntityA> DoTaggedSearchExtension(IQueryable<EntityA> query, DbContext db){
      //Problem is now here... (Not any more it isnt!)
      return query.Where(x => x.PropertyOfEntityA == true);
   }
}
</code>```

Live example for demo: https://dotnetfiddle.net/SiICNV (Compiles fine but had to comment out the EF-specific stuff)

Body:
I'm currently working on a piece of code, where Generics are driving me crazy. Maybe somebody can point out the issue.

All our DatabaseEntities have common Interfaces, one for example is IDatabaseEntityWithId.

let's say there is EntityA : IDatabaseEntityWithId. We have different, non-generic Forms / Controls. We also have Controls that should be shared, and do their Job independent of the concrete Type.

Now, coming from Java this is quite easy there, cause every control could just use for example List<?> and pass / Process the list without actually caring what is inside.

So, our Search-Control should also be able to "deal" with List<T> and IQueryable<T> without caring what T actually is.

The base implementation for an EntityList has the following methods, which our Search should utilize to generate the results, do pagination, etc.

public class EntityListControl{
  public virtual IQueryable<T> BaseQuery<T>(DBContext dbContext) where T : IDBEntityWithId => throw new NotImplementedException();
  public virtual List<string> AutocompleteSuggestionsQuickSearch() => throw new NotImplementedException();
  public virtual List<IDBEntityWithId> DoQuickSearch(string reference) => throw new NotImplementedException();
  public virtual IQueryable<T> DoTaggedSearchExtensions<T>(IQueryable<T> query, DbContext dbContext) where T : IDBEntityWithId  => throw new NotImplementedException();
}

The SearchControl has a reference to the currently loaded EntityListControl and basically should just act as orchestrator for the concrete methods, i.e. shortened it looks like:

class SearchControl{
   public void DoSearch(){
      String[] searchTags = this.SearchField.Text.Split(' ');
      using (DbContext db = DbContext.Factory()){
          var query = this.EntityListControl.BaseQuery<IDBEntityWithId>(db);

          //do other stuff.

          query = this.EntityListControl.DoTaggedSearchExtension<IDBEntityWithID>(query, db);

         //do other stuff

          List<IDBEntityWithId> result = query.Take(X).ToList();

         //do other stuff

          this.EntityListView.FactoryRows(result);
      }

You'll get the idea.

Now, for one of the EntityListControl-implementations, these methods might look like:

public class EntityList: UserControl{
    ...
}
...

public class EntityListA : EntityList{

   public override IQueryable<EntityA> BaseQuery<EntityA>(DbContext db){
       return db.EntityA.Include("EntityB").Include("EntityC");
   }

   public IQueryable<EntityA> DoTaggedSearchExtension<EntityA>(IQueryable<EntityA> query, DbContext db){
      //Problem is now here...
      return query.Where(x => x.PropertyOfEntityA == true);
   }
}

The Problem is now in the concrete implementation of DoTaggedSearchExtension. Visual Studio here clearly lines out, that T is EntityA and the query is IQueryable<EntityA> - but refuses to accept any properties that don't belong to IDBEntityWithId. Why is that?

The error Message even reads "EntityA does not contain a definition for 'property'...." - which is wrong, it is there... IntelliSense just proposes properties of the common interface.

Obviously in the generic EntityListControl, I can not be more concrete on the type of T, IBaseEntityWithId is one of the common interfaces I can ensure for all entities.

Ofc, I could leave away the whole generic-part and JUST work with interfaces, casting them to the actual type in the implementations, but that's not the point, isn't it?

Any suggestion on how to fix this particular example?

Body:
You want the base class to be generic, not the methods!

public class EntityList<T> where T : IDBEntityWithId
{
  public virtual IQueryable<T> BaseQuery(DBContext dbContext) => throw new NotImplementedException();
  public virtual List<string> AutocompleteSuggestionsQuickSearch() => throw new NotImplementedException();
  public virtual List<IDBEntityWithId> DoQuickSearch(string reference) => throw new NotImplementedException();
  public virtual IQueryable<T> DoTaggedSearchExtensions(IQueryable<T> query, DbContext dbContext) => throw new NotImplementedException();
}

When you then implement this, you get the concrete types you expect

public class EntityListA : EntityList<EntityA>{

   public override IQueryable<EntityA> BaseQuery(DbContext db){
       return db.EntityA.Include("EntityB").Include("EntityC");
   }

   public IQueryable<EntityA> DoTaggedSearchExtension(IQueryable<EntityA> query, DbContext db){
      //Problem is now here... (Not any more it isnt!)
      return query.Where(x => x.PropertyOfEntityA == true);
   }
}

Live example for demo: https://dotnetfiddle.net/SiICNV (Compiles fine but had to comment out the EF-specific stuff)


问:
ssis export from excel
I'm using ssis to export from Excel to text. Everything exports fine except one column has data in this format 0X906111 but is null when exporting.  What am I doing wrong?

答:
Is it coming into your data flow as null, or being lost on the output?  Try adding a data viewer (from the data flow line) to view the input.

If the data is coming in as null, try changing the Excel column type.

If the data is coming in okay, but not being exported correctly, use the data conversion control in ssis to set the data type accordingly.

Title: ssis export from excel


问:
Terraform cycle error using count with AWS API Gateway
I am getting Cycle errors when adding dependencies on the previous count index

I want to define an API path /test1/{id} using AWS Terraform with the id resource depending on the test1. If the resource is test1, the set the parent as the root API gateway resource.

```<code>locals {
  resources = ["test1", "{id}"]
}

resource "aws_api_gateway_rest_api" "root_api" {
  name = "dev-api"
}

resource "aws_api_gateway_resource" "dev_gateway_test_resources" {
  count       = length(local.resources)
  path_part   = local.resources[count.index]
  parent_id   = count.index == 0 ? aws_api_gateway_rest_api.root_api.root_resource_id : aws_api_gateway_resource.dev_gateway_test_resources[count.index - 1].id
  rest_api_id = aws_api_gateway_rest_api.root_api.id
}
</code>```

The response:

```<code>Error: Cycle: aws_api_gateway_resource.dev_gateway_test_resources[1], aws_api_gateway_resource.dev_gateway_test_resources[0]
</code>```

I have tried the same logic using for_each however I still see the same error

答:
What you have there is a resource which is referencing itself. As of today, this is not supported in Terraform, even if it can be easily seen that the resource does not actually has a reference to itself, but to another resource of the same type created in a loop. There is an issue for supporting self referencing: GitHub

Now, I don't know how many item can have your resources list, but in this case you would probably want to duplicate your aws_api_gateway_resource:

```<code>resource "aws_api_gateway_resource" "dev_gateway_test_root" {
  path_part   = "test"
  parent_id   = aws_api_gateway_rest_api.root_api.root_resource_id
  rest_api_id = aws_api_gateway_rest_api.root_api.id
}

resource "aws_api_gateway_resource" "dev_gateway_test_resources" {
  path_part   = "{id}"
  parent_id   = aws_api_gateway_resource.dev_gateway_test_root.id
  rest_api_id = aws_api_gateway_rest_api.root_api.id
}
</code>```

What you want to achieve is to have an endpoint such as /test/{id}. From my experience API gateway endpoints tend to grow in width, meaning that you will have something like:

```<code>/
 /test1
    /{id}
 /test2
    /{id}
 /test3
 ...
 /testN
</code>```

Instead of having a really lengthy endpoint, like /test/child1/child2/.../{id}.

You can have a loop for each level in the tree, they wont have self-referencing. You can not really have a loop between different levels of the tree.

Body:
I am getting Cycle errors when adding dependencies on the previous count index

I want to define an API path /test1/{id} using AWS Terraform with the id resource depending on the test1. If the resource is test1, the set the parent as the root API gateway resource.

locals {
  resources = ["test1", "{id}"]
}

resource "aws_api_gateway_rest_api" "root_api" {
  name = "dev-api"
}

resource "aws_api_gateway_resource" "dev_gateway_test_resources" {
  count       = length(local.resources)
  path_part   = local.resources[count.index]
  parent_id   = count.index == 0 ? aws_api_gateway_rest_api.root_api.root_resource_id : aws_api_gateway_resource.dev_gateway_test_resources[count.index - 1].id
  rest_api_id = aws_api_gateway_rest_api.root_api.id
}

The response:

Error: Cycle: aws_api_gateway_resource.dev_gateway_test_resources[1], aws_api_gateway_resource.dev_gateway_test_resources[0]

I have tried the same logic using for_each however I still see the same error

Body:
What you have there is a resource which is referencing itself. As of today, this is not supported in Terraform, even if it can be easily seen that the resource does not actually has a reference to itself, but to another resource of the same type created in a loop. There is an issue for supporting self referencing: GitHub

Now, I don't know how many item can have your resources list, but in this case you would probably want to duplicate your aws_api_gateway_resource:

resource "aws_api_gateway_resource" "dev_gateway_test_root" {
  path_part   = "test"
  parent_id   = aws_api_gateway_rest_api.root_api.root_resource_id
  rest_api_id = aws_api_gateway_rest_api.root_api.id
}

resource "aws_api_gateway_resource" "dev_gateway_test_resources" {
  path_part   = "{id}"
  parent_id   = aws_api_gateway_resource.dev_gateway_test_root.id
  rest_api_id = aws_api_gateway_rest_api.root_api.id
}

What you want to achieve is to have an endpoint such as /test/{id}. From my experience API gateway endpoints tend to grow in width, meaning that you will have something like:

/
 /test1
    /{id}
 /test2
    /{id}
 /test3
 ...
 /testN

Instead of having a really lengthy endpoint, like /test/child1/child2/.../{id}.

You can have a loop for each level in the tree, they wont have self-referencing. You can not really have a loop between different levels of the tree.


问:
Sphinx autodoc fails to import module
I'm trying to document a project using Sphinx, and am running into an issue where only some modules are being imported from a folder. My project structure looks like this:

```<code>Project
|
|--Main
|    |--Scripts
|          __init__.py
|          libsmop.py
|          conv_table.py
|          f_discrim.py
|          recipes.py
|          ...
</code>```

When I try to run make html, libsmop and recipes are imported without any issue, however conv_table and f_discrim get the following error:

WARNING: autodoc: failed to import module u'conv_table' from module u'Scripts'; the following exception was raised:No module named conv_table

I don't think it's my config file because it's finding all of the files when I run sphinx-apidoc -o _rst Main/Scripts and I've confirmed that they appear in the resulting Scripts.rst file.

Why is autodoc finding some modules but not others?

Edit:
conv_table.py is of this form:

```<code>import re
import numpy as np

"""
conv_table dictionary at the bottom of this file maps from matlab functions
to their python equivalents.
"""

def get_args(line,separator=",", open_char='(', close_char=')'):
    """Returns the arguments of line

    >>> get_args('ones(3,1,length(arr))')

...
< a bunch of function definitions>
...

conv_table = {... < a very big dictionary > ...}
</code>```

答:
I had a similar issue like yours, the fix was to append the path that holds that module inside the ../source/conf.py file using

sys.path.insert(0, os.path.abspath('whatever relative path works for your folder structure'))
sys.path.append('/path/to/the/conv_table/')

Title: Sphinx autodoc fails to import module

Body:
I'm trying to document a project using Sphinx, and am running into an issue where only some modules are being imported from a folder. My project structure looks like this:

Project
|
|--Main
|    |--Scripts
|          __init__.py
|          libsmop.py
|          conv_table.py
|          f_discrim.py
|          recipes.py
|          ...

When I try to run make html, libsmop and recipes are imported without any issue, however conv_table and f_discrim get the following error:

WARNING: autodoc: failed to import module u'conv_table' from module u'Scripts'; the following exception was raised:No module named conv_table

I don't think it's my config file because it's finding all of the files when I run sphinx-apidoc -o _rst Main/Scripts and I've confirmed that they appear in the resulting Scripts.rst file.

Why is autodoc finding some modules but not others?

Edit:
conv_table.py is of this form:

import re
import numpy as np

"""
conv_table dictionary at the bottom of this file maps from matlab functions
to their python equivalents.
"""

def get_args(line,separator=",", open_char='(', close_char=')'):
    """Returns the arguments of line

    >>> get_args('ones(3,1,length(arr))')

...

...

conv_table = {...  ...}


问:
sprintf like function in python
I have code in R and I want to convert that in python, I would like to know the function like sprintf. I have this query,

```<code>query1 = sprintf('''select * from table''',, toString(ids), df1, df2)
</code>```

答:
Python has a % operator for this.

```<code>string = "A = %d\nB = %s\n" % (100, 1000)
print(string)
#output
A = 100
B = 1000
</code>```

or fstring

```<code>string = f"A = {100}\nB = {1000}\n"
print(string)
</code>```

or format

```<code>string = "A = {}\nB = {}\n".format(100,1000)
print(string)
</code>```

Title: sprintf like function in python

Body:
I have code in R and I want to convert that in python, I would like to know the function like sprintf. I have this query,

query1 = sprintf('''select * from table''',, toString(ids), df1, df2)

Body:
Python has a % operator for this.

string = "A = %d\nB = %s\n" % (100, 1000)
print(string)
#output
A = 100
B = 1000

or fstring

string = f"A = {100}\nB = {1000}\n"
print(string)

or format

string = "A = {}\nB = {}\n".format(100,1000)
print(string)


问:
Kentico 11 Accessing Session Variables on Session End
we are trying to access the session variables on session end.  We have created a new module called ApplicationSessionEventsHandlerModule and added a SessionEnd_Execute handler.

However, the Kentico SessionHelper class does not work and neither does System.Web.HttpContext.Current.Session.  How do you recommend we access session variables on session end?  See code:

```<code>  public class ApplicationSessionEventsHandlerModule : Module
    {
    public ApplicationSessionEventsHandlerModule() :
        base("ApplicationSessionEventsHandlerModule")
        {
        }

    protected override void OnInit()
        {
        base.OnInit();
        ApplicationEvents.SessionStart.Execute += SessionStart_Execute;
        ApplicationEvents.SessionEnd.Execute += SessionEnd_Execute;
        }

    private void SessionEnd_Execute(object sender, EventArgs e)
        {

        if (SessionHelper.SessionIsAvailable)
            {
            if (SessionHelper.GetValue("UserDetails") != null)
                {
               //Do stuff
                    }
                }

            }

    private void SessionStart_Execute(object sender, EventArgs e)
        {

        if (SessionHelper.GetValue("UserDetails") != null)
            {
          //do stuff
            }
        }
    }
</code>```

答:
Reference link - https://docs.xperience.io/k11/custom-development/handling-global-events/reference-global-system-events#ReferenceGlobalsystemevents-ApplicationEvents

Please try the following -

Create a Global folder under \App_Code\

Create a ApplicationSessionEventsHandler.cs file under Global folder with the following code snippet and try -

```<code> using CMS.Base;
 using CMS.DocumentEngine;
 using CMS.Helpers;
 using CMS.Membership;

 [ApplicationSessionEventsHandler]
 public partial class CMSModuleLoader
 {
 public class ApplicationSessionEventsHandler : CMSLoaderAttribute
 {
 public override void Init()
 {
 ApplicationEvents.SessionEnd.Execute += SessionEnd_Execute;
 ApplicationEvents.SessionStart.Execute += SessionStart_Execute;

 }

 private void SessionStart_Execute(object sender, System.EventArgs e)
 {
 if (SessionHelper.GetValue("UserDetails") != null)
 {
 //do stuff
 }
 }
 private void SessionEnd_Execute(object sender, System.EventArgs e)
 {
 if (SessionHelper.SessionIsAvailable)
 {
 if (SessionHelper.GetValue("UserDetails") != null)
 {
 //Do stuff
 }
 }
 }
 }
 }
</code>```

Body:
we are trying to access the session variables on session end.  We have created a new module called ApplicationSessionEventsHandlerModule and added a SessionEnd_Execute handler.

However, the Kentico SessionHelper class does not work and neither does System.Web.HttpContext.Current.Session.  How do you recommend we access session variables on session end?  See code:

  public class ApplicationSessionEventsHandlerModule : Module
    {
    public ApplicationSessionEventsHandlerModule() :
        base("ApplicationSessionEventsHandlerModule")
        {
        }

    protected override void OnInit()
        {
        base.OnInit();
        ApplicationEvents.SessionStart.Execute += SessionStart_Execute;
        ApplicationEvents.SessionEnd.Execute += SessionEnd_Execute;
        }

    private void SessionEnd_Execute(object sender, EventArgs e)
        {

        if (SessionHelper.SessionIsAvailable)
            {
            if (SessionHelper.GetValue("UserDetails") != null)
                {
               //Do stuff
                    }
                }

            }

    private void SessionStart_Execute(object sender, EventArgs e)
        {

        if (SessionHelper.GetValue("UserDetails") != null)
            {
          //do stuff
            }
        }
    }

Body:
Reference link - https://docs.xperience.io/k11/custom-development/handling-global-events/reference-global-system-events#ReferenceGlobalsystemevents-ApplicationEvents

Please try the following -

Create a Global folder under \App_Code\

Create a ApplicationSessionEventsHandler.cs file under Global folder with the following code snippet and try -

 using CMS.Base;
 using CMS.DocumentEngine;
 using CMS.Helpers;
 using CMS.Membership;

 [ApplicationSessionEventsHandler]
 public partial class CMSModuleLoader
 {
 public class ApplicationSessionEventsHandler : CMSLoaderAttribute
 {
 public override void Init()
 {
 ApplicationEvents.SessionEnd.Execute += SessionEnd_Execute;
 ApplicationEvents.SessionStart.Execute += SessionStart_Execute;

 }

 private void SessionStart_Execute(object sender, System.EventArgs e)
 {
 if (SessionHelper.GetValue("UserDetails") != null)
 {
 //do stuff
 }
 }
 private void SessionEnd_Execute(object sender, System.EventArgs e)
 {
 if (SessionHelper.SessionIsAvailable)
 {
 if (SessionHelper.GetValue("UserDetails") != null)
 {
 //Do stuff
 }
 }
 }
 }
 }


问:
How to do tuple reduction in thrust (also return a tuple)?
Say I have a tuple consisting of two thrust::device_vector. I want the output to be a tuple consisting of two scalars, which are the sum of two vectors respectively. For example,

```<code>input tuple consisting of two vectors:
a: 3, 5, 2
b: 6, 1, 7
output tuple consisting of two scalars:
10
14
</code>```

I think this should be extremely easy but somehow I still haven't figure out a way to do so.
My code

```<code>#include <iostream>
#include <stdlib.h>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/transform.h>
#include <thrust/tuple.h>
#include <thrust/transform_reduce.h>
#include <thrust/iterator/zip_iterator.h>

template<typename T>
struct TestTuplePlus
{
    __host__ __device__
    thrust::tuple<T, T> operator()(thrust::tuple<T, T>& t0, thrust::tuple<T, T>& t1)
    {
            return thrust::make_tuple(thrust::get<0>(t0) + thrust::get<0>(t1), thrust::get<1>(t0) + thrust::get<1>(t1));
    }
};

int main()
{
  thrust::device_vector<float> a(3, 0);
  thrust::device_vector<float> b(3, 0);

  a[0] = 3;
  a[1] = 5;
  a[2] = 2;
  b[0] = 6;
  b[1] = 1;
  b[2] = 7;

  auto begin = thrust::make_zip_iterator(thrust::make_tuple(a.begin(), b.begin()));
  auto end = thrust::make_zip_iterator(thrust::make_tuple(a.end(), b.end()));

  // reduce to a tuple
  thrust::tuple<float, float> result = thrust::reduce(begin, end, thrust::make_tuple<float,float>(0,0), TestTuplePlus<float>()); // produce compilation error

  return 0;
}
</code>```

The compilation error:

```<code>/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/detail/function.h(96): error: function "TestTuplePlus<T>::operator() [with T=float]" cannot be called with the given argument list
            argument types are: (thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, thrust::detail::tuple_of_iterator_references<float &, float &, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>)
            object type is: TestTuplePlus<float>
          detected during:
            instantiation of "Result thrust::detail::wrapped_function<Function, Result>::operator()(Argument1 &, const Argument2 &) const [with Function=TestTuplePlus<float>, Result=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, Argument1=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, Argument2=thrust::detail::tuple_of_iterator_references<thrust::device_reference<float>, thrust::device_reference<float>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/detail/sequential/reduce.h(61): here
            instantiation of "OutputType thrust::system::detail::sequential::reduce(thrust::system::detail::sequential::execution_policy<DerivedPolicy> &, InputIterator, InputIterator, OutputType, BinaryFunction) [with DerivedPolicy=thrust::detail::seq_t, InputIterator=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, OutputType=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryFunction=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/detail/reduce.inl(71): here
            instantiation of "T thrust::reduce(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, T, BinaryFunction) [with DerivedPolicy=thrust::detail::seq_t, InputIterator=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryFunction=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/cuda/detail/reduce.h(1022): here
            instantiation of "T thrust::cuda_cub::reduce_n(thrust::cuda_cub::execution_policy<Derived> &, InputIt, Size, T, BinaryOp) [with Derived=thrust::cuda_cub::tag, InputIt=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, Size=signed long, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryOp=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/cuda/detail/reduce.h(1037): here
            instantiation of "T thrust::cuda_cub::reduce(thrust::cuda_cub::execution_policy<Derived> &, InputIt, InputIt, T, BinaryOp) [with Derived=thrust::cuda_cub::tag, InputIt=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryOp=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/detail/reduce.inl(71): here
            instantiation of "T thrust::reduce(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, T, BinaryFunction) [with DerivedPolicy=thrust::cuda_cub::tag, InputIterator=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryFunction=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/detail/reduce.inl(186): here
            instantiation of "T thrust::reduce(InputIterator, InputIterator, T, BinaryFunction) [with InputIterator=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryFunction=TestTuplePlus<float>]"
</code>```

To be honest, I have no idea how to fix it.

I actually find this post, but I haven't got it compiled either.

Anyway, is there an easy way to do tuple reduction in cuda?

答:
The compilation error is due to missing "const" qualifier in your functor, i.e., it should be:</p>
```<code>thrust::tuple<T, T> operator()(const thrust::tuple<T, T>& t0, const thrust::tuple<T, T>& t1)
</code>```

Body:
Say I have a tuple consisting of two thrust::device_vector. I want the output to be a tuple consisting of two scalars, which are the sum of two vectors respectively. For example,

input tuple consisting of two vectors:
a: 3, 5, 2
b: 6, 1, 7
output tuple consisting of two scalars:
10
14

I think this should be extremely easy but somehow I still haven't figure out a way to do so.
My code

#include <iostream>
#include <stdlib.h>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/transform.h>
#include <thrust/tuple.h>
#include <thrust/transform_reduce.h>
#include <thrust/iterator/zip_iterator.h>

template<typename T>
struct TestTuplePlus
{
    __host__ __device__
    thrust::tuple<T, T> operator()(thrust::tuple<T, T>& t0, thrust::tuple<T, T>& t1)
    {
            return thrust::make_tuple(thrust::get<0>(t0) + thrust::get<0>(t1), thrust::get<1>(t0) + thrust::get<1>(t1));
    }
};

int main()
{
  thrust::device_vector<float> a(3, 0);
  thrust::device_vector<float> b(3, 0);

  a[0] = 3;
  a[1] = 5;
  a[2] = 2;
  b[0] = 6;
  b[1] = 1;
  b[2] = 7;

  auto begin = thrust::make_zip_iterator(thrust::make_tuple(a.begin(), b.begin()));
  auto end = thrust::make_zip_iterator(thrust::make_tuple(a.end(), b.end()));

  // reduce to a tuple
  thrust::tuple<float, float> result = thrust::reduce(begin, end, thrust::make_tuple<float,float>(0,0), TestTuplePlus<float>()); // produce compilation error

  return 0;
}

The compilation error:

/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/detail/function.h(96): error: function "TestTuplePlus<T>::operator() [with T=float]" cannot be called with the given argument list
            argument types are: (thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, thrust::detail::tuple_of_iterator_references<float &, float &, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>)
            object type is: TestTuplePlus<float>
          detected during:
            instantiation of "Result thrust::detail::wrapped_function<Function, Result>::operator()(Argument1 &, const Argument2 &) const [with Function=TestTuplePlus<float>, Result=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, Argument1=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, Argument2=thrust::detail::tuple_of_iterator_references<thrust::device_reference<float>, thrust::device_reference<float>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/detail/sequential/reduce.h(61): here
            instantiation of "OutputType thrust::system::detail::sequential::reduce(thrust::system::detail::sequential::execution_policy<DerivedPolicy> &, InputIterator, InputIterator, OutputType, BinaryFunction) [with DerivedPolicy=thrust::detail::seq_t, InputIterator=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, OutputType=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryFunction=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/detail/reduce.inl(71): here
            instantiation of "T thrust::reduce(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, T, BinaryFunction) [with DerivedPolicy=thrust::detail::seq_t, InputIterator=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryFunction=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/cuda/detail/reduce.h(1022): here
            instantiation of "T thrust::cuda_cub::reduce_n(thrust::cuda_cub::execution_policy<Derived> &, InputIt, Size, T, BinaryOp) [with Derived=thrust::cuda_cub::tag, InputIt=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, Size=signed long, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryOp=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/cuda/detail/reduce.h(1037): here
            instantiation of "T thrust::cuda_cub::reduce(thrust::cuda_cub::execution_policy<Derived> &, InputIt, InputIt, T, BinaryOp) [with Derived=thrust::cuda_cub::tag, InputIt=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryOp=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/detail/reduce.inl(71): here
            instantiation of "T thrust::reduce(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, T, BinaryFunction) [with DerivedPolicy=thrust::cuda_cub::tag, InputIterator=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryFunction=TestTuplePlus<float>]"
/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/detail/reduce.inl(186): here
            instantiation of "T thrust::reduce(InputIterator, InputIterator, T, BinaryFunction) [with InputIterator=thrust::zip_iterator<thrust::tuple<thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::detail::normal_iterator<thrust::device_ptr<float>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, T=thrust::tuple<float, float, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, BinaryFunction=TestTuplePlus<float>]"

To be honest, I have no idea how to fix it.

I actually find this post, but I haven't got it compiled either.

Anyway, is there an easy way to do tuple reduction in cuda?

Body:
The compilation error is due to missing "const" qualifier in your functor, i.e., it should be:

thrust::tuple<T, T> operator()(const thrust::tuple<T, T>& t0, const thrust::tuple<T, T>& t1)