问:
Python reload pip doesn't work
I am currently trying to upgrade pip and then install paramiko inside a python script because the script itself makes use of paramiko. Below is a simple case of what I am trying to accomplish

```<code>import pip

try:
        import paramiko
except ImportError:
        pip.main(["install", "--upgrade", "pip"])
        reload(pip)
        pip.main(["install", "--user", "paramiko"])
        import paramiko

ssh = paramiko.SSHClient()
</code>```

I am running this script in an Ubuntu 16.04 VM with python 2.7 which doesn't have paramiko and pip version 8.1.1. For the output of the pip upgrade after running the script, I get:

Collecting pip   Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
      100% |████████████████████████████████| 1.3MB 712kB/s Installing collected packages: pip Successfully installed pip-8.1.1 You are using
  pip version 8.1.1, however version 9.0.1 is available. You should
  consider upgrading via the 'pip install --upgrade pip' command.

After that, it tries to install paramiko as expected, but it fails because it basically can't install the Cryptography dependency that paramiko uses. However, if it was using the upgraded pip, this would not be a problem. I have reverted the VM back to a previous snapshot to manually install paramiko by doing

```<code>pip install --upgrade pip
pip install --user paramiko
</code>```

in the shell and it works, but I need to be able to perform this in a script.

Note, I also reverted the VM to a previous snapshot and tried installing paramiko with the above command in the shell, but without performing the pip upgrade first, and as expected I get the same error as what the script gives when trying to install paramiko.
Now I have done a bit of testing by trying to having this in the script:

```<code>pip.main(["install", "--upgrade", "pip"])
reload(pip)
pip.main(["install", "--upgrade", "pip"])
reload(pip)
</code>```

and as for the output I get this:

Collecting pip

  Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)

```<code>100% |████████████████████████████████| 1.3MB 712kB/s Installing collected packages: pip Successfully installed pip-8.1.1 You are using pip version 8.1.1, however version 9.0.1 is available.
</code>```

You should consider upgrading via the 'pip install --upgrade pip' command.

Collecting pip

  Using cached pip-9.0.1-py2.py3-none-any.whl

Installing collected packages: pip Successfully installed pip-8.1.1

You are using pip version 8.1.1, however version 9.0.1 is available.

You should consider upgrading via the 'pip install --upgrade pip' command.

Notice how it says using cached pip-9.0.1 at the second pip upgrade. Does this mean after the reload, the script is using pip 9.0.1? If so then why is the paramiko install not using the upgraded pip? How can I get the paramiko to use the upgraded pip?

答:
So after some digging around, it looks like this is a pip bug that has been patched. Unfortunately, I may not always have control over the machine that will run this script (i.e. can't apply the patch) so my solution was just to use os.execl() to restart the script whenever the second import for paramiko in the except section fails.

As for the bug, in summary, after a pip upgrade, the wrong version was reported, but pip was upgraded. So if the script was ran again, the new version of pip was used which allowed paramiko to be installed properly, hence the solution to restart the script.

For guidance on using os.execl() since python documentation is not very clear on it, follow this link

Sample of how my code looks now:

```<code>import pip
import sys
import os

try:
        import paramiko
except ImportError:
        pip.main(["install", "--upgrade", "pip"])
        pip.main(["install", "--user", "paramiko"])
        try:
            import paramiko
        except ImportError:
            os.execl(sys.executable, 'python', __file__, *sys.argv[1:])

ssh = paramiko.SSHClient()
</code>```

Title: Python reload pip doesn't work

Body:
I am currently trying to upgrade pip and then install paramiko inside a python script because the script itself makes use of paramiko. Below is a simple case of what I am trying to accomplish

import pip

try:
        import paramiko
except ImportError:
        pip.main(["install", "--upgrade", "pip"])
        reload(pip)
        pip.main(["install", "--user", "paramiko"])
        import paramiko

ssh = paramiko.SSHClient()

I am running this script in an Ubuntu 16.04 VM with python 2.7 which doesn't have paramiko and pip version 8.1.1. For the output of the pip upgrade after running the script, I get:

Collecting pip   Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
      100% |████████████████████████████████| 1.3MB 712kB/s Installing collected packages: pip Successfully installed pip-8.1.1 You are using
  pip version 8.1.1, however version 9.0.1 is available. You should
  consider upgrading via the 'pip install --upgrade pip' command.

After that, it tries to install paramiko as expected, but it fails because it basically can't install the Cryptography dependency that paramiko uses. However, if it was using the upgraded pip, this would not be a problem. I have reverted the VM back to a previous snapshot to manually install paramiko by doing

pip install --upgrade pip
pip install --user paramiko

in the shell and it works, but I need to be able to perform this in a script.

Note, I also reverted the VM to a previous snapshot and tried installing paramiko with the above command in the shell, but without performing the pip upgrade first, and as expected I get the same error as what the script gives when trying to install paramiko.
Now I have done a bit of testing by trying to having this in the script:

pip.main(["install", "--upgrade", "pip"])
reload(pip)
pip.main(["install", "--upgrade", "pip"])
reload(pip)

and as for the output I get this:

Collecting pip

  Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)

100% |████████████████████████████████| 1.3MB 712kB/s Installing collected packages: pip Successfully installed pip-8.1.1 You are using pip version 8.1.1, however version 9.0.1 is available.

You should consider upgrading via the 'pip install --upgrade pip' command.

Collecting pip

  Using cached pip-9.0.1-py2.py3-none-any.whl

Installing collected packages: pip Successfully installed pip-8.1.1

You are using pip version 8.1.1, however version 9.0.1 is available.

You should consider upgrading via the 'pip install --upgrade pip' command.

Notice how it says using cached pip-9.0.1 at the second pip upgrade. Does this mean after the reload, the script is using pip 9.0.1? If so then why is the paramiko install not using the upgraded pip? How can I get the paramiko to use the upgraded pip?

