问:
"image is not a numpy array, neither a scalar" from cv2
This is my code:

```<code>import cv2
cam=cv2.VideoCapture(0,cv2.CAP_DSHOW)
cam.set(3,640)
cam.set(4,480)
detector=cv2.CascadeClassifier('C:\\Users\\Hp\\Downloads\\8f51e58ac0813cb695f3733926c77f52-
07eed8d5486b1abff88d7e34891f1326a9b6a6f5\\8f51e58ac0813cb695f3733926c77f52-
07eed8d5486b1abff88d7e34891f1326a9b6a6f5\\haarcascade_frontalface_default.xml')
face_id=input("Enter a numeric face id: ")
print("taking sample, look at camera......")
count=0
while True:
      ret, img=cam.read('C:\\Users\\Hp\\Desktop\\leo software
      company\\gamedata\\.vscode\\Maya.Ai')
      converted_image=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      faces=detector.detectMultiScale(converted_image, 1.3, 5)
      for (x,y,w,h) in faces:
          cv2.rectangle(img, (x,y),(x+w,y+h),(255,0,0),2)
          count += 1
          cv2.imwrite("samples/face."+str(face_id)+'.'+str(count)+".jpg",
          converted_image[y:y+h,x:x+w])
          cv2.imshow('image',img)
          k=cv2.waitKey(100) & 0xff
          if k == 27:
          break
         elif count>=10:
         break
         print("id made succesfully")
         cam.release()
         cv2.destroyAllWindows
</code>```

I am getting this error:

Traceback (most recent call last):
  File "c:\Users\Hp\Desktop\leo software company\game data\.vscode\Maya.Ai\id.py", line 10, in <module>
    ret, img=cam.read('C:\\Users\\Hp\\Desktop\\leo software company\\game data\\.vscode\\Maya.Ai')
cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'read'
> Overload resolution failed:
>  - image is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'image'
```

答:
It might help to look at the tutorial from the documentation. You can initialize your VideoCapture to read from a connected camera (as you have done) or from a saved file, as it appears that you intend. After creating the VideoCapture object, its read method doesn't require any arguments, it just grabs the next frame.

So you probably want something like:

```<code>cam = cv2.VideoCapture(
      r'C:\Users\Hp\Desktop\leo software company\gamedata\.vscode\Maya.Ai',
      cv2.CAP_DSHOW
)
</code>```

And then:

```<code>ret, img = cam.read()
</code>```

Also: The way you show your strings split across lines would throw a SyntaxError, so your actual code must not look like that. And you can use a raw string literal to avoid needing to double all your backslashes for Windows file paths.

Body:
This is my code:

import cv2
cam=cv2.VideoCapture(0,cv2.CAP_DSHOW)
cam.set(3,640)
cam.set(4,480)
detector=cv2.CascadeClassifier('C:\\Users\\Hp\\Downloads\\8f51e58ac0813cb695f3733926c77f52-
07eed8d5486b1abff88d7e34891f1326a9b6a6f5\\8f51e58ac0813cb695f3733926c77f52-
07eed8d5486b1abff88d7e34891f1326a9b6a6f5\\haarcascade_frontalface_default.xml')
face_id=input("Enter a numeric face id: ")
print("taking sample, look at camera......")
count=0
while True:
      ret, img=cam.read('C:\\Users\\Hp\\Desktop\\leo software
      company\\gamedata\\.vscode\\Maya.Ai')
      converted_image=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      faces=detector.detectMultiScale(converted_image, 1.3, 5)
      for (x,y,w,h) in faces:
          cv2.rectangle(img, (x,y),(x+w,y+h),(255,0,0),2)
          count += 1
          cv2.imwrite("samples/face."+str(face_id)+'.'+str(count)+".jpg",
          converted_image[y:y+h,x:x+w])
          cv2.imshow('image',img)
          k=cv2.waitKey(100) & 0xff
          if k == 27:
          break
         elif count>=10:
         break
         print("id made succesfully")
         cam.release()
         cv2.destroyAllWindows

I am getting this error:

Traceback (most recent call last):
  File "c:\Users\Hp\Desktop\leo software company\game data\.vscode\Maya.Ai\id.py", line 10, in <module>
    ret, img=cam.read('C:\\Users\\Hp\\Desktop\\leo software company\\game data\\.vscode\\Maya.Ai')
cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'read'
> Overload resolution failed:
>  - image is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'image'

Body:
It might help to look at the tutorial from the documentation. You can initialize your VideoCapture to read from a connected camera (as you have done) or from a saved file, as it appears that you intend. After creating the VideoCapture object, its read method doesn't require any arguments, it just grabs the next frame.

So you probably want something like:

cam = cv2.VideoCapture(
      r'C:\Users\Hp\Desktop\leo software company\gamedata\.vscode\Maya.Ai',
      cv2.CAP_DSHOW
)

And then:

ret, img = cam.read()

Also: The way you show your strings split across lines would throw a SyntaxError, so your actual code must not look like that. And you can use a raw string literal to avoid needing to double all your backslashes for Windows file paths.


来源: stackexchange

问:
CSS animation to repeat every nth second
I am animating a svg element currently like following

.r1 {
     transform-box: fill-box;
     transform-origin: 50% 50%;
     animation-name: simpleRotation,xRotation;
     animation-delay: 0s, 2s;
     animation-duration: 2s;
     animation-iteration-count: 1, 1;
     animation-timing-function: linear;
     animation-direction: normal;
     animation-fill-mode: forwards;
}

@keyframes simpleRotation {
     from {
         transform: rotate(0deg);
    }
     to {
         transform: rotate(359deg);

    }
}
@keyframes xRotation {
     from {
         transform: rotateX(0deg);
    }
     to {
         transform: rotateX(359deg);
    }
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<rect class="r1" id="r1" x="10" y="10" width="10" height="10" stroke="black" stroke-width="0.25" fill="orange"></rect>
    </svg></code>```

Is it possible to queue the animations (with javascript) in a way so that every 2s the animation runs one after another in a loop such as simpleRotation(0-2s);xRotation(2-4s);simpleRotation(4-6s);xRotation(6-8s);simpleRotation(8-10s);.....

答:
This is a case where the SVG animation format is easier to use than the CSS keyframe animation - in principle.

SVG animations can define the begin time of an animation in relation to events - among them the end of another animation. This way you can chain animations together, and you can define loops:

```<code><animate id="first" begin="0s;second.end" ... />
<animate id="second" begin="first.end" ... />
</code>```

The first animation begins at 0s and additionally is triggered by the end of the second animation, and the second animation is triggered by the end of the first animation.

The problem with your use case is that you want to animate 3D transform functions. SVG transform differs in syntax from CSS transform, and does only support 2D transformations. So the following example at best can simulate the effect, and a number of details has to be written in a different manner.

<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
  <rect id="r1" x="-5" y="-5" transform="translate(15 15)"
        width="10" height="10" stroke="black" stroke-width="0.25" fill="orange">
    <animateTransform id="simpleRotation" attributeName="transform" type="rotate"
                      additive="sum" begin="0s;xRotation.end" dur="2s"
                      from="0" to="360" />
    <animateTransform id="xRotation" attributeName="transform" type="scale"
                      additive="sum" begin="simpleRotation.end" dur="2s"
                      values="1 1;1 -1;1 1" keyTimes="0;0.5;1"
                      calcMode="spline" keySplines=".375 0 .375 1;.375 0 .375 1" />
  </rect>
</svg>```

the center of the animated object is initially placed at 0,0 to simplify the following transforms. Each <animateTransform> gets a additive="sum" attribute so that it is post-multiplied to the static translate() attribute on the animated object.

to simulate xRotate(), a scale transform is used that scales the y axis from 1 to -1 and back. Additionally, a spline interpolation is defined to give the impression of a smooth "rotation". Note that this simulation only works in absence of a perspective.

ContentLicense: CC BY-SA 4.0

Body:
I am animating a svg element currently like following

.r1 {
     transform-box: fill-box;
     transform-origin: 50% 50%;
     animation-name: simpleRotation,xRotation;
     animation-delay: 0s, 2s;
     animation-duration: 2s;
     animation-iteration-count: 1, 1;
     animation-timing-function: linear;
     animation-direction: normal;
     animation-fill-mode: forwards;
}

@keyframes simpleRotation {
     from {
         transform: rotate(0deg);
    }
     to {
         transform: rotate(359deg);

    }
}
@keyframes xRotation {
     from {
         transform: rotateX(0deg);
    }
     to {
         transform: rotateX(359deg);
    }
}

<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<rect class="r1" id="r1" x="10" y="10" width="10" height="10" stroke="black" stroke-width="0.25" fill="orange"></rect>
    </svg>

Is it possible to queue the animations (with javascript) in a way so that every 2s the animation runs one after another in a loop such as simpleRotation(0-2s);xRotation(2-4s);simpleRotation(4-6s);xRotation(6-8s);simpleRotation(8-10s);.....

Body:
This is a case where the SVG animation format is easier to use than the CSS keyframe animation - in principle.

SVG animations can define the begin time of an animation in relation to events - among them the end of another animation. This way you can chain animations together, and you can define loops:

<animate id="first" begin="0s;second.end" ... />
<animate id="second" begin="first.end" ... />

The first animation begins at 0s and additionally is triggered by the end of the second animation, and the second animation is triggered by the end of the first animation.

The problem with your use case is that you want to animate 3D transform functions. SVG transform differs in syntax from CSS transform, and does only support 2D transformations. So the following example at best can simulate the effect, and a number of details has to be written in a different manner.

<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
  <rect id="r1" x="-5" y="-5" transform="translate(15 15)"
        width="10" height="10" stroke="black" stroke-width="0.25" fill="orange">
    <animateTransform id="simpleRotation" attributeName="transform" type="rotate"
                      additive="sum" begin="0s;xRotation.end" dur="2s"
                      from="0" to="360" />
    <animateTransform id="xRotation" attributeName="transform" type="scale"
                      additive="sum" begin="simpleRotation.end" dur="2s"
                      values="1 1;1 -1;1 1" keyTimes="0;0.5;1"
                      calcMode="spline" keySplines=".375 0 .375 1;.375 0 .375 1" />
  </rect>
</svg>

the center of the animated object is initially placed at 0,0 to simplify the following transforms. Each <animateTransform> gets a additive="sum" attribute so that it is post-multiplied to the static translate() attribute on the animated object.

to simulate xRotate(), a scale transform is used that scales the y axis from 1 to -1 and back. Additionally, a spline interpolation is defined to give the impression of a smooth "rotation". Note that this simulation only works in absence of a perspective.


问:
CSS to mimic silverlight screen shot
I'm pretty sure the question has been asked and answered, I don't know HTML and CSS well enough to know where or how to search.  Any help would be appreciated.

I have a community application built in Silverlight: http://www.scalerailsonline.com/default.aspx

Having finally decided to give Microsoft the chuck I'm rewriting it from scratch.  90% of is pretty easy.  I'm struggling with the HTML and CSS to accomplish the chat portion of the app.

I've marked up a screen shot to show what I'm trying to do.

The main goal is to create a scrollable container that expands and contracts to fit the available space.

First I want to have the web page expand or contract to fit in the browser window.

Second  Right column expands horizontally to fit the content and the bottom area expands vertically to fit the chat entry controls.

Once the page is fit to the browser and the space is committed to the right and bottom, i want the container to fit.  If the content in the container is too much (99% of the time it will be), then vertical scroll bar will display.

I have tried all kinds of combination of width/height: 100%.  But that just seems to make the areas big enough to fit the content, not constrain to a 100% of the browser.

I can set the div to "Height: 200%; Overflow: Scroll" to get the scroll bars.  But I just don't seem to be able to get the div to expand only to the available space.

答: The answer is:  There really isnt a good answer.  Resorting to java script resize event to set the hight of the box based on screen room - heights of screen elements.</p>

ContentLicense: CC BY-SA 3.0

Title: CSS to mimic silverlight screen shot


问:
Get the position of points on a line segment with constraints
I am designing a layout engine for ZenUML. One requirement (after simplification) is this:

There a line segment;

There are n (n < 100, it can be reduced to n < 30 if that makes difference in performance) points on this line segment, the order is fixed; (e.g. P1 ~ Pn)

There are known minimum distances between some points; (e.g. m_d(P2,P4)=500)

The length of the line segment should be as small as possible;

(nice to have) The gaps between neighbour points should be as even as possible (measured by standard deviation^, and must not violate 1~4).

(newly added) it must give result in no longer than 1s for 30 points in worst case (more or less constraints).

^ standard deviation is used as a best alternative that I can precisely describe with my limited math knowledge. However as Roman and David pointed out, there is more appealing result in some cases that does not satisfy lowest standard deviation. See this comment

A sister questions is asked at https://math.stackexchange.com/q/4377433. It is the same problem described with math language (matrix).

For example, for a line segment that has 4 points on it (P1 ~ P4).

m_d(P1, P3) = 200, m_d(P1, P4) = 900.

a. One solution would be P1 = 0, P2 = 0, P3 = 200, P4 = 900.

b. A better solution would be P1 = 0, P2 = 100, P3 = 200, P4 = 900. (put P3 at the middle)

c. A even better solution would be P1 = 0, P2 = 300, P3 = 600, P4 = 900. (evenly distribute P2 and P3).

Some background of this problem. Have a look at the following diagram.

We need to create a layout for all the lifelines (vertical lines). #1, #2

The gap between those line are decided by the length of messages. #3

We need to make sure the total width of the diagram is small (to save space when there are many participants). #4

In this diagram, B should be moved to closer to C. #5

I have implemented the following which satisfies constraints #1~#4. Can some please help with constraint #5? It can be partially met (like b in the example) or fully met (like c in the example). We can use standard deviation to measure the quality of the solution.
Updated: thanks to Roman Svistunov, the base implementation to get non-optimal position is dramatically simplified.

```<code>// Known minimum distance is stored in a matrix
// e.g.
//    m = [
//      [0, 100, 0, 900],
//      [0, 0,   0, 0],
//      [0, 0,   0, 0],
//      [0, 0,   0, 0],
//    ]