Body:
So after some digging around, it looks like this is a pip bug that has been patched. Unfortunately, I may not always have control over the machine that will run this script (i.e. can't apply the patch) so my solution was just to use os.execl() to restart the script whenever the second import for paramiko in the except section fails.

As for the bug, in summary, after a pip upgrade, the wrong version was reported, but pip was upgraded. So if the script was ran again, the new version of pip was used which allowed paramiko to be installed properly, hence the solution to restart the script.

For guidance on using os.execl() since python documentation is not very clear on it, follow this link

Sample of how my code looks now:

import pip
import sys
import os

try:
        import paramiko
except ImportError:
        pip.main(["install", "--upgrade", "pip"])
        pip.main(["install", "--user", "paramiko"])
        try:
            import paramiko
        except ImportError:
            os.execl(sys.executable, 'python', __file__, *sys.argv[1:])

ssh = paramiko.SSHClient()


来源: stackexchange

问:
Validation of a certificate chain when opening a SSLSocket in Java
My Setup

My goal is to set up a SSL/TLS secured connection (explicit) with an FTP-Server.
The appropriate Root CA Certificate is stored in a Truststore called truststore.jks.
After the AUTH TLScommand I'm using the following code to build up the SSLSocket.

```<code>public SSLSocket enterSecureMode(Socket s) throws Exception {
        KeyStore truststore = KeyStore.getInstance("JKS");
        truststore.load(Files.newInputStream(Paths.get("truststore.jks")), "mypass".toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(truststore);

        SSLContext sCon = SSLContext.getInstance("TLS");
        sCon.init(null, tmf.getTrustManagers(), null);

        SSLSocketFactory sslSocketFactory = sCon.getSocketFactory();
        return (SSLSocket) sslSocketFactory.createSocket(s, "<HOSTNAME>", 21, true);
}
</code>```

The code itself is running fine and I received a secured Socket-Connection, but I wonder whether this can stand attacks like e.g MITM or not. I mean would that program discover an attempt of somebody trying to 'give me a Fake-Certificate'.
Therefore I'd be very happy if some more experienced SSL-Network-Programmers could enlight me :D

答:
This is sufficient. The attacker would have to provide a certificate signed by the root CA. However you don't need all this code: you only need

```<code>System.setProperty("javax.net.ssl.trustStore", "truststore.jks");
SSLContext sCon = SSLContext.getDefault();
SSLSocketFactory sslSocketFactory = sCon.getSocketFactory();
return (SSLSocket) sslSocketFactory.createSocket(s, "<HOSTNAME>", 21, true)
</code>```

If you want to be totally paranoid, after creating the SSLSocket you can get the SSLSession and then the peer certificate chain and make sure that the zeroth entry exactly matches the exact server's certificate, but this step is mostly omitted.

ContentLicense: CC BY-SA 3.0

Body:
My Setup

My goal is to set up a SSL/TLS secured connection (explicit) with an FTP-Server.
The appropriate Root CA Certificate is stored in a Truststore called truststore.jks.
After the AUTH TLScommand I'm using the following code to build up the SSLSocket.

public SSLSocket enterSecureMode(Socket s) throws Exception {
        KeyStore truststore = KeyStore.getInstance("JKS");
        truststore.load(Files.newInputStream(Paths.get("truststore.jks")), "mypass".toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(truststore);

        SSLContext sCon = SSLContext.getInstance("TLS");
        sCon.init(null, tmf.getTrustManagers(), null);

        SSLSocketFactory sslSocketFactory = sCon.getSocketFactory();
        return (SSLSocket) sslSocketFactory.createSocket(s, "<HOSTNAME>", 21, true);
}

The code itself is running fine and I received a secured Socket-Connection, but I wonder whether this can stand attacks like e.g MITM or not. I mean would that program discover an attempt of somebody trying to 'give me a Fake-Certificate'.
Therefore I'd be very happy if some more experienced SSL-Network-Programmers could enlight me :D

Body:
This is sufficient. The attacker would have to provide a certificate signed by the root CA. However you don't need all this code: you only need

System.setProperty("javax.net.ssl.trustStore", "truststore.jks");
SSLContext sCon = SSLContext.getDefault();
SSLSocketFactory sslSocketFactory = sCon.getSocketFactory();
return (SSLSocket) sslSocketFactory.createSocket(s, "<HOSTNAME>", 21, true)

If you want to be totally paranoid, after creating the SSLSocket you can get the SSLSession and then the peer certificate chain and make sure that the zeroth entry exactly matches the exact server's certificate, but this step is mostly omitted.


问:
Nested kernels in CUDA
CUDA currently does not allow nested kernels.

To be specific, I have the following problem:
I have N number of M-dimensional data. To process each of the N data-points, three kernels need to be run in a sequence. Since, nesting of kernels is not allowed, I cannot create a kernel with calls to the three kernels. Therefore, I have to process each data-point serially.

One solution is to write a big kernel containing the functionality of all the other three kernels, but I think it will sub-optimal.

Can anyone suggest how streams can be used to run the N data-points in parallel, while retaining the the three smaller kernels.

Thanks.

答:
Well, if you want to use streams... you will want to create N streams:

```<code>cudaStream_t streams;
streams = malloc(N * sizeof(cudaStream_t));
for(i=0; i<N; i++)
{
    cudaStreamCreate(&streams[i]);
}
</code>```

Then for the ith data point, you want to use cudaMemcpyAsync for transfers:

```<code>cudaMemcpyAsync(dst, src, kind, count, streams[i]);
</code>```

and call your kernels with all four configuration parameters (sharedMemory can be 0, of course):

```<code>kernel_1 <<< nBlocks, nThreads, sharedMemory, streams[i] >>> ( args );
kernel_2 <<< nBlocks, nThreads, sharedMemory, streams[i] >>> ( args );
</code>```

and of course cleanup:

```<code>for(i=0; i<N; i++)
{
    cudaStreamDestroy(streams[i]);
}
free(streams)
</code>```

ContentLicense: CC BY-SA 2.5

Title: Nested kernels in CUDA

Body:
Well, if you want to use streams... you will want to create N streams:

cudaStream_t streams;
streams = malloc(N * sizeof(cudaStream_t));
for(i=0; i<N; i++)
{
    cudaStreamCreate(&streams[i]);
}

Then for the ith data point, you want to use cudaMemcpyAsync for transfers:

cudaMemcpyAsync(dst, src, kind, count, streams[i]);

and call your kernels with all four configuration parameters (sharedMemory can be 0, of course):

kernel_1 <<< nBlocks, nThreads, sharedMemory, streams[i] >>> ( args );
kernel_2 <<< nBlocks, nThreads, sharedMemory, streams[i] >>> ( args );

and of course cleanup:

for(i=0; i<N; i++)
{
    cudaStreamDestroy(streams[i]);
}
free(streams)


问:
SAS ODS Excel to produce a excel sheet with three tables side by side
I am trying to prepare a report in Excel using ODS Excel in SAS. The report contains two sheets with three tables place side by side.  I used Panelcol option but it is not available in ODS Excel. Also, the Start_at= option does not seem to be working?

Is there anyway it can be done in ODS Excel? Please let me know.
Any help will be very much appreciated.

Thank you,
Shankar

答:
Shankar:

The ODS EXCEL docs for 9.4 state the start_at= options can not be changed mid-sheet. Link

(START_AT='string') specifies a starting cell for the report. The
  default is to start at column 1 and row 1. Default    1,1 Tip This option
  cannot be changed in the middle of a sheet. Example    ods excel
  options(start_at="2,2");

So this example does not work as you expect.  The first start_at is honored, and subsequent output on the same page is stacked while still honoring the first start at column.

```<code>ods _all_ close;
ods excel file='%temp%\3-tables-in-first-tab-sample.xlsx'
  options (sheet_interval='none')
;

ods excel options (start_at="2,2");

* top of output is upper left corner;
proc print noobs data=sashelp.class(keep=name age);run;

* top of output is to the right and down three rows from the first output;
ods excel options (start_at="5,5");

proc print noobs data=sashelp.class(keep=name weight height);run;

ods excel options (start_at="1,9");
proc print noobs data=sashelp.class(keep=name sex);run;

ods excel close;
</code>```

One way to put multiple tables together is to create a faux table that appends data in a sidewise manner.

Example:

```<code>%macro RHS_append (out=, data=);
  %if not %sysfunc(exist(&out)) %then %do;
    data &out; set &data; run;
    %return;
  %end;

  %local nout names labels;

  proc contents noprint data=&out out=outvar(keep=name varnum label);
  proc contents noprint data=&data out=datavar(keep=name varnum label);
  proc sql noprint; select count(*) into :nout trimmed from outvar;
  select
    cats(name,'=v',&nout+varnum+1)
  , case
      when label=''
      then cats('v',&nout+varnum+1,'=',quote(trim(name)))
      else ''
    end
  into
    :names separated by ' '
  , :labels separated by ' '
  from datavar order by varnum
  ;
  create table gap (v%eval(&nout+1) char(1) label='a0'x);
  quit;

  data &out;
    merge &out gap &data(rename=(&names)); %* rare use of merge without by;
    label &labels;
  run;

%mend;

options mprint;

proc delete data=foo;
run;
%RHS_append (out=foo, data=sashelp.class);
%RHS_append (out=foo, data=sashelp.gas);
%RHS_append (out=foo, data=sashelp.cars);

ods excel file='%temp%\3-tables-in-first-tab-sample.xlsx';

proc print label data=foo;
run;

ods excel close;
</code>```

Body:
Shankar:

The ODS EXCEL docs for 9.4 state the start_at= options can not be changed mid-sheet. Link

(START_AT='string') specifies a starting cell for the report. The
  default is to start at column 1 and row 1. Default    1,1 Tip This option
  cannot be changed in the middle of a sheet. Example    ods excel
  options(start_at="2,2");

So this example does not work as you expect.  The first start_at is honored, and subsequent output on the same page is stacked while still honoring the first start at column.

ods _all_ close;
ods excel file='%temp%\3-tables-in-first-tab-sample.xlsx'
  options (sheet_interval='none')
;

ods excel options (start_at="2,2");

* top of output is upper left corner;
proc print noobs data=sashelp.class(keep=name age);run;

* top of output is to the right and down three rows from the first output;
ods excel options (start_at="5,5");

proc print noobs data=sashelp.class(keep=name weight height);run;

ods excel options (start_at="1,9");
proc print noobs data=sashelp.class(keep=name sex);run;

ods excel close;

One way to put multiple tables together is to create a faux table that appends data in a sidewise manner.

Example:

%macro RHS_append (out=, data=);
  %if not %sysfunc(exist(&out)) %then %do;
    data &out; set &data; run;
    %return;
  %end;

  %local nout names labels;

  proc contents noprint data=&out out=outvar(keep=name varnum label);
  proc contents noprint data=&data out=datavar(keep=name varnum label);
  proc sql noprint; select count(*) into :nout trimmed from outvar;
  select
    cats(name,'=v',&nout+varnum+1)
  , case
      when label=''
      then cats('v',&nout+varnum+1,'=',quote(trim(name)))
      else ''
    end
  into
    :names separated by ' '
  , :labels separated by ' '
  from datavar order by varnum
  ;
  create table gap (v%eval(&nout+1) char(1) label='a0'x);
  quit;

  data &out;
    merge &out gap &data(rename=(&names)); %* rare use of merge without by;
    label &labels;
  run;

%mend;

options mprint;

proc delete data=foo;
run;
%RHS_append (out=foo, data=sashelp.class);
%RHS_append (out=foo, data=sashelp.gas);
%RHS_append (out=foo, data=sashelp.cars);

ods excel file='%temp%\3-tables-in-first-tab-sample.xlsx';

proc print label data=foo;
run;

ods excel close;


问:
Speed up a while loop in Matlab using cellfun
I have the following code in Matlab.

```<code>pc = [0.0195    0.2356    0.6280
    0.0229    0.2356    0.6280
    0.0393    0.2318    0.6180
    0.0196    0.2350    0.6310
    0.0212    0.2343    0.6290
    0.0230    0.2350    0.6310
    0.0245    0.2332    0.6260
    0.0378    0.2313    0.6210
    0.0408    0.2294    0.6160
    0.0426    0.2302    0.6180];

x = pc(:,1);
y = pc(:,2);
z = pc(:,3);

dt = delaunayTriangulation(pc);
dtTri = [dt(:,[1,2,3]);dt(:,[2,3,4]);dt(:,[1,2,4]);dt(:,[1,3,4])];
dtTri = unique(sort(dtTri,2),'rows');

i = 1;
lim = .005;

while (i<=size(dtTri,1))
    d12 = sqrt(abs(x(dtTri(i,1))-x(dtTri(i,2)))^2+...
        abs(y(dtTri(i,1))-y(dtTri(i,2)))^2+...
        abs(z(dtTri(i,1))-z(dtTri(i,2)))^2);

    if (d12>lim)
        dtTri(i,:)=[];
        continue;
    end
    i = i+1;
end
</code>```

Is there any way in Matlab to reduce the running time of the above code (while loop), using something like cellfun? Because my pc size is very large and it takes a long time to run the code. Thank you.

答:
If I understand the problem, you can remove the whole while and replace it with the following code:

```<code> d12 = sqrt((x(dtTri(:,1))-x(dtTri(:,2))).^2 + ...
 (y(dtTri(:,1))-y(dtTri(:,2))).^2 + ...
 (z(dtTri(:,1))-z(dtTri(:,2))).^2);
 foundLimit = find(d12 <= lim);
 newDtTri = dtTri(foundLimit,:);
</code>```

Body:
I have the following code in Matlab.

pc = [0.0195    0.2356    0.6280
    0.0229    0.2356    0.6280
    0.0393    0.2318    0.6180
    0.0196    0.2350    0.6310
    0.0212    0.2343    0.6290
    0.0230    0.2350    0.6310
    0.0245    0.2332    0.6260
    0.0378    0.2313    0.6210
    0.0408    0.2294    0.6160
    0.0426    0.2302    0.6180];

x = pc(:,1);
y = pc(:,2);
z = pc(:,3);

dt = delaunayTriangulation(pc);
dtTri = [dt(:,[1,2,3]);dt(:,[2,3,4]);dt(:,[1,2,4]);dt(:,[1,3,4])];
dtTri = unique(sort(dtTri,2),'rows');

i = 1;
lim = .005;

while (i<=size(dtTri,1))
    d12 = sqrt(abs(x(dtTri(i,1))-x(dtTri(i,2)))^2+...
        abs(y(dtTri(i,1))-y(dtTri(i,2)))^2+...
        abs(z(dtTri(i,1))-z(dtTri(i,2)))^2);

    if (d12>lim)
        dtTri(i,:)=[];
        continue;
    end
    i = i+1;
end

Is there any way in Matlab to reduce the running time of the above code (while loop), using something like cellfun? Because my pc size is very large and it takes a long time to run the code. Thank you.

Body:
If I understand the problem, you can remove the whole while and replace it with the following code:

 d12 = sqrt((x(dtTri(:,1))-x(dtTri(:,2))).^2 + ...
 (y(dtTri(:,1))-y(dtTri(:,2))).^2 + ...
 (z(dtTri(:,1))-z(dtTri(:,2))).^2);
 foundLimit = find(d12 <= lim);
 newDtTri = dtTri(foundLimit,:);


问:
Why my app crashes when I try to assign value of one vectors element to another vector element?
I'm very new to C++. I try to find vertices of my shape and draw circles around them. I draw circles in this line of main function:

```<code>drawVertices(getDrawingVertices(getAllPoints()), src_clone);
</code>```

I think that everything is all right with my drawVertices() function:

```<code>void drawVertices(vector<Point> drawingVertices, Mat src_clone) {
    for( int i = 0; i < drawingVertices.size(); i++ ){
        circle(src_clone, drawingVertices[i], 5, Scalar(255), 3, 8);
    }
}
</code>```

But not all right with getDrawingVertices() function. My app crashes when I try to assign value of one vectors element to another vectors element:

drawingVertices[i] = pointsArray[topLength];

Full code of function getDrawingVertices() is here:

```<code>vector<Point> getDrawingVertices (vector<Point> pointsArray) {
       int N = pointsArray.size();
       int numberOfDrawingVertices = 8;
       vector<Point> drawingVertices(numberOfDrawingVertices);
       vector<int> lengthArray(N);
       Point centerPoint = findCenter();

   for (int i = 0; i < N; i++) {
        lengthArray[i] = floor(pow((pow((pointsArray[i].x - centerPoint.x),2) +
        pow((pointsArray[i].y - centerPoint.y),2)),0.5));
   }

   for (int i = 0; i < 8; i++) {
        int topLength = 0;
        for (int j = 0; j < N; j++) {
            if (lengthArray[j] > lengthArray[topLength]) {
                topLength = j;
            }
        }
        drawingVertices[i] = pointsArray[topLength];
        lengthArray[topLength] = 0;
   }
   return drawingVertices;
}
</code>```

And here is the code of function getAllPoints ():

```<code>vector<Point> getAllPoints () {
    vector<Point> pointsArray;
        for( int i = 0; i < contours.size(); i++ ){
            for (int j = 0; j < contours[i].size(); j++) {
                pointsArray.push_back(contours[i][j]);
            }
    }
    return pointsArray;
}
</code>```

Edit

Function findCenter():

```<code> Point findCenter() {

    vector<Moments> mu(contours.size() );
    for( int i = 0; i < contours.size(); i++ ){
        mu[i] = moments( contours[i], false );
    }
    vector<Point2f> mc( contours.size() );
    for( int i = 0; i < contours.size(); i++ ) {
        mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 );
    }

    int SumX = 0;
    int SumY = 0;
    int pointCol = 0;
    for(int j= 0; j < mc.size();j++) {
       SumX += mc[j].x;
       SumY += mc[j].y;
       pointCol++;
    }

    Point centerPoint;
    centerPoint.x = floor(SumX/pointCol);
    centerPoint.y = floor(SumY/pointCol);
    return centerPoint;
}
</code>```

Where I have a mistake?

答:
You should learn to debug your code yourself.

If your app crashes at this line

```<code>drawingVertices[i] = pointsArray[topLength];
</code>```

You should check the length of drawingVertices and pointsArray, as well as i and topLength.  Check that i>=0, i<drawingVertices.size(), and same for pointsArray and topLength.  If one of these condition does not obey, check when and how it becomes wrong.

(On a side note, instead of pow(x, 0.5) use sqrt(x). But it is not connected to your problem.)

ContentLicense: CC BY-SA 4.0

Body:
I'm very new to C++. I try to find vertices of my shape and draw circles around them. I draw circles in this line of main function:

drawVertices(getDrawingVertices(getAllPoints()), src_clone);

I think that everything is all right with my drawVertices() function:

void drawVertices(vector<Point> drawingVertices, Mat src_clone) {
    for( int i = 0; i < drawingVertices.size(); i++ ){
        circle(src_clone, drawingVertices[i], 5, Scalar(255), 3, 8);
    }
}

But not all right with getDrawingVertices() function. My app crashes when I try to assign value of one vectors element to another vectors element:

drawingVertices[i] = pointsArray[topLength];

Full code of function getDrawingVertices() is here:

vector<Point> getDrawingVertices (vector<Point> pointsArray) {
       int N = pointsArray.size();
       int numberOfDrawingVertices = 8;
       vector<Point> drawingVertices(numberOfDrawingVertices);
       vector<int> lengthArray(N);
       Point centerPoint = findCenter();

   for (int i = 0; i < N; i++) {
        lengthArray[i] = floor(pow((pow((pointsArray[i].x - centerPoint.x),2) +
        pow((pointsArray[i].y - centerPoint.y),2)),0.5));
   }

   for (int i = 0; i < 8; i++) {
        int topLength = 0;
        for (int j = 0; j < N; j++) {
            if (lengthArray[j] > lengthArray[topLength]) {
                topLength = j;
            }
        }
        drawingVertices[i] = pointsArray[topLength];
        lengthArray[topLength] = 0;
   }
   return drawingVertices;
}

And here is the code of function getAllPoints ():

vector<Point> getAllPoints () {
    vector<Point> pointsArray;
        for( int i = 0; i < contours.size(); i++ ){
            for (int j = 0; j < contours[i].size(); j++) {
                pointsArray.push_back(contours[i][j]);
            }
    }
    return pointsArray;
}

Edit

Function findCenter():

 Point findCenter() {

    vector<Moments> mu(contours.size() );
    for( int i = 0; i < contours.size(); i++ ){
        mu[i] = moments( contours[i], false );
    }
    vector<Point2f> mc( contours.size() );
    for( int i = 0; i < contours.size(); i++ ) {
        mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 );
    }

    int SumX = 0;
    int SumY = 0;
    int pointCol = 0;
    for(int j= 0; j < mc.size();j++) {
       SumX += mc[j].x;
       SumY += mc[j].y;
       pointCol++;
    }

    Point centerPoint;
    centerPoint.x = floor(SumX/pointCol);
    centerPoint.y = floor(SumY/pointCol);
    return centerPoint;
}

Where I have a mistake?

Body:
You should learn to debug your code yourself.

If your app crashes at this line

drawingVertices[i] = pointsArray[topLength];

You should check the length of drawingVertices and pointsArray, as well as i and topLength.  Check that i>=0, i<drawingVertices.size(), and same for pointsArray and topLength.  If one of these condition does not obey, check when and how it becomes wrong.

(On a side note, instead of pow(x, 0.5) use sqrt(x). But it is not connected to your problem.)


问:
How do I change this so it fails the way I want?
I think the following code stops if it encounters any error, period.

```<code>public static void exportDatapointsToCSV(List<Datapoint> datapointList, File exportDir) {

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy (h:mm a)", Locale.US);
    File file = new File(exportDir, "my_datapoints.csv");

    try {
        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
        csvWrite.writeNext(new String[]{"DATAPOINT_LABEL", "VALUE", "TIMESTAMP", "TIMESTAMP_FORMATTED", "NOTE"});

        for (Datapoint datapoint : datapointList) {
            String arrStr[] = new String[]{
                    datapoint.getLabel(),
                    datapoint.getValue() + "",
                    datapoint.getTimestamp() + "",
                    sdf.format(new Date(datapoint.getTimestamp())) + "",
                    datapoint.getNote()
            };
            csvWrite.writeNext(arrStr);
        }
        csvWrite.close();
    }
    catch (Exception sqlEx) {
        Log.e(LOG_TAG, sqlEx.getMessage(), sqlEx);
    }
}
</code>```

But I want it to:

Throw an error and stop if it can't create the file to begin with.

If it encounters an error on a given row, log it but move onto the next row, rather than stopping the entire thing.

Do I just move the try/catch into the loop? Does that cover it?

答:
It is better if I show right in the code:</p>

```<code>public static void exportDatapointsToCSV(List<Datapoint> datapointList, File exportDir) {

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy (h:mm a)", Locale.US);

    File file = null;
    try{ ( file = new File(exportDir, "my_datapoints.csv") ).createNewFile(); }
    catch(IOException e){throw new RuntimeException(e); }

    CSVWriter csvWrite = null;
    try {
        csvWrite = new CSVWriter(new FileWriter(file));
        csvWrite.writeNext(new String[]{"DATAPOINT_LABEL", "VALUE", "TIMESTAMP", "TIMESTAMP_FORMATTED", "NOTE"});

        for (Datapoint datapoint : datapointList) {
            String arrStr[] = new String[]{
                    datapoint.getLabel(),
                    datapoint.getValue() + "",
                    datapoint.getTimestamp() + "",
                    sdf.format(new Date(datapoint.getTimestamp())) + "",
                    datapoint.getNote()
            };
            try{ csvWrite.writeNext(arrStr); }
            catch(Exception e){LOG.error(e);}
        }
    }
    catch (Exception sqlEx) {
        Log.e(LOG_TAG, sqlEx.getMessage(), sqlEx);
    }finally{
       if (csvWrite != null){
           csvWrite.close();
       }
    }
}
</code>```

Body:
I think the following code stops if it encounters any error, period.

public static void exportDatapointsToCSV(List<Datapoint> datapointList, File exportDir) {

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy (h:mm a)", Locale.US);
    File file = new File(exportDir, "my_datapoints.csv");

    try {
        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
        csvWrite.writeNext(new String[]{"DATAPOINT_LABEL", "VALUE", "TIMESTAMP", "TIMESTAMP_FORMATTED", "NOTE"});

        for (Datapoint datapoint : datapointList) {
            String arrStr[] = new String[]{
                    datapoint.getLabel(),
                    datapoint.getValue() + "",
                    datapoint.getTimestamp() + "",
                    sdf.format(new Date(datapoint.getTimestamp())) + "",
                    datapoint.getNote()
            };
            csvWrite.writeNext(arrStr);
        }
        csvWrite.close();
    }
    catch (Exception sqlEx) {
        Log.e(LOG_TAG, sqlEx.getMessage(), sqlEx);
    }
}

But I want it to:

Throw an error and stop if it can't create the file to begin with.

If it encounters an error on a given row, log it but move onto the next row, rather than stopping the entire thing.

Do I just move the try/catch into the loop? Does that cover it?

Body:
It is better if I show right in the code:

public static void exportDatapointsToCSV(List<Datapoint> datapointList, File exportDir) {

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy (h:mm a)", Locale.US);

    File file = null;
    try{ ( file = new File(exportDir, "my_datapoints.csv") ).createNewFile(); }
    catch(IOException e){throw new RuntimeException(e); }

    CSVWriter csvWrite = null;
    try {
        csvWrite = new CSVWriter(new FileWriter(file));
        csvWrite.writeNext(new String[]{"DATAPOINT_LABEL", "VALUE", "TIMESTAMP", "TIMESTAMP_FORMATTED", "NOTE"});

        for (Datapoint datapoint : datapointList) {
            String arrStr[] = new String[]{
                    datapoint.getLabel(),
                    datapoint.getValue() + "",
                    datapoint.getTimestamp() + "",
                    sdf.format(new Date(datapoint.getTimestamp())) + "",
                    datapoint.getNote()
            };
            try{ csvWrite.writeNext(arrStr); }
            catch(Exception e){LOG.error(e);}
        }
    }
    catch (Exception sqlEx) {
        Log.e(LOG_TAG, sqlEx.getMessage(), sqlEx);
    }finally{
       if (csvWrite != null){
           csvWrite.close();
       }
    }
}


问:
WPF iDataErrorInfo (validating textboxes) not working as intended
UPDATE: I fixed this by improving my switch statement. Made use of nameof!

I'm trying to validate a set of user inputs from textboxes.

I have my class with interface setup. Snippet of it below:

```<code>public class PatientValidation : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _id;
        private string _fname;

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string p)
        {
            PropertyChangedEventHandler ph = PropertyChanged;
            if (ph != null)
            {
                ph(this, new PropertyChangedEventArgs(p));
            }

        }

        public string Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        public string Fname
        {
            get
            {
                return _fname;
            }
            set
            {
                _fname = value;
            }
        }
</code>```

And a switch statement to return error message based on user input:

```<code>public string this[string PropertyName]
        {
            get
            {
                string result = null;

                switch (PropertyName)
                {
                    case "Id":
                        if (string.IsNullOrEmpty(Id))
                            result = "ID number is required.";
                        break;

                    case "fname":
                        if (string.IsNullOrEmpty(Fname))
                            result = "First name is required.";
                        break;
                  }
                    return result;
             }
          }
</code>```

My relevant code in XAML:

```<code><Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

<TextBox x:Name="textBox_IDNumber" Text="{Binding Id, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
<TextBox x:Name="textBox_FirstName" Text="{Binding Fname, Mode=TwoWay, ValidatesOnDataErrors=True"}/>
</code>```

Here's the problem I've run into: ONLY the first textbox (ID) is validated correctly and the error tooltip is shown. None of the other textboxes.
Different bindings and triggers hasn't solved the issue. Any help would be much appreciated.

答:
Typo error (you used lowercase in your switch):

```<code>case "fname":
</code>```

but in your Binding:

```<code>Text="{Binding Fname
</code>```

Body:
UPDATE: I fixed this by improving my switch statement. Made use of nameof!

I'm trying to validate a set of user inputs from textboxes.

I have my class with interface setup. Snippet of it below:

public class PatientValidation : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _id;
        private string _fname;

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string p)
        {
            PropertyChangedEventHandler ph = PropertyChanged;
            if (ph != null)
            {
                ph(this, new PropertyChangedEventArgs(p));
            }

        }

        public string Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        public string Fname
        {
            get
            {
                return _fname;
            }
            set
            {
                _fname = value;
            }
        }