let f = function (n: number, m: Array<Array<number>>): number {
  let array = Array(n).fill(0);
  for (let i = 0; i < n; i++) {
    array[i] = f(i, m) + m[i][n];
  }
  return Math.max(0, ...array);
}
</code>```

答:
First, we should find the smallest possible length of the segment. This can be done with the greedy algorithm: place the first point at 0 (without loss of generality) and add the following points one by one with a coordinate as small as possible without violating distances to all previous points. We would fix the last point at its position and would not move it any further. I would suppose that correctness is obvious.

Now the tricky part with optimizing the standard deviation of neighboring distances. We would introduce n-1 variables - the distances, I would name them Di. Trying to minimize their standard deviation is the same as minimizing variance.

And because E(D) is constant as we fixed the first and last points, minimizing standard deviation is the same as minimizing the sum of differences.

Constraints on distances are all linear as whenever you have m_d(Pi,Pj) you would add constraint (sum Dk for k=i to j) >= m_d(Pi,Pj). To guarantee that sum of distances is what we need it to be we also would add contracts sum Dk is both less or equal and greater or equal to the minimum distance calculated in step one. Therefore this problem is a quadratic optimization problem, that can be solved with libraries.

Note that the coordinates for the points then would be easily restored from distances as prefix sums.

Even though for 30 points probably any algorithm of quadratic programming would be fast enough if you want to invest time into further optimization if you intend to increase the number of points, as this problem can actually be solved exactly in polynomial time.

The first and most important optimization. Because the target function is the sum of squares, the matrix of target quadratic form is positive definite (as it is the identity matrix), and therefore there exists a polynomial algorithm, as have been shown in this paper.

The last optimization I would like to mention might not be relevant, but it is very good on most of the possible m_d matrices. To reduce the quadratic programming problem size, we can find optimal positions for some points before quadratic programming.

First, we would make reductions in matrix to ensure that for any i<j<k it is true that m_d(Pi,Pj)+m_d(Pj,Pk)>=m_d(Pi,Pk) with updating the right side. Note that with this computation m_d(P1,Pn) is also the minimum possible length of the segment. After this, we can say that for any i, if m_d(P1,Pi)+m_d(Pi,Pn)=m_d(P1,Pn) is true there is only one possible position for the point Pi, and, this allows us to remove one of the variables as now distance from P(i-1) to Pi and from Pi to P(i+1) can be replaced with one variable - distance from P(i-1) to P(i+1): the first distance is just Pi position - P(i-1) position with can be derived by induction, and the P(i+1) position is P(i-1) position + this new distance.

Edit 2

There are two versions of this pseudocode, both assume you use some kind of quadratic programming library, first one is the intuitive one (and used in a few libraries), the second one is for libraries that use matrices as their input (but less intuitive):

```<code>function findOptimal(Matrix md, int n) {
  int minLength = findMinLength(md, n);
  Constraints constraints; //from library
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (j < i)
        //Here sum of variables is not their current sum
        //but some object for library
        constraints.AddConstranit(sum of variables i to j >= md[j][i])
      else
        constraints.AddContraint(sum of variables j to i >= md[j][i])
    }
  }
  constraints.AddConstraint(sum of variables 0 to n-1 <= minLength)
  variables = QuadraticOptimizationSolve(sum of squares of variables, contracts)
  positions = Array<number>(n);
  positions[0] = 0;
  for (int i = 0; i < n; ++i) {
    positions[i + 1] = positions[i] + variables[i];
  }
  return positions;
}
</code>```

As matrices are more common as inputs in libraries, here is the pseudocode to work with them:

```<code>function findOptimal(Matrix md, int n) {
  int minLength = findMinLength(md, n);
  Matrix optimization = Matrix(n - 1, n - 1);
  for (int i = 0; i < n - 1; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      if (i == j)
        optimization[i][j] = 1
      else
        optimization[i][j] = 0
    }
  }
  Matrix constraints = Matrix(n * (n - 1) / 2 + 1, n)
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      constraintRow = i * (i - 1) / 2 + j;
      //constriant is sum of distances from j to i is
      //at least md[j][i]
      for (int k = j; k < i; ++k) {
        //these are coefficients in linear combination
        //negative because we need >= while optimizers usually use <= contraints
        if (k >= k and k < i)
          constraints[constraintRow][k] = -1
        else
          constraints[constraintRow][k] = 0
      }
      constraints[constraintsRow][n - 1] = -md[i][j];
    }
  }
  variables = QuadraticOptimizationSolve(optimization, constraints);
  positions = Array<number>(n);
  positions[0] = 0;
  for (int i = 0; i < n; ++i) {
    positions[i + 1] = positions[i] + variables[i];
  }
  return positions;
}
Also this solution assumes in your library the constants vector in constraints is the last column, but sometimes it might be separate vector
</code>```

Edit 3

This is the javascript solution for your problem. Easily handles 30 points in a second (actually on my setup it can process up to 100 points in a second). It uses a different implementation of findMinLength function than yours (yours does not save the result of recursive calls and is exponential because of that). Also, I was unable to find a good library for quadratic programming, so I ended up using this solution (was edited in edit 4). This is for node js, but combining all the files (and removing requires and exports, for vsmall you need to define vsmall as epsilon in their file), and this library seems to work. It appears that the previous library was just a browser adaptation of this one, and this one had an issue that was fixed years after the adaptation was made, but it is quite easy to do it manually (as it has no dependencies).

let findMinLength = function (n, m) {
  let answer = [];
  answer[0] = 0;
  for (let i = 1; i < n; i++) {
    answer[i] = 0;
    for (let j = 0; j < i; j++) {
      answer[i] = Math.max(answer[i], answer[j] + m[i][j], answer[j] + m[j][i]);
    }
  }
  return answer[n - 1];
}
let findOptimal = function(n, m) {
  let minLength = findMinLength(n, m);
  let dmat = [];
  let dvec = [];
  for (let i = 0; i < n - 1; i++) {
    dmat[i + 1] = []
    dvec[i + 1] = 0
    for (let j = 0; j < n - 1; j++) {
      if (i == j)
        dmat[i + 1][j + 1] = 1;
      else
        dmat[i + 1][j + 1] = 0;
    }
  }
  let amat = [];
  for (let i = 0; i < n - 1; i++) {
    amat[i + 1] = [];
    amat[i + 1][1] = -1;
  }
  let bvec = [];
  bvec[1] = -1;
  for (let i = 1; i < n; i++) {
    for (let j = 0; j < i; j++) {
      let constraintRow = i * (i - 1) / 2 + j;
      for (let k = 0; k < n - 1; k++) {
        amat[k + 1][constraintRow + 2] = (k >= j && k < i) ? 1 : 0;
      }
      bvec[constraintRow + 2]=Math.max(m[i][j], m[j][i]) / minLength;
    }
  }
  let variables = solveQP(dmat,dvec,amat,bvec).solution;
  let positions = [];
  positions[0] = 0;
  for (let i = 0; i < n - 1; i++) {
    positions[i + 1] = positions[i] + variables[i + 1] * minLength;
  }
  return positions;
}
```
<p>For example, calling <code>findOptimal(4, m)</code> where m is as in your example (with 4 points), the function returns <code>[0,300,600,900]</code></p>
<p><strong>Edit</strong></p>
<p>As noted in @david-eisenstat there exists an interpretation of the 5th point as maximizing minimum gap, but there is a simpler and asymptotically faster solution, namely, answer binary search.</p>
<p>We know that the minimum gap is at least 0 and at most the segment length / n. We can maximize it with binary search, with a checker ensuring that every gap is at least our value. If <code>findMinLength</code> is a function that solves 1-4, the following is the solution that optimizes this metric (pseudocode):</p>
```function findOptimal(Matrix md, int n) {
  minLength = findMinLength(md, n) //The answer to minimize segment length
  minGapL = 0 //Lower bound in maximized minimum gap length
  minGapR = ceiling(minLength / n) //Upper bound in maximized minimum gap
  while minGapL < minGapR {
    minGapTest = (minGapL + minGapR) / 2
    mdCopy = md.copy()
    //This loop ensures that every gap is at least minimum gap tested
    for (int i = 1; i < n; ++i) {
      mdCopy[i - 1][i] = max(mdCopy[i - 1][i], miGapTest)
    }
    int testLength = findMinLength(mdCopy, n)
    if (testLength == minLength) {
      //if it is possible to get tested minimum gap update lower bound
      minGapL = minGapTest
    } else {
      //otherwise update lower bound
      minGapR = minGapTest - 1;
    }
  }
  return (minLength, minGapL);
}
```

With this function returning both length and maximized minGap one can reconstruct the answer with the matrix modification as in the loop and then solve just problem for 1-4. This solution is polynomial (the 1-4 problem can be solved in linear time as shown above, and binary search is logarithmic of the answer, and with the answer being exponential of the input size it is also linear of the input size).

Body:
I am designing a layout engine for ZenUML. One requirement (after simplification) is this:

There a line segment;

There are n (n < 100, it can be reduced to n < 30 if that makes difference in performance) points on this line segment, the order is fixed; (e.g. P1 ~ Pn)

There are known minimum distances between some points; (e.g. m_d(P2,P4)=500)

The length of the line segment should be as small as possible;

(nice to have) The gaps between neighbour points should be as even as possible (measured by standard deviation^, and must not violate 1~4).

(newly added) it must give result in no longer than 1s for 30 points in worst case (more or less constraints).

^ standard deviation is used as a best alternative that I can precisely describe with my limited math knowledge. However as Roman and David pointed out, there is more appealing result in some cases that does not satisfy lowest standard deviation. See this comment

A sister questions is asked at https://math.stackexchange.com/q/4377433. It is the same problem described with math language (matrix).

For example, for a line segment that has 4 points on it (P1 ~ P4).

m_d(P1, P3) = 200, m_d(P1, P4) = 900.

a. One solution would be P1 = 0, P2 = 0, P3 = 200, P4 = 900.

b. A better solution would be P1 = 0, P2 = 100, P3 = 200, P4 = 900. (put P3 at the middle)

c. A even better solution would be P1 = 0, P2 = 300, P3 = 600, P4 = 900. (evenly distribute P2 and P3).

Some background of this problem. Have a look at the following diagram.

We need to create a layout for all the lifelines (vertical lines). #1, #2

The gap between those line are decided by the length of messages. #3

We need to make sure the total width of the diagram is small (to save space when there are many participants). #4

In this diagram, B should be moved to closer to C. #5

I have implemented the following which satisfies constraints #1~#4. Can some please help with constraint #5? It can be partially met (like b in the example) or fully met (like c in the example). We can use standard deviation to measure the quality of the solution.
Updated: thanks to Roman Svistunov, the base implementation to get non-optimal position is dramatically simplified.

// Known minimum distance is stored in a matrix
// e.g.
//    m = [
//      [0, 100, 0, 900],
//      [0, 0,   0, 0],
//      [0, 0,   0, 0],
//      [0, 0,   0, 0],
//    ]

let f = function (n: number, m: Array<Array<number>>): number {
  let array = Array(n).fill(0);
  for (let i = 0; i < n; i++) {
    array[i] = f(i, m) + m[i][n];
  }
  return Math.max(0, ...array);
}

Body:
First, we should find the smallest possible length of the segment. This can be done with the greedy algorithm: place the first point at 0 (without loss of generality) and add the following points one by one with a coordinate as small as possible without violating distances to all previous points. We would fix the last point at its position and would not move it any further. I would suppose that correctness is obvious.

Now the tricky part with optimizing the standard deviation of neighboring distances. We would introduce n-1 variables - the distances, I would name them Di. Trying to minimize their standard deviation is the same as minimizing variance.

And because E(D) is constant as we fixed the first and last points, minimizing standard deviation is the same as minimizing the sum of differences.

Constraints on distances are all linear as whenever you have m_d(Pi,Pj) you would add constraint (sum Dk for k=i to j) >= m_d(Pi,Pj). To guarantee that sum of distances is what we need it to be we also would add contracts sum Dk is both less or equal and greater or equal to the minimum distance calculated in step one. Therefore this problem is a quadratic optimization problem, that can be solved with libraries.

Note that the coordinates for the points then would be easily restored from distances as prefix sums.

Even though for 30 points probably any algorithm of quadratic programming would be fast enough if you want to invest time into further optimization if you intend to increase the number of points, as this problem can actually be solved exactly in polynomial time.

The first and most important optimization. Because the target function is the sum of squares, the matrix of target quadratic form is positive definite (as it is the identity matrix), and therefore there exists a polynomial algorithm, as have been shown in this paper.

The last optimization I would like to mention might not be relevant, but it is very good on most of the possible m_d matrices. To reduce the quadratic programming problem size, we can find optimal positions for some points before quadratic programming.

First, we would make reductions in matrix to ensure that for any i<j<k it is true that m_d(Pi,Pj)+m_d(Pj,Pk)>=m_d(Pi,Pk) with updating the right side. Note that with this computation m_d(P1,Pn) is also the minimum possible length of the segment. After this, we can say that for any i, if m_d(P1,Pi)+m_d(Pi,Pn)=m_d(P1,Pn) is true there is only one possible position for the point Pi, and, this allows us to remove one of the variables as now distance from P(i-1) to Pi and from Pi to P(i+1) can be replaced with one variable - distance from P(i-1) to P(i+1): the first distance is just Pi position - P(i-1) position with can be derived by induction, and the P(i+1) position is P(i-1) position + this new distance.

Edit 2

There are two versions of this pseudocode, both assume you use some kind of quadratic programming library, first one is the intuitive one (and used in a few libraries), the second one is for libraries that use matrices as their input (but less intuitive):

function findOptimal(Matrix md, int n) {
  int minLength = findMinLength(md, n);
  Constraints constraints; //from library
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (j < i)
        //Here sum of variables is not their current sum
        //but some object for library
        constraints.AddConstranit(sum of variables i to j >= md[j][i])
      else
        constraints.AddContraint(sum of variables j to i >= md[j][i])
    }
  }
  constraints.AddConstraint(sum of variables 0 to n-1 <= minLength)
  variables = QuadraticOptimizationSolve(sum of squares of variables, contracts)
  positions = Array<number>(n);
  positions[0] = 0;
  for (int i = 0; i < n; ++i) {
    positions[i + 1] = positions[i] + variables[i];
  }
  return positions;
}

As matrices are more common as inputs in libraries, here is the pseudocode to work with them:

function findOptimal(Matrix md, int n) {
  int minLength = findMinLength(md, n);
  Matrix optimization = Matrix(n - 1, n - 1);
  for (int i = 0; i < n - 1; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      if (i == j)
        optimization[i][j] = 1
      else
        optimization[i][j] = 0
    }
  }
  Matrix constraints = Matrix(n * (n - 1) / 2 + 1, n)
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      constraintRow = i * (i - 1) / 2 + j;
      //constriant is sum of distances from j to i is
      //at least md[j][i]
      for (int k = j; k < i; ++k) {
        //these are coefficients in linear combination
        //negative because we need >= while optimizers usually use <= contraints
        if (k >= k and k < i)
          constraints[constraintRow][k] = -1
        else
          constraints[constraintRow][k] = 0
      }
      constraints[constraintsRow][n - 1] = -md[i][j];
    }
  }
  variables = QuadraticOptimizationSolve(optimization, constraints);
  positions = Array<number>(n);
  positions[0] = 0;
  for (int i = 0; i < n; ++i) {
    positions[i + 1] = positions[i] + variables[i];
  }
  return positions;
}
Also this solution assumes in your library the constants vector in constraints is the last column, but sometimes it might be separate vector

Edit 3

This is the javascript solution for your problem. Easily handles 30 points in a second (actually on my setup it can process up to 100 points in a second). It uses a different implementation of findMinLength function than yours (yours does not save the result of recursive calls and is exponential because of that). Also, I was unable to find a good library for quadratic programming, so I ended up using this solution (was edited in edit 4). This is for node js, but combining all the files (and removing requires and exports, for vsmall you need to define vsmall as epsilon in their file), and this library seems to work. It appears that the previous library was just a browser adaptation of this one, and this one had an issue that was fixed years after the adaptation was made, but it is quite easy to do it manually (as it has no dependencies).

let findMinLength = function (n, m) {
  let answer = [];
  answer[0] = 0;
  for (let i = 1; i < n; i++) {
    answer[i] = 0;
    for (let j = 0; j < i; j++) {
      answer[i] = Math.max(answer[i], answer[j] + m[i][j], answer[j] + m[j][i]);
    }
  }
  return answer[n - 1];
}
let findOptimal = function(n, m) {
  let minLength = findMinLength(n, m);
  let dmat = [];
  let dvec = [];
  for (let i = 0; i < n - 1; i++) {
    dmat[i + 1] = []
    dvec[i + 1] = 0
    for (let j = 0; j < n - 1; j++) {
      if (i == j)
        dmat[i + 1][j + 1] = 1;
      else
        dmat[i + 1][j + 1] = 0;
    }
  }
  let amat = [];
  for (let i = 0; i < n - 1; i++) {
    amat[i + 1] = [];
    amat[i + 1][1] = -1;
  }
  let bvec = [];
  bvec[1] = -1;
  for (let i = 1; i < n; i++) {
    for (let j = 0; j < i; j++) {
      let constraintRow = i * (i - 1) / 2 + j;
      for (let k = 0; k < n - 1; k++) {
        amat[k + 1][constraintRow + 2] = (k >= j && k < i) ? 1 : 0;
      }
      bvec[constraintRow + 2]=Math.max(m[i][j], m[j][i]) / minLength;
    }
  }
  let variables = solveQP(dmat,dvec,amat,bvec).solution;
  let positions = [];
  positions[0] = 0;
  for (let i = 0; i < n - 1; i++) {
    positions[i + 1] = positions[i] + variables[i + 1] * minLength;
  }
  return positions;
}

For example, calling findOptimal(4, m) where m is as in your example (with 4 points), the function returns [0,300,600,900]

Edit

As noted in @david-eisenstat there exists an interpretation of the 5th point as maximizing minimum gap, but there is a simpler and asymptotically faster solution, namely, answer binary search.

We know that the minimum gap is at least 0 and at most the segment length / n. We can maximize it with binary search, with a checker ensuring that every gap is at least our value. If findMinLength is a function that solves 1-4, the following is the solution that optimizes this metric (pseudocode):

function findOptimal(Matrix md, int n) {
  minLength = findMinLength(md, n) //The answer to minimize segment length
  minGapL = 0 //Lower bound in maximized minimum gap length
  minGapR = ceiling(minLength / n) //Upper bound in maximized minimum gap
  while minGapL < minGapR {
    minGapTest = (minGapL + minGapR) / 2
    mdCopy = md.copy()
    //This loop ensures that every gap is at least minimum gap tested
    for (int i = 1; i < n; ++i) {
      mdCopy[i - 1][i] = max(mdCopy[i - 1][i], miGapTest)
    }
    int testLength = findMinLength(mdCopy, n)
    if (testLength == minLength) {
      //if it is possible to get tested minimum gap update lower bound
      minGapL = minGapTest
    } else {
      //otherwise update lower bound
      minGapR = minGapTest - 1;
    }
  }
  return (minLength, minGapL);
}