And a switch statement to return error message based on user input:

public string this[string PropertyName]
        {
            get
            {
                string result = null;

                switch (PropertyName)
                {
                    case "Id":
                        if (string.IsNullOrEmpty(Id))
                            result = "ID number is required.";
                        break;

                    case "fname":
                        if (string.IsNullOrEmpty(Fname))
                            result = "First name is required.";
                        break;
                  }
                    return result;
             }
          }

My relevant code in XAML:

        </Style>

<TextBox x:Name="textBox_IDNumber" Text="{Binding Id, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
<TextBox x:Name="textBox_FirstName" Text="{Binding Fname, Mode=TwoWay, ValidatesOnDataErrors=True"}/>

Here's the problem I've run into: ONLY the first textbox (ID) is validated correctly and the error tooltip is shown. None of the other textboxes.
Different bindings and triggers hasn't solved the issue. Any help would be much appreciated.

Body:
Typo error (you used lowercase in your switch):

case "fname":

but in your Binding:

Text="{Binding Fname


问:
windows controls are disappearing in a win form after installing for adifferent locale
I am new to windows forms and localization.I have a windows  form which is containing some radio buttons and checkboxes and some buttons. this from is impleneted as a child form inside a master form. the problems i am facing now is when i install the english build controls are appearing as they are positioned while desinging. But the problem is when i install the danish build for the same app the controls are not visible in the UI. but they are available somewhere on the Winform. the reason for making this statement is when the tab+ enter keys are pressed the browse dailog which is availble in the UI is opening but could not see it.