With this function returning both length and maximized minGap one can reconstruct the answer with the matrix modification as in the loop and then solve just problem for 1-4. This solution is polynomial (the 1-4 problem can be solved in linear time as shown above, and binary search is logarithmic of the answer, and with the answer being exponential of the input size it is also linear of the input size).


问:
Cannot scrape input from dynamic dropdown menu due to  (Python - Selenium)
this is a somewhat discussed problem, however I haven't find a solution in other posts.

I am creating an automation to submit parcels, using Selenium on Python, for this website:
https://www.mondospedizioni.com

To select Country and City, I need to add an input on a text field.
The text field is actually a search field, that opens a dropdown menu upon adding the key.

I fail to select the option and close the dropdown menu, thus filling the "Country"/"City" field and moving fwd.

The div I refer to (Country):

```<code><span class="select2-selection__rendered" id="select2-nazione_mittente-container" title="Selezionare Nazione di ritiro">Selezionare Nazione di ritiro</span>
</code>```

The one for City:

```<code><span class="select2-selection__placeholder">Inserire il CAP e selezionare la località</span>
</code>```

This Post uses Select, but because of the  tags I receive error:

```<code>Select only works on <select> elements, not on <span>.
</code>```

I found another suggestion in this post from Anzel and even this one did not work for me.

My code gets stuck when I try to enter the sender's city, and the error is given to me when the code calls the input field for the receiver's country. My code:

```<code>import pandas as pd
import numpy as np
import time
from datetime import datetime, date

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

import itertools

driver = webdriver.Chrome(executable_path...)
url = "https://www.mondospedizioni.com/"
driver.get(url)
driver.set_page_load_timeout(-1) #Makes sure the webpage is fully loaded before scraping. Thus avoiding errors.

#All Page Components
hoCapito =driver.find_element(By.XPATH, '//*[@id="cookie-bar"]/p/a[1]')

countryFrom = driver.find_element(By.XPATH, '//*[@id="select2-nazione_mittente-container"]')
cityFrom = driver.find_element(By.XPATH, '//*[@id="loc_mittente"]')
postFrom = driver.find_element(By.XPATH, '//*[@id="cap_mittente"]')

countryTo = driver.find_element(By.XPATH, '//*[@id="select2-nazione_destinatario-container"]')
cityTo = driver.find_element(By.XPATH, '//*[@id="loc_destinatario"]')
postTo = driver.find_element(By.XPATH, '//*[@id="cap_destinatario"]')

myWeight = driver.find_element(By.XPATH, '//*[@id="peso1"]')
myLength = driver.find_element(By.XPATH, '//*[@id="l1"]')
myWidth = driver.find_element(By.XPATH, '//*[@id="p1"]')
myHeight = driver.find_element(By.XPATH, '//*[@id="h1"]')

calculate = driver.find_element(By.XPATH, '//*[@id="btn-calcola"]')

#MyKeys
driver = webdriver.Chrome(executable_path=r"C:\Users\AndreaPaviglianiti\Jupyter-2022\webdrivers\chromedriver.exe")
url = "https://www.mondospedizioni.com/"
driver.get(url)
driver.set_page_load_timeout(-1) #Makes sure the webpage is fully loaded before scraping. Thus avoiding errors.

hoCapito =driver.find_element(By.XPATH, '//*[@id="cookie-bar"]/p/a[1]')

countryFrom = driver.find_element(By.XPATH, '//*[@id="select2-nazione_mittente-container"]')
cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-container"]/span')
postFrom = driver.find_element(By.XPATH, '//*[@id="cap_mittente"]')

countryTo = driver.find_element(By.XPATH, '//*[@id="select2-nazione_destinatario-container"]')
cityTo = driver.find_element(By.XPATH, '//*[@id="loc_destinatario"]')
postTo = driver.find_element(By.XPATH, '//*[@id="cap_destinatario"]')

#Accept Cookies
hoCapito.click()

countryFrom.click()
select_countryFrom = driver.find_element(By.CLASS_NAME, 'select2-search__field').send_keys('Italia')
confirm_countryFrom = driver.find_element(By.XPATH, '//*[@id="select2-nazione_mittente-results"]').click()

cityFrom.click()
select_cityFrom = driver.find_element(By.CLASS_NAME, 'select2-search__field').send_keys('Partinico')
confirm_cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-results"]/li/div').click()

countryTo.click()
select_countryTo = driver.find_element(By.CLASS_NAME, 'select2-search_field').send_keys('Repubblica Slovacca')
confirm_countryTo = driver.find_element(By.XPATH, '//*[@id="select2-nazione_destinatario-result-885w-Repubblica Slovacca"]').click()

cityTo.send_keys('Bratislava')
postFrom.send_keys(82101)

myWeight.send_keys(30)
myLength.send_keys(47)
myWidth.send_keys(25)
myHeight.send_keys(38)

calculate.click()
time.sleep(3)
driver.set_page_load_timeout(-1)
</code>```

My error (now - based on my latest attempt):

```<code><ipython-input-77-dd29b60fadf7>:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path=r"C:\Users\AndreaPaviglianiti\Jupyter-2022\webdrivers\chromedriver.exe")

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-77-dd29b60fadf7> in <module>
     25 cityFrom.click()
     26 select_cityFrom = driver.find_element(By.CLASS_NAME, 'select2-search__field').send_keys('Partinico')
---> 27 confirm_cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-results"]/li/div').click()
     28 #confirm_cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-results"]/li/div/text()').click()
     29 #confirm_cityFrom = driver.find_element(By.XPATH, '/html/body/span/span/span[2]').click()

~\Miniconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
   1242             value = '[name="%s"]' % value
   1243
-> 1244         return self.execute(Command.FIND_ELEMENT, {
   1245             'using': by,
   1246             'value': value})['value']

~\Miniconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    422         response = self.command_executor.execute(driver_command, params)
    423         if response:
--> 424             self.error_handler.check_response(response)
    425             response['value'] = self._unwrap_value(
    426                 response.get('value', None))

~\Miniconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    245                 alert_text = value['alert'].get('text')
    246             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 247         raise exception_class(message, screen, stacktrace)
    248
    249     def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="select2-loc_cap_mittente-results"]/li/div"}
  (Session info: chrome=98.0.4758.81)
Stacktrace:
Backtrace:
    Ordinal0 [0x007F7AC3+2587331]
    Ordinal0 [0x0078ADD1+2141649]
    Ordinal0 [0x00683BB8+1063864]
    Ordinal0 [0x006B01CE+1245646]
    Ordinal0 [0x006B03CB+1246155]
    Ordinal0 [0x006DA612+1418770]
    Ordinal0 [0x006C86D4+1345236]
    Ordinal0 [0x006D8A0A+1411594]
    Ordinal0 [0x006C84A6+1344678]
    Ordinal0 [0x006A53F6+1201142]
    Ordinal0 [0x006A62E6+1204966]
    GetHandleVerifier [0x0099DF22+1680738]
    GetHandleVerifier [0x00A50DBC+2413564]
    GetHandleVerifier [0x0088D151+563089]
    GetHandleVerifier [0x0088BF13+558419]
    Ordinal0 [0x0079081E+2164766]
    Ordinal0 [0x00795508+2184456]
    Ordinal0 [0x00795650+2184784]
    Ordinal0 [0x0079F5BC+2225596]
    BaseThreadInitThunk [0x77CDFA29+25]
    RtlGetAppContainerNamedObjectPath [0x77E27A9E+286]
    RtlGetAppContainerNamedObjectPath [0x77E27A6E+238]
</code>```

EDIT
To clarify the matter I am now adding two printscreens that focus on the elements I cannot manage - the Sender's Locality.

It is more advanced to what I am used to (and I am fairly new with Selenium).

答:
It turns out that it is actually  that I need to target and that works only if I get to have only one option in the dropdown menu.

I came up with this solution and it works, for I was able to finish to automate the form:

```<code>#Sender's City
cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-container"]').click()
select_cityFrom = driver.find_element(By.XPATH, '/html/body/span/span/span[1]/input').send_keys('Partanna')
time.sleep(1)
confirm_cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-results"]/li/div').click()
</code>```

Title: Cannot scrape input from dynamic dropdown menu due to <span> (Python - Selenium)

Body:
this is a somewhat discussed problem, however I haven't find a solution in other posts.

I am creating an automation to submit parcels, using Selenium on Python, for this website:
https://www.mondospedizioni.com

To select Country and City, I need to add an input on a text field.
The text field is actually a search field, that opens a dropdown menu upon adding the key.

I fail to select the option and close the dropdown menu, thus filling the "Country"/"City" field and moving fwd.

The div I refer to (Country):

Selezionare Nazione di ritiro

The one for City:

Inserire il CAP e selezionare la località

This Post uses Select, but because of the  tags I receive error:

Select only works on <select> elements, not on .

I found another suggestion in this post from Anzel and even this one did not work for me.

My code gets stuck when I try to enter the sender's city, and the error is given to me when the code calls the input field for the receiver's country. My code:

import pandas as pd
import numpy as np
import time
from datetime import datetime, date

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

import itertools

driver = webdriver.Chrome(executable_path...)
url = "https://www.mondospedizioni.com/"
driver.get(url)
driver.set_page_load_timeout(-1) #Makes sure the webpage is fully loaded before scraping. Thus avoiding errors.

#All Page Components
hoCapito =driver.find_element(By.XPATH, '//*[@id="cookie-bar"]/p/a[1]')

countryFrom = driver.find_element(By.XPATH, '//*[@id="select2-nazione_mittente-container"]')
cityFrom = driver.find_element(By.XPATH, '//*[@id="loc_mittente"]')
postFrom = driver.find_element(By.XPATH, '//*[@id="cap_mittente"]')

countryTo = driver.find_element(By.XPATH, '//*[@id="select2-nazione_destinatario-container"]')
cityTo = driver.find_element(By.XPATH, '//*[@id="loc_destinatario"]')
postTo = driver.find_element(By.XPATH, '//*[@id="cap_destinatario"]')

myWeight = driver.find_element(By.XPATH, '//*[@id="peso1"]')
myLength = driver.find_element(By.XPATH, '//*[@id="l1"]')
myWidth = driver.find_element(By.XPATH, '//*[@id="p1"]')
myHeight = driver.find_element(By.XPATH, '//*[@id="h1"]')

calculate = driver.find_element(By.XPATH, '//*[@id="btn-calcola"]')

#MyKeys
driver = webdriver.Chrome(executable_path=r"C:\Users\AndreaPaviglianiti\Jupyter-2022\webdrivers\chromedriver.exe")
url = "https://www.mondospedizioni.com/"
driver.get(url)
driver.set_page_load_timeout(-1) #Makes sure the webpage is fully loaded before scraping. Thus avoiding errors.

hoCapito =driver.find_element(By.XPATH, '//*[@id="cookie-bar"]/p/a[1]')

countryFrom = driver.find_element(By.XPATH, '//*[@id="select2-nazione_mittente-container"]')
cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-container"]/span')
postFrom = driver.find_element(By.XPATH, '//*[@id="cap_mittente"]')

countryTo = driver.find_element(By.XPATH, '//*[@id="select2-nazione_destinatario-container"]')
cityTo = driver.find_element(By.XPATH, '//*[@id="loc_destinatario"]')
postTo = driver.find_element(By.XPATH, '//*[@id="cap_destinatario"]')

#Accept Cookies
hoCapito.click()

countryFrom.click()
select_countryFrom = driver.find_element(By.CLASS_NAME, 'select2-search__field').send_keys('Italia')
confirm_countryFrom = driver.find_element(By.XPATH, '//*[@id="select2-nazione_mittente-results"]').click()

cityFrom.click()
select_cityFrom = driver.find_element(By.CLASS_NAME, 'select2-search__field').send_keys('Partinico')
confirm_cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-results"]/li/div').click()

countryTo.click()
select_countryTo = driver.find_element(By.CLASS_NAME, 'select2-search_field').send_keys('Repubblica Slovacca')
confirm_countryTo = driver.find_element(By.XPATH, '//*[@id="select2-nazione_destinatario-result-885w-Repubblica Slovacca"]').click()

cityTo.send_keys('Bratislava')
postFrom.send_keys(82101)

myWeight.send_keys(30)
myLength.send_keys(47)
myWidth.send_keys(25)
myHeight.send_keys(38)

calculate.click()
time.sleep(3)
driver.set_page_load_timeout(-1)

My error (now - based on my latest attempt):

<ipython-input-77-dd29b60fadf7>:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path=r"C:\Users\AndreaPaviglianiti\Jupyter-2022\webdrivers\chromedriver.exe")

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-77-dd29b60fadf7> in <module>
     25 cityFrom.click()
     26 select_cityFrom = driver.find_element(By.CLASS_NAME, 'select2-search__field').send_keys('Partinico')
---> 27 confirm_cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-results"]/li/div').click()
     28 #confirm_cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-results"]/li/div/text()').click()
     29 #confirm_cityFrom = driver.find_element(By.XPATH, '/html/body/span/span/span[2]').click()

~\Miniconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
   1242             value = '[name="%s"]' % value
   1243
-> 1244         return self.execute(Command.FIND_ELEMENT, {
   1245             'using': by,
   1246             'value': value})['value']

~\Miniconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    422         response = self.command_executor.execute(driver_command, params)
    423         if response:
--> 424             self.error_handler.check_response(response)
    425             response['value'] = self._unwrap_value(
    426                 response.get('value', None))

~\Miniconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    245                 alert_text = value['alert'].get('text')
    246             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 247         raise exception_class(message, screen, stacktrace)
    248
    249     def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="select2-loc_cap_mittente-results"]/li/div"}
  (Session info: chrome=98.0.4758.81)
Stacktrace:
Backtrace:
    Ordinal0 [0x007F7AC3+2587331]
    Ordinal0 [0x0078ADD1+2141649]
    Ordinal0 [0x00683BB8+1063864]
    Ordinal0 [0x006B01CE+1245646]
    Ordinal0 [0x006B03CB+1246155]
    Ordinal0 [0x006DA612+1418770]
    Ordinal0 [0x006C86D4+1345236]
    Ordinal0 [0x006D8A0A+1411594]
    Ordinal0 [0x006C84A6+1344678]
    Ordinal0 [0x006A53F6+1201142]
    Ordinal0 [0x006A62E6+1204966]
    GetHandleVerifier [0x0099DF22+1680738]
    GetHandleVerifier [0x00A50DBC+2413564]
    GetHandleVerifier [0x0088D151+563089]
    GetHandleVerifier [0x0088BF13+558419]
    Ordinal0 [0x0079081E+2164766]
    Ordinal0 [0x00795508+2184456]
    Ordinal0 [0x00795650+2184784]
    Ordinal0 [0x0079F5BC+2225596]
    BaseThreadInitThunk [0x77CDFA29+25]
    RtlGetAppContainerNamedObjectPath [0x77E27A9E+286]
    RtlGetAppContainerNamedObjectPath [0x77E27A6E+238]

EDIT
To clarify the matter I am now adding two printscreens that focus on the elements I cannot manage - the Sender's Locality.

It is more advanced to what I am used to (and I am fairly new with Selenium).

Body:
It turns out that it is actually  that I need to target and that works only if I get to have only one option in the dropdown menu.

I came up with this solution and it works, for I was able to finish to automate the form:

#Sender's City
cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-container"]').click()
select_cityFrom = driver.find_element(By.XPATH, '/html/body/span/span/span[1]/input').send_keys('Partanna')
time.sleep(1)
confirm_cityFrom = driver.find_element(By.XPATH, '//*[@id="select2-loc_cap_mittente-results"]/li/div').click()


问:
Oracle - Where clause parameter change query speed
Using a query with a table and a View, however if i change the where column the speed drastically changes.
What can be the problem?
its quite hard to google "performance changes on where clause" since all exemples are not even close to this.

```<code>SELECT ADRE.*
FROM INVOICE N
INNER JOIN ENTRY_INVOICE NAF
     ON NAF.COMPANY = N.COMPANY
     AND NAF.ID_INVOICE = N.ID_INVOICE
INNER JOIN UV_ENTRY_ALL ADRE
    ON ADRE.COMPANY = NAF.COMPANY
    AND ADRE.ID_ENTRY = NAF.ID_ENTRY
WHERE
    NAF.COMPANY = 1
    /*
    AND NAF.ID_INVOICE = 113806
        => 40 SECONDS

    AND  NAF.ID_ENTRY = 387473
        => 0,6 SECONDS

    AND EXISTS (SELECT 1 FROM ENTRY_INVOICE WHERE COMPANY = 1 AND ID_INVOICE=113806)
        => 1,6 SECONDS
    */
</code>```

ENTRY_INVOICE
IT HAS AT MOST 2 RECORDS FOR EACH INVOICE.

COMPANY
ID_INVOICE
ID_ENTRY

1
113706
387224

1
113706
387225

1
113707
387226

1
113806
387473

PLAN EXPLANATION
https://exists-stack.tiiny.site/

As questioned, even if remove the table INVOICE, and select especific columns, it wont change the speed.

答:
I would rewrite the query based on the primary table in question you are trying to get your data from based on the criteria.  Make sure to have an index that will best help in those situations. THEN Join to the other tables.  Also, if dealing with the condition of a specific invoice, I would just add that to the JOIN clause as well.  Hopefully the engine will get best match for criteria and run with that. In this case, your Entry Invoice table would have TWO indexes... one based on the company + invoice, the other based on company + id entry.

As for your invoice table, that is not even being used within your output and would otherwise just be like that of a lookup table, so I added that as a second join

```<code>Table           Index
Entry_Invoice   ( Company, ID_Invoice )
Entry_Invoice   ( Company, ID_Entry )
Invoice         ( Company, ID_Invoice )
UV_Entry_All    ( Company, ID_Entry )

SELECT
        ADRE.*
    FROM
        ENTRY_INVOICE NAF
            JOIN UV_ENTRY_ALL ADRE
                ON NAF.COMPANY = ADRE.COMPANY
                AND NAF.ID_ENTRY = ADRE.ID_ENTRY
            -- you dont actually NEED the invoice table here
            INVOICE N
                ON NAF.COMPANY = N.COMPANY
                AND NAF.ID_INVOICE = N.ID_INVOICE
    WHERE
            NAF.COMPANY = 1
        AND
        /*
            NAF.ID_INVOICE = 113806
                 vs
            NAF.ID_ENTRY = 387473

            -- dont think this would even be necessary any more
            EXISTS (SELECT 1 FROM ENTRY_INVOICE WHERE COMPANY = 1 AND ID_INVOICE=113806)
        */
</code>```

Body:
Using a query with a table and a View, however if i change the where column the speed drastically changes.
What can be the problem?
its quite hard to google "performance changes on where clause" since all exemples are not even close to this.

SELECT ADRE.*
FROM INVOICE N
INNER JOIN ENTRY_INVOICE NAF
     ON NAF.COMPANY = N.COMPANY
     AND NAF.ID_INVOICE = N.ID_INVOICE
INNER JOIN UV_ENTRY_ALL ADRE
    ON ADRE.COMPANY = NAF.COMPANY
    AND ADRE.ID_ENTRY = NAF.ID_ENTRY
WHERE
    NAF.COMPANY = 1
    /*
    AND NAF.ID_INVOICE = 113806
        => 40 SECONDS

    AND  NAF.ID_ENTRY = 387473
        => 0,6 SECONDS

    AND EXISTS (SELECT 1 FROM ENTRY_INVOICE WHERE COMPANY = 1 AND ID_INVOICE=113806)
        => 1,6 SECONDS
    */

ENTRY_INVOICE
IT HAS AT MOST 2 RECORDS FOR EACH INVOICE.

COMPANY
ID_INVOICE
ID_ENTRY

1
113706
387224

1
113706
387225

1
113707
387226

1
113806
387473

PLAN EXPLANATION
https://exists-stack.tiiny.site/

As questioned, even if remove the table INVOICE, and select especific columns, it wont change the speed.

Body:
I would rewrite the query based on the primary table in question you are trying to get your data from based on the criteria.  Make sure to have an index that will best help in those situations. THEN Join to the other tables.  Also, if dealing with the condition of a specific invoice, I would just add that to the JOIN clause as well.  Hopefully the engine will get best match for criteria and run with that. In this case, your Entry Invoice table would have TWO indexes... one based on the company + invoice, the other based on company + id entry.

As for your invoice table, that is not even being used within your output and would otherwise just be like that of a lookup table, so I added that as a second join

Table           Index
Entry_Invoice   ( Company, ID_Invoice )
Entry_Invoice   ( Company, ID_Entry )
Invoice         ( Company, ID_Invoice )
UV_Entry_All    ( Company, ID_Entry )

SELECT
        ADRE.*
    FROM
        ENTRY_INVOICE NAF
            JOIN UV_ENTRY_ALL ADRE
                ON NAF.COMPANY = ADRE.COMPANY
                AND NAF.ID_ENTRY = ADRE.ID_ENTRY
            -- you dont actually NEED the invoice table here
            INVOICE N
                ON NAF.COMPANY = N.COMPANY
                AND NAF.ID_INVOICE = N.ID_INVOICE
    WHERE
            NAF.COMPANY = 1
        AND
        /*
            NAF.ID_INVOICE = 113806
                 vs
            NAF.ID_ENTRY = 387473

            -- dont think this would even be necessary any more
            EXISTS (SELECT 1 FROM ENTRY_INVOICE WHERE COMPANY = 1 AND ID_INVOICE=113806)
        */