I tried the following ways.
set the Locked property of the controls to true.
set the autosize as true
anchor all the controls to left..

please let me know if anyone have any pointers on this ..

Thanks in advance..

答:
I am not quite sure what type of localization you guys do, so I might be not 100% right here, but it seems that Danish resx file (something.da.resx) defines incorrect Location for the missing controls. Make sure that Location and Size values are correct.

If you are using some kind of tool to localize the contents, still Location of the controls is likely the cause, but I can't tell you why it happen (version incompatibility?).


问:
I can't install JDK7 on Ubuntu 16.04 Download Failed
I'm trying to install jdk7 on a my Ubuntu 16.04 machine, here are the commands that I executed:

```<code>sudo apt-get update
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
</code>```

The last one returned Download Failed error and was not able to install the package.

Error Screenshot

When I did some digging I found that Oracle no longer provide JDK7 download link on their official site.

If someone has figured out a way to install it please help !

答:
You should restart your machine and then again try installing.

Or

Try openjdk as

```<code>sudo apt-get update
sudo apt-get install openjdk-7-jdk
</code>```

openjdk is an open source alternative to oracles jdk. And it should be much preferred on an open source OS like ubuntu

Body:
I'm trying to install jdk7 on a my Ubuntu 16.04 machine, here are the commands that I executed:

sudo apt-get update
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

The last one returned Download Failed error and was not able to install the package.

Error Screenshot

When I did some digging I found that Oracle no longer provide JDK7 download link on their official site.

If someone has figured out a way to install it please help !

Body:
You should restart your machine and then again try installing.

Or

Try openjdk as

sudo apt-get update
sudo apt-get install openjdk-7-jdk

openjdk is an open source alternative to oracles jdk. And it should be much preferred on an open source OS like ubuntu


问:
Permissions error with Apache Beam example on Google Dataflow
I'm having trouble submitting an Apache Beam example from a local machine to our cloud platform.

Using gcloud auth list I can see that the correct account is currently active. I can use gsutil and the web client to interact with the file system. I can use the cloud shell to run pipelines through the python REPL.

But when I try and run the python wordcount example I get the following error:

```<code>IOError: Could not upload to GCS path gs://my_bucket/tmp: access denied.
Please verify that credentials are valid and that you have write access
to the specified path.
</code>```

Is there something I am missing with regards to the credentials?

答:
Same Error Solved after creating a bucket.

gsutil mb gs://<bucket-name-from-the-error>/

Body:
I'm having trouble submitting an Apache Beam example from a local machine to our cloud platform.

Using gcloud auth list I can see that the correct account is currently active. I can use gsutil and the web client to interact with the file system. I can use the cloud shell to run pipelines through the python REPL.

But when I try and run the python wordcount example I get the following error:

IOError: Could not upload to GCS path gs://my_bucket/tmp: access denied.
Please verify that credentials are valid and that you have write access
to the specified path.

Is there something I am missing with regards to the credentials?


问:
How to show custom error in the admin
I'm raising a validationError in pre_save but whenever I try to create a new object, instead of getting the red error as the others, I get a yellow page with the error.</p>

```<code>@receiver(pre_save, sender=Student)
def student_pre_save(sender,instance, **kwargs):

    if instance.classroom.student_set.count() == instance.classroom.QttMax_Stu:
        raise ValidationError("No places left")
</code>```

答:
```<code>def clean(self, *args, **kwargs):
    try:
        super(Student, self).save(*args, **kwargs)
    except ValidationError:
        raise ValidationError("No places left")
</code>```

I've put this in the model and it worked great.

Title: How to show custom error in the admin

Body:
I'm raising a validationError in pre_save but whenever I try to create a new object, instead of getting the red error as the others, I get a yellow page with the error.

@receiver(pre_save, sender=Student)
def student_pre_save(sender,instance, **kwargs):

    if instance.classroom.student_set.count() == instance.classroom.QttMax_Stu:
        raise ValidationError("No places left")

Body:
def clean(self, *args, **kwargs):
    try:
        super(Student, self).save(*args, **kwargs)
    except ValidationError:
        raise ValidationError("No places left")

I've put this in the model and it worked great.


问:
Center content in responsive bootstrap navbar
I'm having trouble centering my content in the bootstrap navbar. I'm using bootstrap 3. I've read many posts, but the CSS or methods used will not work with my code! I'm really frustrated, so this is like my last option. Any help would be appreciated!

```<code><!DOCTYPE html>
<html>
  <head>
    <title>Navigation</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="css/style.css" rel="stylesheet" media="screen">
  </head>
  <body>
    <div class="container">
      <nav class="navbar navbar-default" role="navigation">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse navbar-ex1-collapse">
          <ul class="nav navbar-nav">
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
          <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
              <ul class="dropdown-menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li><a href="#">Separated link</a></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </nav>
      <h1>Home</h1>

   </div>
     <!--JAVASCRIPT-->
   <script src="js/jquery-1.10.2.min.js"></script>
   <script src="js/bootstrap.min.js"></script>
 </body>
</html>
</code>```

https://jsfiddle.net/amk07fb3/

答:
V4 of BootStrap is adding Center (justify-content-center) and Right Alignment (justify-content-end) as per: https://v4-alpha.getbootstrap.com/components/navs/#horizontal-alignment</p>

```<code><ul class="nav justify-content-center">
  <li class="nav-item">
    <a class="nav-link active" href="#">Active</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#">Disabled</a>
  </li>
</ul>
</code>```

Body:
I'm having trouble centering my content in the bootstrap navbar. I'm using bootstrap 3. I've read many posts, but the CSS or methods used will not work with my code! I'm really frustrated, so this is like my last option. Any help would be appreciated!

<!DOCTYPE html>

    <title>Navigation</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="css/style.css" rel="stylesheet" media="screen">

          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
            Toggle navigation

          </button>

Link

Link

Link

Link

              Dropdown

Action

Another action

Something else here

Separated link

One more separated link

Home

https://jsfiddle.net/amk07fb3/

Body:
V4 of BootStrap is adding Center (justify-content-center) and Right Alignment (justify-content-end) as per: https://v4-alpha.getbootstrap.com/components/navs/#horizontal-alignment

<ul class="nav justify-content-center">
  <li class="nav-item">
    <a class="nav-link active" href="#">Active</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#">Disabled</a>
  </li>
</ul>


问:
Is there a boilerplate somewhere for a MacOS application with a dark sidebar
Trying to learn Swift and MacOS application development. I'm looking for a boilerplate project for a rather 'standard' MacOS application as seen here - dark sidebar and main content window.