问:
How can I retrieve specific information from a XML file using python?
I am working with Sentinel-2 Images, and I want to retrieve the Cloud_Coverage_Assessment from the XML file. I need to do this with Python.

Does anyone have any idea how to do this? I think I have to use the xml.etree.ElementTree but I'm not sure how?

The XML file:

```<code><n1:Level-1C_User_Product xmlns:n1="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd">
  <n1:General_Info>
  ...
  </n1:General_Info>
  <n1:Geometric_Info>
  ...
  </n1:Geometric_Info>
  <n1:Auxiliary_Data_Info>
  ...
  </n1:Auxiliary_Data_Info>
  <n1:Quality_Indicators_Info>
    <Cloud_Coverage_Assessment>90.7287</Cloud_Coverage_Assessment>
    <Technical_Quality_Assessment>
    ...
    </Technical_Quality_Assessment>
    <Quality_Control_Checks>
    ...
    </Quality_Control_Checks>
  </n1:Quality_Indicators_Info>
</n1:Level-1C_User_Product>
</code>```

答:
..and I want to retrieve the Cloud_Coverage_Assessment

Try the below  (use xpath)

```<code>import xml.etree.ElementTree as ET

xml = '''<n1:Level-1C_User_Product xmlns:n1="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd">
  <n1:General_Info>
  </n1:General_Info>
  <n1:Geometric_Info>
  </n1:Geometric_Info>
  <n1:Auxiliary_Data_Info>
  </n1:Auxiliary_Data_Info>
  <n1:Quality_Indicators_Info>
    <Cloud_Coverage_Assessment>90.7287</Cloud_Coverage_Assessment>
    <Technical_Quality_Assessment>
    </Technical_Quality_Assessment>
    <Quality_Control_Checks>
    </Quality_Control_Checks>
  </n1:Quality_Indicators_Info>
</n1:Level-1C_User_Product>'''

root = ET.fromstring(xml)
print(root.find('.//Cloud_Coverage_Assessment').text)
</code>```

output

```<code>90.7287
</code>```

Body:
I am working with Sentinel-2 Images, and I want to retrieve the Cloud_Coverage_Assessment from the XML file. I need to do this with Python.

Does anyone have any idea how to do this? I think I have to use the xml.etree.ElementTree but I'm not sure how?

The XML file:

<n1:Level-1C_User_Product xmlns:n1="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd">
  <n1:General_Info>
  ...
  </n1:General_Info>
  <n1:Geometric_Info>
  ...
  </n1:Geometric_Info>
  <n1:Auxiliary_Data_Info>
  ...
  </n1:Auxiliary_Data_Info>
  <n1:Quality_Indicators_Info>
    <Cloud_Coverage_Assessment>90.7287</Cloud_Coverage_Assessment>
    <Technical_Quality_Assessment>
    ...
    </Technical_Quality_Assessment>
    <Quality_Control_Checks>
    ...
    </Quality_Control_Checks>
  </n1:Quality_Indicators_Info>
</n1:Level-1C_User_Product>

Body:
..and I want to retrieve the Cloud_Coverage_Assessment

Try the below  (use xpath)

import xml.etree.ElementTree as ET

xml = '''<n1:Level-1C_User_Product xmlns:n1="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd">
  <n1:General_Info>
  </n1:General_Info>
  <n1:Geometric_Info>
  </n1:Geometric_Info>
  <n1:Auxiliary_Data_Info>
  </n1:Auxiliary_Data_Info>
  <n1:Quality_Indicators_Info>
    <Cloud_Coverage_Assessment>90.7287</Cloud_Coverage_Assessment>
    <Technical_Quality_Assessment>
    </Technical_Quality_Assessment>
    <Quality_Control_Checks>
    </Quality_Control_Checks>
  </n1:Quality_Indicators_Info>
</n1:Level-1C_User_Product>'''

root = ET.fromstring(xml)
print(root.find('.//Cloud_Coverage_Assessment').text)

output

90.7287


问:
API returning 401 on Azure but not when run locally
The application in question is a web site which has an API tacked on the side that reuses the many of the data access methods developed for the website. So there maybe some interference between the web site authentication/authorization and the API's. But if that was the case I don't understand why it works locally.

When I run locally, I can test the API using Swagger or Postman to login, get the Bearer token and use it to call the API methods. On Azure, although the login succeeds the next call to the API returns a 401:

```<code>www-authenticate: Bearer error="invalid_token",error_description="The signature is invalid"
</code>```

The most obvious difference is the appsettings.json which I've copied to the Application Configuration blade on Azure:

The original appsettings looked like:

```<code>  "Rt5": {
    "BaseAddress": "https://localhost:44357"
  },
  "Azure": {
    "BuildNumber": ""
  },
  "AuthentificationJWT": {
    "Audience": "ddt-ssp",
    "Issuer": "ddt-rt5",
    "SecurityKey": "not so secret"
  },
</code>```

Usage - ConfigureServices():

```<code>        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
            {
                options.AccessDeniedPath = "/Home/Unauthorized";
                options.LoginPath = "/User/Login";
                options.LogoutPath = "/User/Logout";
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["AuthentificationJWT:SecurityKey"])),
                    ValidateIssuer = true,
                    ValidIssuer = Configuration["AuthentificationJWT:Issuer"],
                    ValidateAudience = true,
                    ValidAudience = Configuration["AuthentificationJWT:Audience"],
                    ValidateLifetime = true,
                    ValidAlgorithms = new HashSet<string> { SecurityAlgorithms.HmacSha256 },
                    ValidTypes = new HashSet<string> { "JWT" }
                };
            });

        services.AddAuthorization(o =>
        {
            o.AddPolicy(AllowViewExceptions.Policy, p
                => p.Requirements.Add(new AllowViewExceptions.Requirement()));
            o.AddPolicy(AllowSubmitException.Policy, p
                => p.Requirements.Add(new AllowSubmitException.Requirement()));
        });
</code>```

Usage - Configure():

```<code>        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseAuthorization();

        if (UseSwagger)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"); });
        }

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
</code>```

Confession: this isn't my code and I'm sure why we have the Cookie stuff mentioned though is does seem to be offer an alternative authorization mechanism. See here:

```<code>    public string GetAuthorization()
    {
        //TODO: use ExtractSecurityToken();
        var claimToken = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == AuthenticationConfig.AccessTokenCookieName);
        if (claimToken != null)
        {
            return $"Bearer {claimToken.Value}";
        }
        else if (_httpContextAccessor.HttpContext.Request.Headers.TryGetValue(AuthenticationConfig.AuthorizationHeader, out var headerToken))
        {
            return headerToken;
        }

        return null;
    }

    private JwtSecurityToken ExtractSecurityToken()
    {
        var accessToken = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == AuthenticationConfig.AccessTokenCookieName)?.Value;
        if (accessToken == null &&
            _httpContextAccessor.HttpContext.Request.Headers.TryGetValue(AuthenticationConfig.AuthorizationHeader, out var value))
        {
            accessToken = value.ToString().Replace("Bearer ", ""); // TODO: Find a better way then extracting Bearer e.g Get token without scheme
        }

        if (accessToken == null)
        {
            return null;
        }

        return (JwtSecurityToken)new JwtSecurityTokenHandler().ReadToken(accessToken);
    }
</code>```

答:
It turned out the problem was caused by the bizarre architecture surround the application. The Access Token value (including the SHA256 checksum calculated using the security key) was being generated in another application and passed to this one to use for its communications.

When run localling both applications were using a security key stored in their appsettings.json file and were the same.  The pipeline which deployed these applications to Azure was substituting security keys for random values and so they differed.

I've left the question here undeleted because Suryasri's link may help others later.

Body:
The application in question is a web site which has an API tacked on the side that reuses the many of the data access methods developed for the website. So there maybe some interference between the web site authentication/authorization and the API's. But if that was the case I don't understand why it works locally.

When I run locally, I can test the API using Swagger or Postman to login, get the Bearer token and use it to call the API methods. On Azure, although the login succeeds the next call to the API returns a 401:

www-authenticate: Bearer error="invalid_token",error_description="The signature is invalid"

The most obvious difference is the appsettings.json which I've copied to the Application Configuration blade on Azure:

The original appsettings looked like:

  "Rt5": {
    "BaseAddress": "https://localhost:44357"
  },
  "Azure": {
    "BuildNumber": ""
  },
  "AuthentificationJWT": {
    "Audience": "ddt-ssp",
    "Issuer": "ddt-rt5",
    "SecurityKey": "not so secret"
  },

Usage - ConfigureServices():

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
            {
                options.AccessDeniedPath = "/Home/Unauthorized";
                options.LoginPath = "/User/Login";
                options.LogoutPath = "/User/Logout";
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["AuthentificationJWT:SecurityKey"])),
                    ValidateIssuer = true,
                    ValidIssuer = Configuration["AuthentificationJWT:Issuer"],
                    ValidateAudience = true,
                    ValidAudience = Configuration["AuthentificationJWT:Audience"],
                    ValidateLifetime = true,
                    ValidAlgorithms = new HashSet<string> { SecurityAlgorithms.HmacSha256 },
                    ValidTypes = new HashSet<string> { "JWT" }
                };
            });

        services.AddAuthorization(o =>
        {
            o.AddPolicy(AllowViewExceptions.Policy, p
                => p.Requirements.Add(new AllowViewExceptions.Requirement()));
            o.AddPolicy(AllowSubmitException.Policy, p
                => p.Requirements.Add(new AllowSubmitException.Requirement()));
        });

Usage - Configure():

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseAuthorization();

        if (UseSwagger)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"); });
        }

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

Confession: this isn't my code and I'm sure why we have the Cookie stuff mentioned though is does seem to be offer an alternative authorization mechanism. See here:

    public string GetAuthorization()
    {
        //TODO: use ExtractSecurityToken();
        var claimToken = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == AuthenticationConfig.AccessTokenCookieName);
        if (claimToken != null)
        {
            return $"Bearer {claimToken.Value}";
        }
        else if (_httpContextAccessor.HttpContext.Request.Headers.TryGetValue(AuthenticationConfig.AuthorizationHeader, out var headerToken))
        {
            return headerToken;
        }

        return null;
    }

    private JwtSecurityToken ExtractSecurityToken()
    {
        var accessToken = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == AuthenticationConfig.AccessTokenCookieName)?.Value;
        if (accessToken == null &&
            _httpContextAccessor.HttpContext.Request.Headers.TryGetValue(AuthenticationConfig.AuthorizationHeader, out var value))
        {
            accessToken = value.ToString().Replace("Bearer ", ""); // TODO: Find a better way then extracting Bearer e.g Get token without scheme
        }

        if (accessToken == null)
        {
            return null;
        }

        return (JwtSecurityToken)new JwtSecurityTokenHandler().ReadToken(accessToken);
    }


问:
How to generate combinations of elements of different lists?
I have a list of lists that goes like

```<code>[[1,2,3],[3,4,5],[5,6,7,8]]
</code>```

I want to create a program/function that creates a combinations like

```<code>1,3,5
1,3,6
1,3,7
1,3,8
1,4,5
1,4,6
.
.
.
</code>```

If there's a way you can do it without using the itertools module, that would be even more appreciated.

Body:
I have a list of lists that goes like

[[1,2,3],[3,4,5],[5,6,7,8]]

I want to create a program/function that creates a combinations like

1,3,5
1,3,6
1,3,7
1,3,8
1,4,5
1,4,6
.
.
.

If there's a way you can do it without using the itertools module, that would be even more appreciated.

Body:
Here is how you can use a recursive function:

def comb(arrs, i=0, numbs=[]):
    if i == len(arrs):
        print(*numbs)
        return
    for j in arrs[i]:
        comb(arrs, i + 1, numbs + [j])

arrs = [[1,2,3],[3,4,5],[5,6,7,8]]
comb(arrs)

Output:

1 3 5
1 3 6
1 3 7
1 3 8
1 4 5
1 4 6
1 4 7
1 4 8
1 5 5
1 5 6
1 5 7
1 5 8
2 3 5
2 3 6
2 3 7
2 3 8
2 4 5
2 4 6
2 4 7
2 4 8
2 5 5
2 5 6
2 5 7
2 5 8
3 3 5
3 3 6
3 3 7
3 3 8
3 4 5
3 4 6
3 4 7
3 4 8
3 5 5
3 5 6
3 5 7
3 5 8


问:
Add a character to a specific part of a string?
I have a list of file names as such:

```<code>"A/B/file.jpeg"

"A/C/file2.jpeg"

"B/C/file3.jpeg"
</code>```

and a couple of variations of such.

My question is how would I be able to add a "new" or any characters into each of these file names after the second "/" such that the length of the string/name doesn't matter just that it is placed after the second "/"

Results would ideally be:

```<code>"A/B/newfile.jpeg"

"A/B/newfile2.jpeg" etc.
</code>```

Thanks!

答:
Using gsub.

```<code>gsub('(file)', 'new\\1', x)
# [1] "A/B/newfile.jpeg"  "A/C/newfile2.jpeg" "B/C/newfile3.jpeg"
</code>```

Data:

```<code>x <- c("A/B/file.jpeg", "A/C/file2.jpeg", "B/C/file3.jpeg")
</code>```

Body:
I have a list of file names as such:

"A/B/file.jpeg"

"A/C/file2.jpeg"

"B/C/file3.jpeg"

and a couple of variations of such.

My question is how would I be able to add a "new" or any characters into each of these file names after the second "/" such that the length of the string/name doesn't matter just that it is placed after the second "/"

Results would ideally be:

"A/B/newfile.jpeg"

"A/B/newfile2.jpeg" etc.

Thanks!

Body:
Using gsub.

gsub('(file)', 'new\\1', x)
# [1] "A/B/newfile.jpeg"  "A/C/newfile2.jpeg" "B/C/newfile3.jpeg"

Data:

x <- c("A/B/file.jpeg", "A/C/file2.jpeg", "B/C/file3.jpeg")


问:
Convert date format, BMC Remedy/smart-it
Problem:
In a field called $Detailed Decription$ sometimes dateformat 08/09/2021 is enterd and this need to be converted to swedish format 2022-02-11

I'am going to use BMC Developer studio and make a filter but i cant find a fitting solution for it. Replacing it wont work (i think) becaus it need to have a value to replace it with.

Maby there can be a function that reads regex (\d{2})/(\d{1,2})/(\d{4}) but how can i convert it?

答:
If it's sometimes - look at AR System User Preferencje form. Check certain user's locale and date time config.
Also is important where the data comes from. Could be a browser setting or java script mod.</p>


问:
Authentication management using "in-database" design
I want to discuss wether good or bad idea:

I got a MySQL DB and created a common table "user" to authenticate an login.

```<code>|User|
|user_id|username|password|created_at|
</code>```

I implemented a stored function and some triggers.

First of all:

```<code>ON BEFORE UPDATE
</code>```

will generate a SHA256 hash and salt when password was changed.
salt is generated out witha mix of created_at, user_id and a global salt_mod which is stored in another "config-table".

So when entering 123 in "password" via normal UPDATe it will produce user-unique password and salt hashes.

Next I implemented a stored function

```<code>checkUserAuth('username', 'password')
</code>```

returns: bool true or false

Ofcourse: receives PLAIN username and password, replicates same hashing logic and returns
bool "true" or "false", depends on if the credentials have been correct.

Pro:

This makes completely sync of password algorithm changes obsolete to any connected apps.

Database-account that is being used by the app can work without SELECT privileges on "username", "password" and "salt" privileges.

Even if the user-account of the database is stolen, passwords are still safe due to lacking permissions to read the FUNCTIONS source-code and the columns that store login-information. We only have an EXECUTE privilege here.

Contra:

Well if someone breaks in on the DB with root privileges, pretty much the source-code on "how to generate the hashes" is leaked (pretty much the salting formula) together with all information in one place. However its still hard to rainbow-table that due to unique hashes on created_at date and user_id in mix with global_salt.

The question to above scenario:

```<code>Is this a good approach or totally vulnerable in point of data-security?
</code>```

I mean on one side, a break-in inside the database is always a problem that should never happen

On the other side, even with stolen source-code of the apps or stolen database-account by a bug in the app you cannot steal any login related data.

The root account is ofcourse excluded from being using elsewhere then on the localhost/server.

What do you think about this?

答:
The primary weakness is that you pass the password in plaintext when you create the row, and every time you call your checkUserAuth() function.

This can be wiretapped (unless you ensure using SSL connections between app and database) and it will be written to the query log if you have the query log enabled. It's also visible in the processlist and the performance_schema statements history tables.

This is a good reason to do the hashing in the client, and send only the salted hash of the password when you create the row. When you authenticate, fetch the salted hash from the database, and validate it against user input in the client.

Body:
I want to discuss wether good or bad idea:

I got a MySQL DB and created a common table "user" to authenticate an login.

|User|
|user_id|username|password|created_at|

I implemented a stored function and some triggers.

First of all:

ON BEFORE UPDATE

will generate a SHA256 hash and salt when password was changed.
salt is generated out witha mix of created_at, user_id and a global salt_mod which is stored in another "config-table".

So when entering 123 in "password" via normal UPDATe it will produce user-unique password and salt hashes.

Next I implemented a stored function

checkUserAuth('username', 'password')

returns: bool true or false

Ofcourse: receives PLAIN username and password, replicates same hashing logic and returns
bool "true" or "false", depends on if the credentials have been correct.

Pro:

This makes completely sync of password algorithm changes obsolete to any connected apps.

Database-account that is being used by the app can work without SELECT privileges on "username", "password" and "salt" privileges.

Even if the user-account of the database is stolen, passwords are still safe due to lacking permissions to read the FUNCTIONS source-code and the columns that store login-information. We only have an EXECUTE privilege here.

Contra:

Well if someone breaks in on the DB with root privileges, pretty much the source-code on "how to generate the hashes" is leaked (pretty much the salting formula) together with all information in one place. However its still hard to rainbow-table that due to unique hashes on created_at date and user_id in mix with global_salt.

The question to above scenario:

Is this a good approach or totally vulnerable in point of data-security?

I mean on one side, a break-in inside the database is always a problem that should never happen

On the other side, even with stolen source-code of the apps or stolen database-account by a bug in the app you cannot steal any login related data.

The root account is ofcourse excluded from being using elsewhere then on the localhost/server.

What do you think about this?


问:
Increases physical memory continuously Visual C++ CryptMsgClose and CryptReleaseContext
I am using C++ and Visual Studio 2005.

I have a project that memory Increases in a very abnormal. When debug the code I Realized That there are Several parts That Contribute to it. Like this for example:

```<code> // has to add crypt32.lib to link
#include <windows.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

void memoryUP( const unsigned char *pData, int cData )
{
    HCRYPTMSG  msg  = NULL;
    HCRYPTPROV hProv        = NULL;

    CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,0);

    msg = CryptMsgOpenToDecode(MY_ENCODING_TYPE,0,0,hProv,NULL,NULL);

    if(!(CryptMsgUpdate( msg, pData, cData, TRUE)))
    {
        if(msg != NULL)
        {
            CryptMsgClose(msg);
            msg = NULL;
        }
    }

    if (hProv != NULL)
        CryptReleaseContext(hProv,0);

    if (msg != NULL)
    {
        CryptMsgClose(msg);
        msg = NULL;
    }
}

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\file.p7s");

    {
        for(int i=0; i<100000; ++i)
        {
            memoryUP( myfile._data, myfile._length );
        }
    }

    delete myfile;

    return 0;
}
</code>```

When I run this code, the memory goes up continuously "when call CryptMsgUpdate". Am I deallocating wrong?

I tried to use Memory Leak Detection Enabling method to detect memory leak but nothing appears:

```<code>#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

and

_CrtDumpMemoryLeaks();
</code>```

Thanks in Advance

答: I think you should call CryptMsgClose before CryptReleaseContext.</p>

Body:
I am using C++ and Visual Studio 2005.

I have a project that memory Increases in a very abnormal. When debug the code I Realized That there are Several parts That Contribute to it. Like this for example:

 // has to add crypt32.lib to link
#include <windows.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

void memoryUP( const unsigned char *pData, int cData )
{
    HCRYPTMSG  msg  = NULL;
    HCRYPTPROV hProv        = NULL;

    CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,0);

    msg = CryptMsgOpenToDecode(MY_ENCODING_TYPE,0,0,hProv,NULL,NULL);

    if(!(CryptMsgUpdate( msg, pData, cData, TRUE)))
    {
        if(msg != NULL)
        {
            CryptMsgClose(msg);
            msg = NULL;
        }
    }

    if (hProv != NULL)
        CryptReleaseContext(hProv,0);

    if (msg != NULL)
    {
        CryptMsgClose(msg);
        msg = NULL;
    }
}

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\file.p7s");

    {
        for(int i=0; i<100000; ++i)
        {
            memoryUP( myfile._data, myfile._length );
        }
    }

    delete myfile;

    return 0;
}

When I run this code, the memory goes up continuously "when call CryptMsgUpdate". Am I deallocating wrong?

I tried to use Memory Leak Detection Enabling method to detect memory leak but nothing appears:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

and

_CrtDumpMemoryLeaks();

Thanks in Advance


问:
cudaErrorUnknown when calling cuda runtime or driver api
After heavy simulation that crashes my GPU, terminating the program and rebooting my computer, I cannot call any cuda api that runs correctly before rebooting.
nvidia-smi works well.
In my Ubuntu 20.04 computer, the CUDA11.6 and nvidia-driver 510.47.03 are installed.

The minimum codes for getting error in my computer are followings,

Driver api version