I'd appreciate any pointers.

答:
I don't have a boilerplate project to point you at (and requests for “a book, tool, software library, tutorial or other off-site resource” are off-topic anyway). But I can tell you how to get two of the main customizations you see in that screen shot:

To get the dark appearance, you set the appearance property of the sidebar's top-level view to NSAppearance(named: NSAppearanceNameVibrantDark). For example, in your sidebar view controller:

```<code>class SidebarController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)
    }
</code>```

To get the fat titlebar with the search box requires two steps.

Give the window a toolbar. Delete the standard items (Colors, Fonts, Print) from the toolbar and add a search field to it. Set the toolbar's “Display” to “Icon Only”.

In code, set the window's titleVisibility to .hidden. For example, in your window controller:

```<code>class MainWindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        window?.titleVisibility = .hidden
    }
</code>```

That setting tells AppKit to merge the toolbar into the title bar and not draw the window title. If you want to show the window title, add a label to the toolbar and set its stringValue to your window title.

Result:

Body:
I don't have a boilerplate project to point you at (and requests for “a book, tool, software library, tutorial or other off-site resource” are off-topic anyway). But I can tell you how to get two of the main customizations you see in that screen shot:

To get the dark appearance, you set the appearance property of the sidebar's top-level view to NSAppearance(named: NSAppearanceNameVibrantDark). For example, in your sidebar view controller:

class SidebarController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)
    }

To get the fat titlebar with the search box requires two steps.

Give the window a toolbar. Delete the standard items (Colors, Fonts, Print) from the toolbar and add a search field to it. Set the toolbar's “Display” to “Icon Only”.

In code, set the window's titleVisibility to .hidden. For example, in your window controller:

class MainWindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        window?.titleVisibility = .hidden
    }

That setting tells AppKit to merge the toolbar into the title bar and not draw the window title. If you want to show the window title, add a label to the toolbar and set its stringValue to your window title.

Result:


问:
Merge two parquet files in HDFS
I have some files in HDFS in parquet format. I would like to merge these files into one single large file.

How can I do that?

I have done some thing like below but for text files.

```<code>hadoop fs -cat /input_hdfs_dir/* | hadoop fs -put - /output_hdfs_file
</code>```

But unable to achieve the desired result in parquet format.

How can I achieve my requirement?

答:
Its not possible to merge parquet files with hdfs commands.

There is a parquet-tools library that can help you achieve the merging of parquet files. The command should be

```<code>java jar ./parquet-tools-<VERSION>.jar <command> <input-directory> <output-file>
</code>```

Title: Merge two parquet files in HDFS

Body:
I have some files in HDFS in parquet format. I would like to merge these files into one single large file.

How can I do that?

I have done some thing like below but for text files.