#include "cuda.h"
int main() {
  CUresult result;
  result = cuInit(0);
  const char* pStr;
  cuGetErrorName(result, &pStr);
  if(result != CUDA_SUCCESS){
      std::cerr << pStr << std::endl;
  }
}
```
<p>The compile command is,</p>
```nvcc a.cu -lcuda
```
<p>and I got <code>CUDA_ERROR_UNKNOWN</code>.</p>
<p>Runtime api version,</p>
<pre class="lang-cpp prettyprint-override"><code>#include <iostream>
int main() {
  int n;
  cudaError_t error = cudaGetDeviceCount(&n);
  if(error){
      std::cerr << cudaGetErrorName(error) << std::endl;
  }
}
</code>```

The compile command is,

```<code>nvcc a.cu
</code>```

and I got cudaUnknownError.

Why I got this error ? Is this a problem of driver ?

答:
looks like you trashed something (driver or cuda runtime) and you are not able to call any function related to cuda.

in my humble experience, I usually get these errors when my kernels runs for too long on a Windows machine and the Windows Display Driver Manager reset my GPU while i'm running the kernel.

Maybe you are experiencing some similiar issues on linux.

To fix this, have you tried to reset your GPU using the following bash command line ?

nvidia-smi --gpu-reset
```

Please also note that all CUDA errors are not recoverable and if the previous command doesn't work, you may reset your computer to fix the issue (or simply unload/reload the cuda driver).

Body:
After heavy simulation that crashes my GPU, terminating the program and rebooting my computer, I cannot call any cuda api that runs correctly before rebooting.
nvidia-smi works well.
In my Ubuntu 20.04 computer, the CUDA11.6 and nvidia-driver 510.47.03 are installed.

The minimum codes for getting error in my computer are followings,

Driver api version

#include "cuda.h"
int main() {
  CUresult result;
  result = cuInit(0);
  const char* pStr;
  cuGetErrorName(result, &pStr);
  if(result != CUDA_SUCCESS){
      std::cerr << pStr << std::endl;
  }
}

The compile command is,

nvcc a.cu -lcuda

and I got CUDA_ERROR_UNKNOWN.

Runtime api version,

#include <iostream>
int main() {
  int n;
  cudaError_t error = cudaGetDeviceCount(&n);
  if(error){
      std::cerr << cudaGetErrorName(error) << std::endl;
  }
}

The compile command is,

nvcc a.cu

and I got cudaUnknownError.

Why I got this error ? Is this a problem of driver ?

Body:
looks like you trashed something (driver or cuda runtime) and you are not able to call any function related to cuda.

in my humble experience, I usually get these errors when my kernels runs for too long on a Windows machine and the Windows Display Driver Manager reset my GPU while i'm running the kernel.

Maybe you are experiencing some similiar issues on linux.

To fix this, have you tried to reset your GPU using the following bash command line ?

nvidia-smi --gpu-reset

Please also note that all CUDA errors are not recoverable and if the previous command doesn't work, you may reset your computer to fix the issue (or simply unload/reload the cuda driver).


问:
XML is not storing completely in Azure Storage Account via java
```<code>public void createAzureBlob(File file) {
//        System.out.println("AccountName : "+configuration.getAccountName());
        String storageConnectionString =
                "DefaultEndpointsProtocol=https;" +
                        "AccountName=" + "csb1003" + ";" + "AccountKey=sVQZOFBU0U/";

        File sourceFile = file;
        File downloadedFile = null;

        CloudStorageAccount storageAccount;
        CloudBlobClient blobClient = null;
        CloudBlobContainer container = null;

        try {
            // Parse the connection string and create a blob client to interact with Blob storage
            storageAccount = CloudStorageAccount.parse(storageConnectionString);
            blobClient = storageAccount.createCloudBlobClient();
//            container = blobClient.getContainerReference("quickstartcontainer5");
            container = blobClient.getContainerReference("transfer-to-ckb-container");

            // Create the container if it does not exist with public access.
//            System.out.println("Creating container: " + container.getName());
            container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER,
                    new BlobRequestOptions(),
                    new OperationContext());

            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(sourceFile.getName());

            //Creating blob and uploading file to it
            blob.uploadFromFile(sourceFile.getAbsolutePath());

            // Download blob. In most cases, you would have to retrieve the reference
            // to cloudBlockBlob here. However, we created that reference earlier, and
            // haven't changed the blob we're interested in, so we can reuse it.
            // Here we are creating a new file to download to. Alternatively you can also pass in the path as a string into downloadToFile method: blob.downloadToFile("/path/to/new/file").
            downloadedFile = new File(sourceFile.getParentFile(), "downloadedFile.xml");
            blob.downloadToFile(downloadedFile.getAbsolutePath());
        } catch (StorageException ex) {
            System.out.println(String.format("Error returned from the service. Http code: %d and error code: %s",
                    ex.getHttpStatusCode(),
                    ex.getErrorCode()));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            System.out.println("The program has completed successfully.");
        }
    }
</code>```

I am using above code to store the xml files into azure storage.The problem i am facing is last few lines of xml are missing in the azure stored file. How to store the all lines of xml into azure storage using java?

See the below screen shot for more info,

Actual :

Expected :

答:
Seems you are making your own connection string if so it won't work like that. Please paste the connection string from portal itself.

Code :

```<code>package com.company;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

import java.io.File;

public class StorageUpload {
    public static void main(String[] args) {
        File file1 = new File("C:\\Users\\v-rash18\\Downloads\\SignUpOrSignin.xml");
        createAzureBlob(file1);
    }

    public static void createAzureBlob(File file) {
//        System.out.println("AccountName : "+configuration.getAccountName());
        String storageConnectionString ="Connection String";
        File sourceFile = file;
        File downloadedFile = null;

        CloudStorageAccount storageAccount;
        CloudBlobClient blobClient = null;
        CloudBlobContainer container = null;

        try {
            // Parse the connection string and create a blob client to interact with Blob storage
            storageAccount = CloudStorageAccount.parse(storageConnectionString);
            blobClient = storageAccount.createCloudBlobClient();
//          container = blobClient.getContainerReference("dscconffiles");
            container = blobClient.getContainerReference("dscconffiles");

            // Create the container if it does not exist with public access.
//            System.out.println("Creating container: " + container.getName());

            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(sourceFile.getName());

            //Creating blob and uploading file to it
            blob.uploadFromFile(sourceFile.getAbsolutePath());

            // Download blob. In most cases, you would have to retrieve the reference
            // to cloudBlockBlob here. However, we created that reference earlier, and
            // haven't changed the blob we're interested in, so we can reuse it.
            // Here we are creating a new file to download to. Alternatively you can also pass in the path as a string into downloadToFile method: blob.downloadToFile("/path/to/new/file").
            downloadedFile = new File(sourceFile.getParentFile(), "downloadedFile.xml");
            blob.downloadToFile(downloadedFile.getAbsolutePath());
        } catch (StorageException ex) {
            System.out.println(String.format("Error returned from the service. Http code: %d and error code: %s",
                    ex.getHttpStatusCode(),
                    ex.getErrorCode()));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            System.out.println("The program has completed successfully.");
        }
    }

}
</code>```

FIle which i have uploaded from local

Content of File in Azure Storage Account

Body:
public void createAzureBlob(File file) {
//        System.out.println("AccountName : "+configuration.getAccountName());
        String storageConnectionString =
                "DefaultEndpointsProtocol=https;" +
                        "AccountName=" + "csb1003" + ";" + "AccountKey=sVQZOFBU0U/";

        File sourceFile = file;
        File downloadedFile = null;

        CloudStorageAccount storageAccount;
        CloudBlobClient blobClient = null;
        CloudBlobContainer container = null;

        try {
            // Parse the connection string and create a blob client to interact with Blob storage
            storageAccount = CloudStorageAccount.parse(storageConnectionString);
            blobClient = storageAccount.createCloudBlobClient();
//            container = blobClient.getContainerReference("quickstartcontainer5");
            container = blobClient.getContainerReference("transfer-to-ckb-container");

            // Create the container if it does not exist with public access.
//            System.out.println("Creating container: " + container.getName());
            container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER,
                    new BlobRequestOptions(),
                    new OperationContext());

            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(sourceFile.getName());

            //Creating blob and uploading file to it
            blob.uploadFromFile(sourceFile.getAbsolutePath());

            // Download blob. In most cases, you would have to retrieve the reference
            // to cloudBlockBlob here. However, we created that reference earlier, and
            // haven't changed the blob we're interested in, so we can reuse it.
            // Here we are creating a new file to download to. Alternatively you can also pass in the path as a string into downloadToFile method: blob.downloadToFile("/path/to/new/file").
            downloadedFile = new File(sourceFile.getParentFile(), "downloadedFile.xml");
            blob.downloadToFile(downloadedFile.getAbsolutePath());
        } catch (StorageException ex) {
            System.out.println(String.format("Error returned from the service. Http code: %d and error code: %s",
                    ex.getHttpStatusCode(),
                    ex.getErrorCode()));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            System.out.println("The program has completed successfully.");
        }
    }

I am using above code to store the xml files into azure storage.The problem i am facing is last few lines of xml are missing in the azure stored file. How to store the all lines of xml into azure storage using java?

See the below screen shot for more info,

Actual :

Expected :

Body:
Seems you are making your own connection string if so it won't work like that. Please paste the connection string from portal itself.

Code :

package com.company;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

import java.io.File;

public class StorageUpload {
    public static void main(String[] args) {
        File file1 = new File("C:\\Users\\v-rash18\\Downloads\\SignUpOrSignin.xml");
        createAzureBlob(file1);
    }

    public static void createAzureBlob(File file) {
//        System.out.println("AccountName : "+configuration.getAccountName());
        String storageConnectionString ="Connection String";
        File sourceFile = file;
        File downloadedFile = null;

        CloudStorageAccount storageAccount;
        CloudBlobClient blobClient = null;
        CloudBlobContainer container = null;

        try {
            // Parse the connection string and create a blob client to interact with Blob storage
            storageAccount = CloudStorageAccount.parse(storageConnectionString);
            blobClient = storageAccount.createCloudBlobClient();
//          container = blobClient.getContainerReference("dscconffiles");
            container = blobClient.getContainerReference("dscconffiles");

            // Create the container if it does not exist with public access.
//            System.out.println("Creating container: " + container.getName());

            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(sourceFile.getName());

            //Creating blob and uploading file to it
            blob.uploadFromFile(sourceFile.getAbsolutePath());

            // Download blob. In most cases, you would have to retrieve the reference
            // to cloudBlockBlob here. However, we created that reference earlier, and
            // haven't changed the blob we're interested in, so we can reuse it.
            // Here we are creating a new file to download to. Alternatively you can also pass in the path as a string into downloadToFile method: blob.downloadToFile("/path/to/new/file").
            downloadedFile = new File(sourceFile.getParentFile(), "downloadedFile.xml");
            blob.downloadToFile(downloadedFile.getAbsolutePath());
        } catch (StorageException ex) {
            System.out.println(String.format("Error returned from the service. Http code: %d and error code: %s",
                    ex.getHttpStatusCode(),
                    ex.getErrorCode()));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            System.out.println("The program has completed successfully.");
        }
    }

}

FIle which i have uploaded from local

Content of File in Azure Storage Account