hadoop fs -cat /input_hdfs_dir/* | hadoop fs -put - /output_hdfs_file

But unable to achieve the desired result in parquet format.

How can I achieve my requirement?

Body:
Its not possible to merge parquet files with hdfs commands.

There is a parquet-tools library that can help you achieve the merging of parquet files. The command should be

java jar ./parquet-tools-<VERSION>.jar <command> <input-directory> <output-file>


问:
Copy/paste from JTextPane to others application without text formatting support
I have a JTextPane with contentType set to "text/html".  The content of the JTextPane is formatted with html.  My lines are formatted like this: "
hello
\n".

I'm able to copy data from my JTextPane to an application that support text formatting like OpenOffice (I get my text with formatting like color and lines breaks), but when I try to paste my data to an application that doesn't support text formatting (like gedit), I lose my lines breaks (I get all the lines merged into one single line).

Any idea how to be able to correctly paste my text with lines breaks to application that doesn't support text formatting?

Thanks!

答:
Forget it... I found that my html formatting was bad.  It's working now with a correct formatting.  I don't even need a \n after the
 tags, swing convert it to line break automatically.

Body: Forget it... I found that my html formatting was bad.  It's working now with a correct formatting.  I don't even need a \n after the &lt;p&gt; tags, swing convert it to line break automatically.


问:
Using vim on a server
I am using vim on remote Linux server. I want to copy some data from my local machine and paste it in vim. How can I do that?
I have already tried Ctrl+Shift+V and p option, but it does not paste the text that was copied from the local machine.</p>

答: Data can be pasted in the editor using shift+insert.</p>

Title: Using vim on a server


问:
Sentiment Analysis using tensorflow
I am exploring tensorflow and would like to do sentiment analysis using the options available. I had a look at the following tutorial http://www.tensorflow.org/tutorials/recurrent/index.html#language_modeling

I have worked woth Naive Bayes Classifier, Maximum Entropy Algorithm and Scikit Learn Classifier and would like to know if there are any better algorithms offered by tensorflow. Is this the right place to start or are there any other options?

Any help pointing in the right direction would be greatly appreciated.

Thanks in advance.

答:
I would suggest you try a character-level LSTM, it's been shown to be able to achieve state-of-the-art results in many text classification tasks one of them being sentiment analysis.

I wrote a pretty lengthy article that you can find here where I go through it's implementation in TensorFlow line by line. The result is a model that is less than 100mb in size and that achieves an accuracy of over 80% on a test set of 80,000 tweets.

Another approach that has proven to be very effective is to use a recursive neural network, you can read the paper from Stanford NLP Group here

Title: Sentiment Analysis using tensorflow


问:
Can we run multiple modules in ansible using single ad-hoc command?
I know it's possible to run multiple adhoc commands one after the other for each module and using playbook.

Playbook:

```<code>- hosts: webservers
  tasks:
   - name: create .ssh dir
     file: path ~/.ssh state=directory
   - name: copy pub key
     copy: src:~/.ssh/id.rsa_pub dest=~/.ssh/authorized_keys
</code>```

I want the above to execute using adhoc in one line. Is it possible to do so?

答:
No, it is not possible.

ansible command accepts only one set of arguments for a single module and its parameters.

-m MODULE_NAME, --module-name=MODULE_NAME

  module name to execute (default=command)

Body:
I know it's possible to run multiple adhoc commands one after the other for each module and using playbook.

Playbook:

- hosts: webservers
  tasks:
   - name: create .ssh dir
     file: path ~/.ssh state=directory
   - name: copy pub key
     copy: src:~/.ssh/id.rsa_pub dest=~/.ssh/authorized_keys

I want the above to execute using adhoc in one line. Is it possible to do so?


问:
Javascript does not send data to the output
I am trying to run a JavaScript when the selected value in a dropdown is changed. But the script isnt working.

Here is my dropdown:

```<code><div class="form-group">
  <label>Status</label>
    <div>
      <select class="form-control select2" id="data" name="data" style="width: 100%;">
        <option value="1">Data one</option>
        <option value="0">Data two</option>
    </select>
  </div>
</div>
</code>```

Here is my javascript:

```<code><script type="text/javascript">
  var dataEl = document.querySelector('#data'),
    outputEl = document.querySelector('#output');

    dataEl.onchange = function() {
      outputEl.innerHTML = dataEl.value
        if (1 === dataEl.value) { '<b>Value one</b>' }
        if (0 === dataEl.value) { '<b>Value two</b>' }
  };
</script>
</code>```

When the selected value = 1 the script needs to send Value one to the output. When the selected value = 0 the script needs to send Value two to the output.

The output looks like:

I also tried to add selected to the options, but the script still not sends the data to the output.

Does someone know what is wrong?

https://jsfiddle.net/7fa84ufc/

答:
Give this a try. I made a couple changes:

I changed the second option's value to "2" instead of "0".

I compared the value against the strings "1" and "2" instead of the numbers 1 and 2.

I added code to actually change the HTML in the output paragraph.

I put an initial value of Value one in the output paragraph so it shows when the page is initially loaded.

Your fiddle is also broken because the output paragraph doesn't exist at the point the JavaScript is executed. Move that above the script.

var dataEl = document.querySelector('#data'),
    outputEl = document.querySelector('#output');

dataEl.onchange = function() {
  if (dataEl.value === "1") {
    outputEl.innerHTML = 'Value one';
  } else if (dataEl.value === "2") {
    outputEl.innerHTML = 'Value two';
  }
};```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="form-group">
  <label>Status</label>
  <div>
    <select id="data" name="data">
      <option value="1">Data one</option>
      <option value="2">Data two</option>
    </select>
  </div>
</div>

<p id="output" name="output"><b>Value one</b></p></code>```

Body:
I am trying to run a JavaScript when the selected value in a dropdown is changed. But the script isnt working.

Here is my dropdown:

  Status

      <select class="form-control select2" id="data" name="data" style="width: 100%;">
        <option value="1">Data one</option>
        <option value="0">Data two</option>
    </select>

Here is my javascript:

When the selected value = 1 the script needs to send Value one to the output. When the selected value = 0 the script needs to send Value two to the output.

The output looks like:

I also tried to add selected to the options, but the script still not sends the data to the output.

Does someone know what is wrong?

https://jsfiddle.net/7fa84ufc/

Body:
Give this a try. I made a couple changes:

I changed the second option's value to "2" instead of "0".

I compared the value against the strings "1" and "2" instead of the numbers 1 and 2.

I added code to actually change the HTML in the output paragraph.

I put an initial value of <b>Value one</b> in the output paragraph so it shows when the page is initially loaded.

Your fiddle is also broken because the output paragraph doesn't exist at the point the JavaScript is executed. Move that above the script.

var dataEl = document.querySelector('#data'),
    outputEl = document.querySelector('#output');

dataEl.onchange = function() {
  if (dataEl.value === "1") {
    outputEl.innerHTML = '<b>Value one</b>';
  } else if (dataEl.value === "2") {
    outputEl.innerHTML = '<b>Value two</b>';
  }
};

<div class="form-group">
  <label>Status</label>
  <div>
    <select id="data" name="data">
      <option value="1">Data one</option>
      <option value="2">Data two</option>
    </select>
  </div>
</div>

<p id="output" name="output"><b>Value one</b></p>


问:
Distributed Data Structure across multiple machines with multicast
I am looking into the possibility of Distributing a data structure across multiple machines. I would like it to run in a process on each machine, and using (Multicast?) replicate a copy of the full data structure on all of the machines.

Does anyone have any experience in this that could point me in the right direction?

答: You can write your data into a central database. Then each instance can access it. If you want to modify the data from an instance you shoul implement a mechanism of locking the data in the database. Is this of any help?</p>


问:
How to compare nested dictionaries?
I have two nested dictionaries.

```<code>dict1 = {(t1,name):{('11','22'):{'33':'456','77':'891'},
                    ('121','212'):{'32':'123', '23':'546'}}}
dict2 = {(t1,name):{('11','22'):{'33':'456','77':'891'},
                     ('121','212'):{'32':'123', '23':'546'}}}
</code>```

Basically both the dicts are same. But I need to compare each key in dict1 and see if that key exists in dict2 (if exists the corresponding value should be matched with dict1 value).

Here is what I have written. But couldn't get the end result.

```<code>for i,j in dict1.items():
    # values of t1,name (i.e. inner key/value pairs of (t1,name)) might interchange
    # order at times that is the reason I used sorted
    for k,v in sorted(j.items()):
        print k           # prints - >('11',22')
        print v           # prints - > '33':'456','77':'891'
        if i in dict2.keys():
           # Here I need to make sure for outer key (t1,name), inner key/value pair of
           # dict2 is same as inner key/value pair of dict1
</code>```

Apologies for this lengthy explanation. I'm not sure whether I am able to explain it clearly.

答:
Not sure I understand what you are looking for but you can use a dict comprehension to construct all the matches:</p>

```<code>>>> {k: v for k, v in dict1.items() if dict2[k] == v}
{('t1', 'name'): {('11', '22'): {'33': '456', '77': '891'},
 ('121', '212'): {'23': '546', '32': '123'}}}
</code>```

Title: How to compare nested dictionaries?

Body:
I have two nested dictionaries.

dict1 = {(t1,name):{('11','22'):{'33':'456','77':'891'},
                    ('121','212'):{'32':'123', '23':'546'}}}
dict2 = {(t1,name):{('11','22'):{'33':'456','77':'891'},
                     ('121','212'):{'32':'123', '23':'546'}}}

Basically both the dicts are same. But I need to compare each key in dict1 and see if that key exists in dict2 (if exists the corresponding value should be matched with dict1 value).

Here is what I have written. But couldn't get the end result.

for i,j in dict1.items():
    # values of t1,name (i.e. inner key/value pairs of (t1,name)) might interchange
    # order at times that is the reason I used sorted
    for k,v in sorted(j.items()):
        print k           # prints - >('11',22')
        print v           # prints - > '33':'456','77':'891'
        if i in dict2.keys():
           # Here I need to make sure for outer key (t1,name), inner key/value pair of
           # dict2 is same as inner key/value pair of dict1

Apologies for this lengthy explanation. I'm not sure whether I am able to explain it clearly.

Body:
Not sure I understand what you are looking for but you can use a dict comprehension to construct all the matches:

>>> {k: v for k, v in dict1.items() if dict2[k] == v}
{('t1', 'name'): {('11', '22'): {'33': '456', '77': '891'},
 ('121', '212'): {'23': '546', '32': '123'}}}


问:
OpenGL ES: Get transformed vertices from shader
I have a performance bottleneck in my OpenGL ES-program by the moment. I thought it would work well - using VBO, textureatlas, few bindings for every draw-call and so on. But when using many sprites at the same time the performance drops alot. I found that the bottleneck is CPU-bound (a bit surprised). More precisely - the bottleneck could be dervied to a method that calculates the screenposition for each  rectangles four vertcies - x1, y1, x2, y2, x3, y3, x4, y4. This is used for collition-detection. What I do in this method is to repeat what is  done in the shaders and i think many cpu-cycles is caused by the MV-multiplications.

```<code> Matrix.multiplyMV(resultVec, 0, mModelMatrix, 0, rhsVec, 0);
</code>```

the rhsVec is a float-array that stores the vertices as described above.

Since this seem to be the bottleneck I wonder how I could access the same vector in the shader when for instance the clip-coordinates are calculated? Clip-coordinates or even better the coordinates produced by he shaders further down in the pipe-line.

the vertex-shader

```<code>uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
varying vec2 v_TexCoordinate;
attribute vec4 position;

void main()
{

   v_TexCoordinate = a_TexCoordinate

   gl_Position = u_MVPMatrix * a_Position;

}
</code>```

snippet of onSurfaceCreated

```<code>     final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
    final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);

    mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
            new String[] {"a_Position",  "a_Color", "a_Normal", "a_TexCoordinate"});

    textureHandle = TextureHelper.loadTexture(context);

    GLES20.glUseProgram(mProgramHandle);

    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
    mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
    //mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color");
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate");

    mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
</code>```

the method that makes the vertex transformation (the bottleneck)

```<code>   private void calcPos(int index) {

    int k = 0;
    for (int i = 0; i < 18; i += 3) {

        rhsVec[0] = vertices[0 + i];
        rhsVec[1] = vertices[1 + i];
        rhsVec[2] = vertices[2 + i];
        rhsVec[3] = 1;

        // *** Step 1 : Getting to eye coordinates ***

        Matrix.multiplyMV(resultVec, 0, mModelMatrix, 0, rhsVec, 0);

        // *** Step 2 : Getting to clip coordinates ***

        float[] rhsVec2 = resultVec;

        Matrix.multiplyMV(resultVec2, 0, mProjectionMatrix, 0, rhsVec2, 0);

        // *** Step 3 : Getting to normalized device coordinates ***

        float inv_w = 1 / resultVec2[3];

        for (int j = 0; j < resultVec2.length - 1; j++) {

            resultVec2[j] = inv_w * resultVec2[j];
        }

        float xPos = (resultVec2[0] * 0.5f + 0.5f) * game_width;

        float yPos = (resultVec2[1] * 0.5f + 0.5f) * game_height;

        float zPos = (1 + resultVec2[2]) * 0.5f;

        SpriteData sD = spriteDataArrayList.get(index);

        switch (k) {

            case 0:
                sD.xPos[0] = xPos;
                sD.yPos[0] = yPos;
                break;

            case 1:
                sD.xPos[2] = xPos;
                sD.yPos[2] = yPos;
                break;

            case 2:
                sD.xPos[3] = xPos;
                sD.yPos[3] = yPos;
                break;

            case 3:
                sD.xPos[1] = xPos;
                sD.yPos[1] = yPos;
                break;
        }
        k++;

        if (i == 3) {
            i += 9;
        }

    }
</code>```

This method is called for each sprite - so for 100 sprites its repeated 100 times. Probably the MV-multiplications hits performance?

答:
To answer the main question, I don't think it's possible to grab the transformed verts from the GPU.

First pass at optimizing the loop.   First off, don't do things over and over inside the loop when they always produce the same result.  Do it outside the loop.  Especially function or property calls.

Next, you can multiply 2 matrices together in such a way that their transforms are applied in order with a single matrix multiplication.  Although it seems like you are untransforming the final result back into screen space.

You are copying data, and then using that data without changing it.  I know that the matrix multiplication is probably expecting 4 floats or a Vec4, but you can write a matrix multiplication that avoids the copy and fills in the w parameter.

Avoid calculations that you ultimately don't use.

Cache results and don't recalculate unless they change.

```<code>private void calcPos(int index) {

// get only once, not every loop
SpriteData sD = spriteDataArrayList.get(index);

int[] vIndices = {0, 1, 2, 5}; // the 4 verts you want

// multiply once outside the loop, use result inside loop
Matrix mvpMatrix = mModelMatrix * mProjectionMatrix; // check order

for (int i = 0; i < 4; ++i) { // only grab verts you want, no need for fancy skips

    int nVert = 3 * vIndices[i]; // 3 floats per vert

    // should avoid copying data when you aren't going to change the copy
    rhsVec[0] = vertices[0 + nVert];
    rhsVec[1] = vertices[1 + nVert];
    rhsVec[2] = vertices[2 + nVert];

    rhsVec[3] = 1; // need to write multiplyMV3 that takes pointer to 3 floats
                   // and fills in the w param, then no need to copy

    // E.g. :
    // Matrix.multiplyMV3(resultVec2, 0, mvpMatrix, 0, &vertices[nVert], 0);

    // do both matrix multiplcations at same time
    Matrix.multiplyMV(resultVec2, 0, mvpMatrix, 0, rhsVec, 0);

    // *** Step 3 : Getting to normalized device coordinates ***
    float inv_w = 1 / resultVec2[3];

    for (int j = 0; j < 2; ++j) // just what we need
        resultVec2[j] *= inv_w;

    // Curious... Transform into projection space, just to transform
    // back into screen space.  Perhaps you are transforming too far?
    float xPos = (resultVec2[0] * 0.5f + 0.5f) * game_width;
    float yPos = (resultVec2[1] * 0.5f + 0.5f) * game_height;
    // float zPos = (1 + resultVec2[2]) * 0.5f; // not used

    switch (i) {

        case 0:
            sD.xPos[0] = xPos;
            sD.yPos[0] = yPos;
            break;

        case 1:
            sD.xPos[2] = xPos;
            sD.yPos[2] = yPos;
            break;

        case 2:
            sD.xPos[3] = xPos;
            sD.yPos[3] = yPos;
            break;

        case 3:
            sD.xPos[1] = xPos;
            sD.yPos[1] = yPos;
            break;
    }
}
</code>```

Body:
I have a performance bottleneck in my OpenGL ES-program by the moment. I thought it would work well - using VBO, textureatlas, few bindings for every draw-call and so on. But when using many sprites at the same time the performance drops alot. I found that the bottleneck is CPU-bound (a bit surprised). More precisely - the bottleneck could be dervied to a method that calculates the screenposition for each  rectangles four vertcies - x1, y1, x2, y2, x3, y3, x4, y4. This is used for collition-detection. What I do in this method is to repeat what is  done in the shaders and i think many cpu-cycles is caused by the MV-multiplications.

 Matrix.multiplyMV(resultVec, 0, mModelMatrix, 0, rhsVec, 0);

the rhsVec is a float-array that stores the vertices as described above.

Since this seem to be the bottleneck I wonder how I could access the same vector in the shader when for instance the clip-coordinates are calculated? Clip-coordinates or even better the coordinates produced by he shaders further down in the pipe-line.

the vertex-shader

uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
varying vec2 v_TexCoordinate;
attribute vec4 position;

void main()
{

   v_TexCoordinate = a_TexCoordinate

   gl_Position = u_MVPMatrix * a_Position;

}

snippet of onSurfaceCreated

     final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
    final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);

    mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
            new String[] {"a_Position",  "a_Color", "a_Normal", "a_TexCoordinate"});

    textureHandle = TextureHelper.loadTexture(context);

    GLES20.glUseProgram(mProgramHandle);

    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
    mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
    //mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color");
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate");

    mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");

the method that makes the vertex transformation (the bottleneck)

   private void calcPos(int index) {

    int k = 0;
    for (int i = 0; i < 18; i += 3) {

        rhsVec[0] = vertices[0 + i];
        rhsVec[1] = vertices[1 + i];
        rhsVec[2] = vertices[2 + i];
        rhsVec[3] = 1;

        // *** Step 1 : Getting to eye coordinates ***

        Matrix.multiplyMV(resultVec, 0, mModelMatrix, 0, rhsVec, 0);

        // *** Step 2 : Getting to clip coordinates ***

        float[] rhsVec2 = resultVec;

        Matrix.multiplyMV(resultVec2, 0, mProjectionMatrix, 0, rhsVec2, 0);

        // *** Step 3 : Getting to normalized device coordinates ***

        float inv_w = 1 / resultVec2[3];

        for (int j = 0; j < resultVec2.length - 1; j++) {

            resultVec2[j] = inv_w * resultVec2[j];
        }

        float xPos = (resultVec2[0] * 0.5f + 0.5f) * game_width;

        float yPos = (resultVec2[1] * 0.5f + 0.5f) * game_height;

        float zPos = (1 + resultVec2[2]) * 0.5f;

        SpriteData sD = spriteDataArrayList.get(index);

        switch (k) {

            case 0:
                sD.xPos[0] = xPos;
                sD.yPos[0] = yPos;
                break;

            case 1:
                sD.xPos[2] = xPos;
                sD.yPos[2] = yPos;
                break;

            case 2:
                sD.xPos[3] = xPos;
                sD.yPos[3] = yPos;
                break;

            case 3:
                sD.xPos[1] = xPos;
                sD.yPos[1] = yPos;
                break;
        }
        k++;

        if (i == 3) {
            i += 9;
        }

    }

This method is called for each sprite - so for 100 sprites its repeated 100 times. Probably the MV-multiplications hits performance?

Body:
To answer the main question, I don't think it's possible to grab the transformed verts from the GPU.

First pass at optimizing the loop.   First off, don't do things over and over inside the loop when they always produce the same result.  Do it outside the loop.  Especially function or property calls.

Next, you can multiply 2 matrices together in such a way that their transforms are applied in order with a single matrix multiplication.  Although it seems like you are untransforming the final result back into screen space.

You are copying data, and then using that data without changing it.  I know that the matrix multiplication is probably expecting 4 floats or a Vec4, but you can write a matrix multiplication that avoids the copy and fills in the w parameter.

Avoid calculations that you ultimately don't use.

Cache results and don't recalculate unless they change.

private void calcPos(int index) {

// get only once, not every loop
SpriteData sD = spriteDataArrayList.get(index);

int[] vIndices = {0, 1, 2, 5}; // the 4 verts you want

// multiply once outside the loop, use result inside loop
Matrix mvpMatrix = mModelMatrix * mProjectionMatrix; // check order

for (int i = 0; i < 4; ++i) { // only grab verts you want, no need for fancy skips

    int nVert = 3 * vIndices[i]; // 3 floats per vert

    // should avoid copying data when you aren't going to change the copy
    rhsVec[0] = vertices[0 + nVert];
    rhsVec[1] = vertices[1 + nVert];
    rhsVec[2] = vertices[2 + nVert];

    rhsVec[3] = 1; // need to write multiplyMV3 that takes pointer to 3 floats
                   // and fills in the w param, then no need to copy

    // E.g. :
    // Matrix.multiplyMV3(resultVec2, 0, mvpMatrix, 0, &vertices[nVert], 0);

    // do both matrix multiplcations at same time
    Matrix.multiplyMV(resultVec2, 0, mvpMatrix, 0, rhsVec, 0);

    // *** Step 3 : Getting to normalized device coordinates ***
    float inv_w = 1 / resultVec2[3];

    for (int j = 0; j < 2; ++j) // just what we need
        resultVec2[j] *= inv_w;

    // Curious... Transform into projection space, just to transform
    // back into screen space.  Perhaps you are transforming too far?
    float xPos = (resultVec2[0] * 0.5f + 0.5f) * game_width;
    float yPos = (resultVec2[1] * 0.5f + 0.5f) * game_height;
    // float zPos = (1 + resultVec2[2]) * 0.5f; // not used

    switch (i) {

        case 0:
            sD.xPos[0] = xPos;
            sD.yPos[0] = yPos;
            break;

        case 1:
            sD.xPos[2] = xPos;
            sD.yPos[2] = yPos;
            break;

        case 2:
            sD.xPos[3] = xPos;
            sD.yPos[3] = yPos;
            break;

        case 3:
            sD.xPos[1] = xPos;
            sD.yPos[1] = yPos;
            break;
    }
}