问:
Iterative slurm job
I'm trying to optimize a study I'm doing. I currently have to job scripts I call them step1 and step2. In step1

```<code>#!/bin/bash

#SBATCH --output=slurm-%j.out
#SBATCH --nodes=16
#SBATCH --ntasks-per-node=28
#SBATCH --time=24:00:00

module load <everything I need>

echo "Start of program at `date`"

srun $HOME/project/bin/my_executable1 ../data/my_datafile0.dat

echo "End of program at `date`"
</code>```

After this job is done I have a new datafile that we can call my_datafile1.dat and this goes into the the second job script step2:

```<code>#!/bin/bash

#SBATCH --output=slurm-%j.out
#SBATCH --nodes=16
#SBATCH --ntasks-per-node=28
#SBATCH --time=24:00:00

module load <everything I need>

echo "Start of program at `date`"

srun $HOME/project/bin/my_executable1 ../data/my_datafile1.dat

echo "End of program at `date`"
</code>```

After this job I have a new datafile called my_datafile2.dat which I use in step1 again and then the new one in step2 etc. I'm wondering if there is a way to write a job script which does this iteration for me. I would like to tell it to do 20 iterations and then I'll end up with my_datafile1.dat, my_datafile2.dat, ..., my_datafile20.dat.

答:
In a single job? If so, you could just use a loop, like follows (with a bit of verbosity in the inner loop, but can be replace by a single line if wanted).

Edit after the clarification in the comments below: Basically one step is an execution of my_executable1 followed by my_executable2. To simplify, let's call A the output of 1 and B the output of 2:

#!/bin/bash

#SBATCH --output=slurm-%j.out
#SBATCH --nodes=16
#SBATCH --ntasks-per-node=28
#SBATCH --time=24:00:00

module load <everything I need>

echo "Start of program at `date`"

for I in $(seq 10); do
  CMD="srun $HOME/project/bin/my_executable1 ../data/my_datafile_A_${I}.dat"
  echo "Launching command \"$CMD\" at $(date)"
  eval $CMD
  CMD="srun $HOME/project/bin/my_executable2 ../data/my_datafile_B_${I}.dat"
  echo "Launching command \"$CMD\" at $(date)"
  eval $CMD
done

echo "End of program at `date`"
```
<p>If for some reason you <em>really</em> want to increment the index at each substep, you can use <code>bc</code> for the small computation:</p>
<pre class="lang-sh prettyprint-override"><code>#!/bin/bash

#SBATCH --output=slurm-%j.out
#SBATCH --nodes=16
#SBATCH --ntasks-per-node=28
#SBATCH --time=24:00:00

module load <everything I need>

echo "Start of program at `date`"

for I in $(seq 10); do
  INDEX=$(echo "2*$I-1" | bc)
  CMD="srun $HOME/project/bin/my_executable1 ../data/my_datafile_${INDEX}.dat"
  echo "Launching command \"$CMD\" at $(date)"
  eval $CMD
  INDEX=$(echo "2*$I" | bc)
  CMD="srun $HOME/project/bin/my_executable2 ../data/my_datafile_${INDEX}.dat"
  echo "Launching command \"$CMD\" at $(date)"
  eval $CMD
done

echo "End of program at `date`"
</code>```

Title: Iterative slurm job

Body:
I'm trying to optimize a study I'm doing. I currently have to job scripts I call them step1 and step2. In step1

#!/bin/bash

#SBATCH --output=slurm-%j.out
#SBATCH --nodes=16
#SBATCH --ntasks-per-node=28
#SBATCH --time=24:00:00

module load <everything I need>

echo "Start of program at `date`"

srun $HOME/project/bin/my_executable1 ../data/my_datafile0.dat

echo "End of program at `date`"

After this job is done I have a new datafile that we can call my_datafile1.dat and this goes into the the second job script step2:

#!/bin/bash

#SBATCH --output=slurm-%j.out
#SBATCH --nodes=16
#SBATCH --ntasks-per-node=28
#SBATCH --time=24:00:00

module load <everything I need>

echo "Start of program at `date`"

srun $HOME/project/bin/my_executable1 ../data/my_datafile1.dat

echo "End of program at `date`"

After this job I have a new datafile called my_datafile2.dat which I use in step1 again and then the new one in step2 etc. I'm wondering if there is a way to write a job script which does this iteration for me. I would like to tell it to do 20 iterations and then I'll end up with my_datafile1.dat, my_datafile2.dat, ..., my_datafile20.dat.

Body:
In a single job? If so, you could just use a loop, like follows (with a bit of verbosity in the inner loop, but can be replace by a single line if wanted).

Edit after the clarification in the comments below: Basically one step is an execution of my_executable1 followed by my_executable2. To simplify, let's call A the output of 1 and B the output of 2:

#!/bin/bash

#SBATCH --output=slurm-%j.out
#SBATCH --nodes=16
#SBATCH --ntasks-per-node=28
#SBATCH --time=24:00:00

module load <everything I need>

echo "Start of program at `date`"

for I in $(seq 10); do
  CMD="srun $HOME/project/bin/my_executable1 ../data/my_datafile_A_${I}.dat"
  echo "Launching command \"$CMD\" at $(date)"
  eval $CMD
  CMD="srun $HOME/project/bin/my_executable2 ../data/my_datafile_B_${I}.dat"
  echo "Launching command \"$CMD\" at $(date)"
  eval $CMD
done

echo "End of program at `date`"

If for some reason you really want to increment the index at each substep, you can use bc for the small computation:

#!/bin/bash

#SBATCH --output=slurm-%j.out
#SBATCH --nodes=16
#SBATCH --ntasks-per-node=28
#SBATCH --time=24:00:00

module load <everything I need>

echo "Start of program at `date`"

for I in $(seq 10); do
  INDEX=$(echo "2*$I-1" | bc)
  CMD="srun $HOME/project/bin/my_executable1 ../data/my_datafile_${INDEX}.dat"
  echo "Launching command \"$CMD\" at $(date)"
  eval $CMD
  INDEX=$(echo "2*$I" | bc)
  CMD="srun $HOME/project/bin/my_executable2 ../data/my_datafile_${INDEX}.dat"
  echo "Launching command \"$CMD\" at $(date)"
  eval $CMD
done

echo "End of program at `date`"


来源: stackexchange

问:
Caesar Cipher in Javascript
I am trying to write a program to solve the following problem in javascript (Written below this paragraph). I don't know why my code isn't working. Could someone help me? I'm new to javascript; this is a free code camp question.

"A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.

Write a function which takes a ROT13 encoded string as input and returns a decoded string."

function rot13(str) { // LBH QVQ VG!

  var string = "";
  for(var i = 0; i < str.length; i++) {
    var temp = str.charAt(i);
    if(temp !== " " || temp!== "!" || temp!== "?") {
       string += String.fromCharCode(13 + String.prototype.charCodeAt(temp));
    } else {
      string += temp;
    }
  }

  return string;
}

// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC")); //should decode to "FREE CODE CAMP"```

答:
Solution using for loop, in each iteration check if character is alphabet then decode it by subtracting 13 from it's ascii code, if decoded character is not alphabet (its ascii code is less than 65), then decode it by adding 13 to it.</p>
```<code>function rot13(str) {

  function isAlphabet(char) {
    return String(char).match(/[A-Z]/);
  }

  const chars = str.split("");

  for(let i = 0 ; i < chars.length; i++) {
    const char = chars[i];
    if(isAlphabet(char)) {
      const decoded = String.fromCharCode(char.charCodeAt() - 13);
      const decodedAlphabet = String.fromCharCode(char.charCodeAt() + 13);
      chars[i] = isAlphabet(decoded)? decoded : decodedAlphabet;
    }
  }

  return chars.join("")
}
</code>```

ContentLicense: CC BY-SA 3.0

Title: Caesar Cipher in Javascript

Body:
Solution using for loop, in each iteration check if character is alphabet then decode it by subtracting 13 from it's ascii code, if decoded character is not alphabet (its ascii code is less than 65), then decode it by adding 13 to it.

function rot13(str) {

  function isAlphabet(char) {
    return String(char).match(/[A-Z]/);
  }

  const chars = str.split("");

  for(let i = 0 ; i < chars.length; i++) {
    const char = chars[i];
    if(isAlphabet(char)) {
      const decoded = String.fromCharCode(char.charCodeAt() - 13);
      const decodedAlphabet = String.fromCharCode(char.charCodeAt() + 13);
      chars[i] = isAlphabet(decoded)? decoded : decodedAlphabet;
    }
  }

  return chars.join("")
}

ContentLicense: CC BY-SA 4.0


问:
How to create Web Services in Java (with Eclipse)
I am really really having hard time to create simple "Hello World" Web Service in Java. If you exclude several mobile Android mobile apps I have done, I am pretty new to Java environment.

First I tried Axis2 and that simply doesn't work out of the box. Fresh installation of Eclipse, Tomcat 6.0 and Axis2. Tried sample test app and it failed miserably. You can read about it here. My conclusion is that Axis2 simply doesn't work (maybe it used to work). I will try to go back to install older versions, maybe one will magically start working. I have to modify some old project at work that was using Axis2, so that's why I have to stick with it. I would hate to have to migrate to some other tool.

Then I ran on Oracle article Getting Started with JAX-RPC and I was thinking, cool, let's try that. Well, I just managed to get pissed off. After getting half way through the article and trying to write some simple test web service I realized that their code examples are poorly written. Missing brackets, wrong references, missing explanations, etc...

First of all, an interface should extend not implement. Am I right?

public interface SunRegPort implements java.rmi.Remote {

Also, this doesn't exist:

import java.xml.rpc.server.ServiceLifecycle;

But this does:

import javax.xml.rpc.server.ServiceLifecycle;

Well, I don't feel competent to criticize too much (cause of my level of knowledge about this subject of course), but after all I am suspicious that this article is complete JUNK and therefore I am not able to follow it.

Please somebody tell me that I am wrong and if anybody has some advice or link to some How-To page that talks about web services, I would appreciate.

Thanks.

答:
If you're still interested in giving Tomcat a shot, here is a tutorial on youtube of making a very basic "Hello world" example using Eclipse.  It leaves a lot to learn; but it looks like it will at least get you started.

FYI, the version of Eclipse used is slightly outdated.  So some of the menu options may not be exactly the same:

http://www.youtube.com/watch?v=EOkN5IPoJVs

Also, the meaning of "Web Service" can be subjective.  So, to be specific; this just shows you how to get a basic HTTP endpoint started on your local machine.


问:
Toolbar disappears when StackNavigationViewStyle on iPad | SwiftUI iOS 15
I have this TabView:

```<code>ZStack(alignment: Alignment(horizontal: .center, vertical: .bottom)){
        TabView(selection: $mainViewProperties.currentView) {

           ...

            //HOME
            NavigationView{
                HomeView(

                )
                    .navigationBarTitleDisplayMode(.inline)
            }
            .tag(MainViewProperties.Views.HOME)

            ...
        }

        //My Custom NavBar
        CustomBottomNav(selectedTab: $mainViewProperties.currentView)
            .padding()

    }
</code>```

And the HomeView's body is this:

```<code>    List{
        ForEach((0...30), id: \.self){ i in
            HStack{
                Spacer()
                Text("New \(i)")
                Spacer()
            }
                .padding()
                .background(Color.backgroundOver)
                .cornerRadius(.lotoUpCornerRadius)
                .lotoUpShadow() // <- CUSTOM EXTENSION

            //Bottom padding
            if i == 30 {
                VStack{

                }
                .frame(height: .paddingForBottomNav) // <- CUSTOM EXTENSION
            }
        }
        .listRowSeparator(.hidden)
        .listRowBackground(Color.background)
    }
    .listStyle(PlainListStyle())
    .background(
        Color.background
    )
    .toolbar {
        ToolbarItem(placement: .principal){

            Text(String.R.home_title)
                .font(getLotoUpFont(size: 20))
                .bold()
                .foregroundWithPrimaryGradient() // <- CUSTOM EXTENSION
        }
    }
</code>```

That look like this:

I don't want the side bar navigation on the iPad, so I added StackNavigationViewStyle to the NavigationView like this:

```<code>            //HOME
            NavigationView{
                HomeView(

                )
                    .navigationBarTitleDisplayMode(.inline)
            }
            .navigationViewStyle(.stack) //<- HERE
            .tag(MainViewProperties.Views.HOME)
</code>```

But then the Toolbar's background was not displayed anymore:

How can I get the Stack Navigation and the Toolbar's background get along together?

PD: I also tried this on HomeView:

```<code>    .toolbar {
        ToolbarItem(placement: .principal){
            HStack{
                Spacer()
                Text(String.R.home_title)
                    .font(getLotoUpFont(size: 20))
                    .bold()
                    .foregroundWithPrimaryGradient()
                Spacer()
            }
            .background(
                Color.red
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .edgesIgnoringSafeArea(.all)
            )
        }
    }
</code>```

But it lets spaces that I don't want:

EDIT:
I tested this on a real device (iPhone 8 Plus) and worked fine, the NavigationBar was there, but on the simulated iPhone 8 plus the NavigationBar background was translucent.
So now I'm suspecting on the simulator. I don't have an iPad to test this out though.

答: It seems to be a bug on simulator with iOS 15. On iOS 15.2 it works just fine.</p>

Body:
I have this TabView:

ZStack(alignment: Alignment(horizontal: .center, vertical: .bottom)){
        TabView(selection: $mainViewProperties.currentView) {

           ...

            //HOME
            NavigationView{
                HomeView(

                )
                    .navigationBarTitleDisplayMode(.inline)
            }
            .tag(MainViewProperties.Views.HOME)

            ...
        }

        //My Custom NavBar
        CustomBottomNav(selectedTab: $mainViewProperties.currentView)
            .padding()

    }

And the HomeView's body is this:

    List{
        ForEach((0...30), id: \.self){ i in
            HStack{
                Spacer()
                Text("New \(i)")
                Spacer()
            }
                .padding()
                .background(Color.backgroundOver)
                .cornerRadius(.lotoUpCornerRadius)
                .lotoUpShadow() // <- CUSTOM EXTENSION

            //Bottom padding
            if i == 30 {
                VStack{

                }
                .frame(height: .paddingForBottomNav) // <- CUSTOM EXTENSION
            }
        }
        .listRowSeparator(.hidden)
        .listRowBackground(Color.background)
    }
    .listStyle(PlainListStyle())
    .background(
        Color.background
    )
    .toolbar {
        ToolbarItem(placement: .principal){

            Text(String.R.home_title)
                .font(getLotoUpFont(size: 20))
                .bold()
                .foregroundWithPrimaryGradient() // <- CUSTOM EXTENSION
        }
    }

That look like this:

I don't want the side bar navigation on the iPad, so I added StackNavigationViewStyle to the NavigationView like this:

            //HOME
            NavigationView{
                HomeView(

                )
                    .navigationBarTitleDisplayMode(.inline)
            }
            .navigationViewStyle(.stack) //<- HERE
            .tag(MainViewProperties.Views.HOME)

But then the Toolbar's background was not displayed anymore:

How can I get the Stack Navigation and the Toolbar's background get along together?

PD: I also tried this on HomeView:

    .toolbar {
        ToolbarItem(placement: .principal){
            HStack{
                Spacer()
                Text(String.R.home_title)
                    .font(getLotoUpFont(size: 20))
                    .bold()
                    .foregroundWithPrimaryGradient()
                Spacer()
            }
            .background(
                Color.red
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .edgesIgnoringSafeArea(.all)
            )
        }
    }

But it lets spaces that I don't want:

EDIT:
I tested this on a real device (iPhone 8 Plus) and worked fine, the NavigationBar was there, but on the simulated iPhone 8 plus the NavigationBar background was translucent.
So now I'm suspecting on the simulator. I don't have an iPad to test this out though.


问:
Is there a simple way to show an image with a color depth of 10 bits on screen using Python?
I have a use case where I get images from Carla simulator buffer in a python script, running on a Win10 localhost, and I want to show these images on an HDR screen in a full 10 bit color depth.

Unfortunately I have not found a library yet that is capable of showing 10bit images and I wouldn't have expected this to be so complicated. Any advice is highly appreciated.

Best regards
Robert

答:
Well, there is such a way - be it using your current GPU + O/S + HDMI + Monitor available capabilities ( Intel advises how do a setup for theirs graphics chips, similarly do other HW manufacturers ),

Summary

Integrated Graphics can support 10-bit color depth.

Description

How to find out if your Intel® Graphics device supports a display with 10-bit color depth.

Resolution

Intel Graphics supports 10-bit and 12-bit color depths (known as deep color). The current Intel Grapchics Driver sets the color depth per the OS configuration by default. You can also manually set the color depth to 10 or 12 bit starting with Intel® Graphics Command Center version 1.100.3407.0 for native HDMI connections (if supported by the display).

You can also enable HDR (High Dynamic Range) and WCG (Wide Color Gamut) in the operating systems for higher color depths by going to Windows > Display settings > Windows HD Color. If the content you're trying to view is 10-bit, switch to Full Screen mode, and the driver will automatically switch to the higher color depth, given HDR and WCC are enabled. This is also true for most DirectX applications.

Note
The operating system, application being viewed, and driver will automatically determine which is the best mode to display dynamically.

or
using some other, HDR-Deep-Color ready hardware chain, as defined since about 2006 ( RPi possibly being one example of the simplest and cheapest case of doing such move )


问:
How to iterate through a nested list of dictionaries in Python
I have a bit complicated json response from a server, below is my  json:

```<code>    services = [{"Mobile": [{"name": "CompanyA", id: 0, address: "XYZ"},
    {"name": CompanyB, id: 1, address: "QWE"},
    {"name": CompanyC, id: 2, address: "TYU"}]
    },
    {"Computer": [{"name": "CompanyD", id: 3, address: "PPP"},
    {"name": CompanyD, id: 4, address: "UYU"},
    {"name": CompanyE, id: 5, address: "NMB"}]
    }]
</code>```

I need to construct new dictionary which only holds bellow data:

```<code>services = [{"Mobile": [{"name": "CompanyA"},{"name": "CompanyB"},
          {"name": "CompanyC"}]},
          {"Computer": [{"name": "CompanyD"},{"name": "CompanyD"},
          {"name": "CompanyE"}]
    }]
</code>```

in other words, delete id and address fields.

答:
Iterating through each nested dictionary/list and deleting the unwanted keys,</p>
```<code>for i in services:
    for j in i.values():
        for k in j:
            del k[id]
            del k["address"]

print(services)

Output:
[{'Mobile': [{'name': 'CompanyA'}, {'name': 'CompanyB'}, {'name': 'CompanyC'}]}, {'Computer': [{'name': 'CompanyD'}, {'name': 'CompanyD'}, {'name': 'CompanyE'}]}]
</code>```

Body:
I have a bit complicated json response from a server, below is my  json:

    services = [{"Mobile": [{"name": "CompanyA", id: 0, address: "XYZ"},
    {"name": CompanyB, id: 1, address: "QWE"},
    {"name": CompanyC, id: 2, address: "TYU"}]
    },
    {"Computer": [{"name": "CompanyD", id: 3, address: "PPP"},
    {"name": CompanyD, id: 4, address: "UYU"},
    {"name": CompanyE, id: 5, address: "NMB"}]
    }]

I need to construct new dictionary which only holds bellow data:

services = [{"Mobile": [{"name": "CompanyA"},{"name": "CompanyB"},
          {"name": "CompanyC"}]},
          {"Computer": [{"name": "CompanyD"},{"name": "CompanyD"},
          {"name": "CompanyE"}]
    }]

in other words, delete id and address fields.

Body:
Iterating through each nested dictionary/list and deleting the unwanted keys,

for i in services:
    for j in i.values():
        for k in j:
            del k[id]
            del k["address"]

print(services)

Output:
[{'Mobile': [{'name': 'CompanyA'}, {'name': 'CompanyB'}, {'name': 'CompanyC'}]}, {'Computer': [{'name': 'CompanyD'}, {'name': 'CompanyD'}, {'name': 'CompanyE'}]}]


问:
MySQL GROUP BY column and COUNT another column conditionally in the same table
I am trying to query a table reviews from a MySQL database. Each row represents a review made on a product. The data for the relevant columns is stored like this:

```<code>{ product_id: 25, rating: 1, recommend: 'false' },
{ product_id: 25, rating: 4, recommend: 'true' },
{ product_id: 25, rating: 3, recommend: 'true' },
{ product_id: 25, rating: 2, recommend: 'false' },
{ product_id: 25,  rating: 1, recommend: 'false'}
</code>```

I need to get the total of each type of rating (eg. how many reviews gave it a ‘1’), and then count how many reviews have recommend ‘true’ and how many have recommend ‘false’. I would like the query to return data like this:

```<code>{ product_id: 25, rating: 1, total: 2, recommendTrue: 2, recommendFalse: 3},
{ product_id: 25, rating: 2, total: 1, recommendTrue: 2, recommendFalse: 3},
{ product_id: 25, rating: 3, total: 1, recommendTrue: 2, recommendFalse: 3},
{ product_id: 25, rating: 4, total: 1, recommendTrue: 2, recommendFalse: 3},
</code>```

Technically I don’t need the recommendTrue and recommendFalse to be on every line, but because I am grouping by the type of rating, the redundancy is fine.

When I use this query (A):

```<code>'SELECT product_id, rating, COUNT(rating) as total'
' ' + 'FROM reviews'
' ' + 'WHERE reviews.product_id = ?'
' ' + 'GROUP BY rating'
' ' + 'LIMIT 50'
</code>```

I get back part of the desired result:

```<code>{ product_id: 25, rating: 1, total: 1 },
{ product_id: 25, rating: 3, total: 1 },
{ product_id: 25, rating: 2, total: 3 },
{ product_id: 25, rating: 5, total: 1 },
{ product_id: 25, rating: 4, total: 2 }
</code>```

I now need to count the total number of True and False recommends amongst all the reviews for product_id 25.

I am trying this query (B):

```<code>"SELECT r.product_id, COUNT(r.recommend='false') as F, COUNT(r.recommend='true') as T"
" " + "FROM reviews AS r"
" " + "WHERE r.product_id = ?"
" " + "GROUP BY r.rating"
" " + "LIMIT 50"
</code>```

And getting this result:

```<code>{ product_id: 25, F: 1, T: 1, rating: 1, total: 1 },
{ product_id: 25, F: 1, T: 1, rating: 3, total: 1 },
{ product_id: 25, F: 3, T: 3, rating: 2, total: 3 },
{ product_id: 25, F: 1, T: 1, rating: 5, total: 1 },
{ product_id: 25, F: 2, T: 2, rating: 4, total: 2 }
</code>```

The recommends need to be counted separately for True and False but should not be counted separately for rating. The way I am writing it, it is counting ALL the recommends (true and false) per rating.

答:
You have two separate queries: one for the true/false and one for the ratings.

You can combibe the two with a join statement. The query in the join will calculate the true/false and then the result is combined with the ratings query:

```<code>select product_id, rating, count(*), t, f
from reviews
  join (
    select
      sum(if(recommend='true', 1, 0)) as t,
      sum(if(recommend='false', 1, 0)) as f
    from reviews
    where product_id=?
  ) as q
where product_id=?
group by product_id, rating, t, f
</code>```

See db-fiddle.

Body:
I am trying to query a table reviews from a MySQL database. Each row represents a review made on a product. The data for the relevant columns is stored like this:

{ product_id: 25, rating: 1, recommend: 'false' },
{ product_id: 25, rating: 4, recommend: 'true' },
{ product_id: 25, rating: 3, recommend: 'true' },
{ product_id: 25, rating: 2, recommend: 'false' },
{ product_id: 25,  rating: 1, recommend: 'false'}

I need to get the total of each type of rating (eg. how many reviews gave it a ‘1’), and then count how many reviews have recommend ‘true’ and how many have recommend ‘false’. I would like the query to return data like this:

{ product_id: 25, rating: 1, total: 2, recommendTrue: 2, recommendFalse: 3},
{ product_id: 25, rating: 2, total: 1, recommendTrue: 2, recommendFalse: 3},
{ product_id: 25, rating: 3, total: 1, recommendTrue: 2, recommendFalse: 3},
{ product_id: 25, rating: 4, total: 1, recommendTrue: 2, recommendFalse: 3},

Technically I don’t need the recommendTrue and recommendFalse to be on every line, but because I am grouping by the type of rating, the redundancy is fine.

When I use this query (A):

'SELECT product_id, rating, COUNT(rating) as total'
' ' + 'FROM reviews'
' ' + 'WHERE reviews.product_id = ?'
' ' + 'GROUP BY rating'
' ' + 'LIMIT 50'

I get back part of the desired result:

{ product_id: 25, rating: 1, total: 1 },
{ product_id: 25, rating: 3, total: 1 },
{ product_id: 25, rating: 2, total: 3 },
{ product_id: 25, rating: 5, total: 1 },
{ product_id: 25, rating: 4, total: 2 }

I now need to count the total number of True and False recommends amongst all the reviews for product_id 25.

I am trying this query (B):

"SELECT r.product_id, COUNT(r.recommend='false') as F, COUNT(r.recommend='true') as T"
" " + "FROM reviews AS r"
" " + "WHERE r.product_id = ?"
" " + "GROUP BY r.rating"
" " + "LIMIT 50"

And getting this result:

{ product_id: 25, F: 1, T: 1, rating: 1, total: 1 },
{ product_id: 25, F: 1, T: 1, rating: 3, total: 1 },
{ product_id: 25, F: 3, T: 3, rating: 2, total: 3 },
{ product_id: 25, F: 1, T: 1, rating: 5, total: 1 },
{ product_id: 25, F: 2, T: 2, rating: 4, total: 2 }

The recommends need to be counted separately for True and False but should not be counted separately for rating. The way I am writing it, it is counting ALL the recommends (true and false) per rating.

Body:
You have two separate queries: one for the true/false and one for the ratings.

You can combibe the two with a join statement. The query in the join will calculate the true/false and then the result is combined with the ratings query:

select product_id, rating, count(*), t, f
from reviews
  join (
    select
      sum(if(recommend='true', 1, 0)) as t,
      sum(if(recommend='false', 1, 0)) as f
    from reviews
    where product_id=?
  ) as q
where product_id=?
group by product_id, rating, t, f

See db-fiddle.


问:
I keep getting this error java.util.NoSuchElementException
I am getting a NoSuchElementException which is probably due to Scanner and I get it after I add a student and go back to the menu.

My main method:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    static ArrayList<Student> students = new ArrayList<>();
    static Scanner scan = new Scanner(System.in);
    static Student s = new Student();

    public static void main(String[] args) {
        menu();
    }

    public static void menu() {
        System.out.println("       ***Students Manage***\n");
        System.out.println("1 - Insert a student");
        System.out.println("2 - Display students");
        System.out.println("3 - Remove student\n");
        option();
    }

    public static void option() {
        int option;

        do {
            System.out.print("Choose an option: ");
            option = scan.nextInt();
        }while(option < 1 || option > 3);

        switch(option) {
            case 1:
                s.addStudent();
                students.add(s);
                break;
            case 2:
                showAllStudents();
        }
        menu();
    }

    private static void showAllStudents() {
        for(int i = 0; i < students.size(); i++) {
            students.get(i).toString();
        }
    }
}
```
<p>My Student method:</p>
```import java.time.LocalDate;
import java.util.Scanner;

public class Student {

    String name;
    int studentNumber;
    static int count = 1;
    int yearNumber;

    public Student() {
    }

    public void addStudent() {
        Scanner scan = new Scanner(System.in);
        LocalDate year = LocalDate.now();
        yearNumber = year.getYear();
        studentNumber = count++;
        studentNumber += yearNumber * 10000;
        System.out.print("Enter the student name: ");
        name = scan.nextLine();

        if(name == null || name.trim().equals("") || !name.contains(" "))
        {
            do{
                if(name.trim().equals("")){
                    System.out.print("Please enter a valid name.\nEnter your name: ");
                } else if(!name.contains(" ")) {
                    System.out.print("You should write at least 2 names(name and surname).\nEnter your name: ");
                }
                name = scan.nextLine();
            }while(name == null || name.trim().equals("") || !name.contains(" "));
        }
        scan.close();
        System.out.println("The student "+name+" was added to the student list "
                + "and his/her student number is "+studentNumber);
    }

    @Override
    public String toString(){
        return name+" - "+studentNumber;
    }
}
```
<p>The error that I'm getting:</p>
```Exception in thread "main" java.util.NoSuchElementException
  at java.base/java.util.Scanner.throwFor(Scanner.java:937)
  at java.base/java.util.Scanner.next(Scanner.java:1594)
  at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
  at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
  at StudentSchool.Main.option(Main.java:29)
  at StudentSchool.Main.menu(Main.java:21)
  at StudentSchool.Main.option(Main.java:40)
  at StudentSchool.Main.menu(Main.java:21)
  at StudentSchool.Main.option(Main.java:40)
  at StudentSchool.Main.menu(Main.java:21)
  at StudentSchool.Main.main(Main.java:13)
```

答:
You have two problems in how you're using Scanner.  The first is that you are opening a second Scanner on System.in while you have one already open and reading from it.  This produces strange results due to buffering.  You should create and use only a single Scanner object that you never close.  You can do this by passing the Scanner you create in your main() method into your addStudent() method so that it can use that same Scanner instead of creating and later closing a new one.  So...

```<code>public void addStudent(Scanner scan) {
    // Scanner scan = new Scanner(System.in);
    ....
    // scan.close();

....

public static void option() {
    ....
    s.addStudent(scan);
    ...
</code>```

When you do this, a second problem will arise.  When you mix calls to nextInt and nextLine on a Scanner object, you run into a problem where nextInt only reads the numeric digits from the input stream, but leaves the newline character produce by the user hitting Return.  When you then call readLine, it reads that newline that's already on the input stream, so instead of waiting for your input, it reads an empty line as the next result.  To avoid this problem, you should add an extra call to readLine right after you call readInt to consume this stray newline character. So...

```<code>System.out.print("Choose an option: ");
option = scan.nextInt();
scan.nextLine();
</code>```

Making these two changes should cause your program to behave as you desire.

Body:
I am getting a NoSuchElementException which is probably due to Scanner and I get it after I add a student and go back to the menu.

My main method:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    static ArrayList<Student> students = new ArrayList<>();
    static Scanner scan = new Scanner(System.in);
    static Student s = new Student();

    public static void main(String[] args) {
        menu();
    }

    public static void menu() {
        System.out.println("       ***Students Manage***\n");
        System.out.println("1 - Insert a student");
        System.out.println("2 - Display students");
        System.out.println("3 - Remove student\n");
        option();
    }

    public static void option() {
        int option;

        do {
            System.out.print("Choose an option: ");
            option = scan.nextInt();
        }while(option < 1 || option > 3);

        switch(option) {
            case 1:
                s.addStudent();
                students.add(s);
                break;
            case 2:
                showAllStudents();
        }
        menu();
    }

    private static void showAllStudents() {
        for(int i = 0; i < students.size(); i++) {
            students.get(i).toString();
        }
    }
}

My Student method:

import java.time.LocalDate;
import java.util.Scanner;

public class Student {

    String name;
    int studentNumber;
    static int count = 1;
    int yearNumber;

    public Student() {
    }

    public void addStudent() {
        Scanner scan = new Scanner(System.in);
        LocalDate year = LocalDate.now();
        yearNumber = year.getYear();
        studentNumber = count++;
        studentNumber += yearNumber * 10000;
        System.out.print("Enter the student name: ");
        name = scan.nextLine();

        if(name == null || name.trim().equals("") || !name.contains(" "))
        {
            do{
                if(name.trim().equals("")){
                    System.out.print("Please enter a valid name.\nEnter your name: ");
                } else if(!name.contains(" ")) {
                    System.out.print("You should write at least 2 names(name and surname).\nEnter your name: ");
                }
                name = scan.nextLine();
            }while(name == null || name.trim().equals("") || !name.contains(" "));
        }
        scan.close();
        System.out.println("The student "+name+" was added to the student list "
                + "and his/her student number is "+studentNumber);
    }

    @Override
    public String toString(){
        return name+" - "+studentNumber;
    }
}

The error that I'm getting:

Exception in thread "main" java.util.NoSuchElementException
  at java.base/java.util.Scanner.throwFor(Scanner.java:937)
  at java.base/java.util.Scanner.next(Scanner.java:1594)
  at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
  at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
  at StudentSchool.Main.option(Main.java:29)
  at StudentSchool.Main.menu(Main.java:21)
  at StudentSchool.Main.option(Main.java:40)
  at StudentSchool.Main.menu(Main.java:21)
  at StudentSchool.Main.option(Main.java:40)
  at StudentSchool.Main.menu(Main.java:21)
  at StudentSchool.Main.main(Main.java:13)

Body:
You have two problems in how you're using Scanner.  The first is that you are opening a second Scanner on System.in while you have one already open and reading from it.  This produces strange results due to buffering.  You should create and use only a single Scanner object that you never close.  You can do this by passing the Scanner you create in your main() method into your addStudent() method so that it can use that same Scanner instead of creating and later closing a new one.  So...

public void addStudent(Scanner scan) {
    // Scanner scan = new Scanner(System.in);
    ....
    // scan.close();

....

public static void option() {
    ....
    s.addStudent(scan);
    ...

When you do this, a second problem will arise.  When you mix calls to nextInt and nextLine on a Scanner object, you run into a problem where nextInt only reads the numeric digits from the input stream, but leaves the newline character produce by the user hitting Return.  When you then call readLine, it reads that newline that's already on the input stream, so instead of waiting for your input, it reads an empty line as the next result.  To avoid this problem, you should add an extra call to readLine right after you call readInt to consume this stray newline character. So...

System.out.print("Choose an option: ");
option = scan.nextInt();
scan.nextLine();

Making these two changes should cause your program to behave as you desire.


问:
cannot get cloudwatch metric data with getMetricStatistics
I currently have a node application where I am trying to use the getMetricStatistics function of the AWS Cloudwatch sdk to retrieve data from a metric into my application.

In order to troubleshoot this I have run the listMetrics function as follows:

```<code>var params = {
        MetricName: 'Open',
        Namespace: 'AWS/SES',
    };
    cloudwatch.listMetrics(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
    });
</code>```

After running the above code, I get output written via console.log:

After this I try to run the getMetricStatistics function as follows:

```<code>    var cloudwatch = new aws.CloudWatch({apiVersion: '2010-08-01'});
    var params = {
        EndTime: new Date(2022,1,31), /* required */
        MetricName: 'Open', /* required */
        Namespace: 'AWS/SES', /* required */
        Period: '3600', /* required */
        StartTime: new Date(2022,1,27), /* required */
        Dimensions: [
          {
            Name: 'test-open-key', /* required */
            Value: 'test-open-value' /* required */
          }
          /* more items */
        ],
        Statistics: [
          'Average',
          /* more items */
        ]
      };
      cloudwatch.getMetricStatistics(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
      });
</code>```

However, the output of the above code is as follows, showing no datapoints:

I have based my input parameters based on what I got from listMetrics, and in my console I can see the following graph, meaning there should be at least one datapoint retrieved on the 29th January.

Would anyone be able to advise on what I'm doing wrong/any further avenues for troubleshooting?

答:
When creating the date object the month is 0-indexed(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#several_ways_to_create_a_date_object)

Change your time values like this:

```<code>StartTime: new Date(2022,0,27)
EndTime: new Date(2022,0,31),
</code>```

Body:
I currently have a node application where I am trying to use the getMetricStatistics function of the AWS Cloudwatch sdk to retrieve data from a metric into my application.

In order to troubleshoot this I have run the listMetrics function as follows:

var params = {
        MetricName: 'Open',
        Namespace: 'AWS/SES',
    };
    cloudwatch.listMetrics(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
    });

After running the above code, I get output written via console.log:

After this I try to run the getMetricStatistics function as follows:

    var cloudwatch = new aws.CloudWatch({apiVersion: '2010-08-01'});
    var params = {
        EndTime: new Date(2022,1,31), /* required */
        MetricName: 'Open', /* required */
        Namespace: 'AWS/SES', /* required */
        Period: '3600', /* required */
        StartTime: new Date(2022,1,27), /* required */
        Dimensions: [
          {
            Name: 'test-open-key', /* required */
            Value: 'test-open-value' /* required */
          }
          /* more items */
        ],
        Statistics: [
          'Average',
          /* more items */
        ]
      };
      cloudwatch.getMetricStatistics(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
      });

However, the output of the above code is as follows, showing no datapoints:

I have based my input parameters based on what I got from listMetrics, and in my console I can see the following graph, meaning there should be at least one datapoint retrieved on the 29th January.

Would anyone be able to advise on what I'm doing wrong/any further avenues for troubleshooting?

Body:
When creating the date object the month is 0-indexed(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#several_ways_to_create_a_date_object)

Change your time values like this:

StartTime: new Date(2022,0,27)
EndTime: new Date(2022,0,31),


问:
Flutter using choice chips to filter array in firebase
I am trying to use Flutter choice chips to act as a filter for firebase but I cannot seem to work out how to achieve this. It doesn't seem to present any errors, but it doesn't present any data so I am confused what I haven't done right here</p>
```<code>_setupTasks(currentUserId, filters) async {
    if (kDebugMode) {
      print("Tasker Browsing: $currentUserId");
    }
    List<Task> tasks =
        await DatabaseService.getTasks(currentUserId, 'Posted', filters);
    if (mounted) {
      setState(() {
        _tasks = tasks;
      });
    }
  }

FilterChip(
                            showCheckmark: false,
                            checkmarkColor: Colors.white,
                            backgroundColor: Colors.grey[300],
                            label: Text(cat['name']),
                            selected: _filters.contains(cat.id),
                            labelStyle: TextStyle(
                              color: _filters.contains(cat.id)
                                  ? Colors.white
                                  : Colors.black,
                            ),
                            selectedColor:
                                Theme.of(context).colorScheme.primary,
                            onSelected: (bool value) {
                              setState(() {
                                if (value) {
                                  _filters.add(cat.id);
                                  _setupTasks(widget.currentUserId, _filters);
                                } else {
                                  _filters.removeWhere((String name) {
                                    return name == cat.id;
                                  });
                                  _setupTasks(widget.currentUserId, _filters);
                                }
                                if (kDebugMode) {
                                  print(_filters);
                                }
                              });
                            }),

static Future<List<Task>> getTasks(
      String userId, String status, List<String> filter) async {
    QuerySnapshot userTasksSnapshot = await tasksRef
        .where('status', isEqualTo: status)
        .where('ownerId', isNotEqualTo: userId)
        .where('typeId', arrayContains: filter)

        // .orderBy('timestamp', descending: true)
        .get();
    List<Task> tasks =
        userTasksSnapshot.docs.map((doc) => Task.fromDoc(doc)).toList();
    return tasks;
  }
</code>```

答: Firestore doesn't support logical OR queries. You can instead make one query for each condition you want to match, and merge the results.</p>

Body:
I am trying to use Flutter choice chips to act as a filter for firebase but I cannot seem to work out how to achieve this. It doesn't seem to present any errors, but it doesn't present any data so I am confused what I haven't done right here

_setupTasks(currentUserId, filters) async {
    if (kDebugMode) {
      print("Tasker Browsing: $currentUserId");
    }
    List<Task> tasks =
        await DatabaseService.getTasks(currentUserId, 'Posted', filters);
    if (mounted) {
      setState(() {
        _tasks = tasks;
      });
    }
  }

FilterChip(
                            showCheckmark: false,
                            checkmarkColor: Colors.white,
                            backgroundColor: Colors.grey[300],
                            label: Text(cat['name']),
                            selected: _filters.contains(cat.id),
                            labelStyle: TextStyle(
                              color: _filters.contains(cat.id)
                                  ? Colors.white
                                  : Colors.black,
                            ),
                            selectedColor:
                                Theme.of(context).colorScheme.primary,
                            onSelected: (bool value) {
                              setState(() {
                                if (value) {
                                  _filters.add(cat.id);
                                  _setupTasks(widget.currentUserId, _filters);
                                } else {
                                  _filters.removeWhere((String name) {
                                    return name == cat.id;
                                  });
                                  _setupTasks(widget.currentUserId, _filters);
                                }
                                if (kDebugMode) {
                                  print(_filters);
                                }
                              });
                            }),

static Future<List<Task>> getTasks(
      String userId, String status, List<String> filter) async {
    QuerySnapshot userTasksSnapshot = await tasksRef
        .where('status', isEqualTo: status)
        .where('ownerId', isNotEqualTo: userId)
        .where('typeId', arrayContains: filter)

        // .orderBy('timestamp', descending: true)
        .get();
    List<Task> tasks =
        userTasksSnapshot.docs.map((doc) => Task.fromDoc(doc)).toList();
    return tasks;
  }


问:
Should virtual dispatch happen when a virtual method is called within a virtual method using object?
```<code>struct B
{
  virtual void bar () {}
  virtual void foo () { bar(); }
};

struct D : B
{
  virtual void bar () {}
  virtual void foo () {}
};
</code>```

Now we call foo() using an object of B as,

```<code>B obj;
obj.foo();  // calls B::bar()
</code>```

Question:
Should bar() will be resolved through virtual dispatch or it will be resolved using the static type of the object (i.e. B).

答:
Answer: from the language point of view call to bar() inside B::foo() is resolved through virtual dispatch.

The bottom line is that from the point of view of C++ language, virtual dispatch always happens when you call a virtual method using its non-qualified name. When you use a qualified name of the method, the virtual dispatch does not happen. That means that the only way to suppress virtual dispatch in C++ is to use this syntax

```<code>some_object_ptr->SomeClass::some_method();
some_object.SomeClass::some_method();
</code>```

In this case the dynamic type if the object on the left-hand side is ignored and the specific method is called directly.

In all other cases virtual dispatch does happen, as far as the language is concerned. I.e. the call is resolved in accordance with the dynamic type of the object. In other words, from the formal point of view, every time you call a virtual method through an immediate object, as in

```<code>B obj;
obj.foo();
</code>```

the method is called through the "virtual dispatch" mechanism, regardless of context ("within a virtual method" or not - doesn't matter).

That's how it is in C++ language. Everything else is just optimizations made by compilers. As you probably know, most (if not all) compilers will generate a non-virtual call to a virtual method, when the call is performed through an immediate object. This is, of course, an obvious optimization, since the compiler knows that the static type of the object is the same as its dynamic type. Again, it doesn't depend on the context ("within a virtual method" or not - doesn't matter).

Inside a virtual method, the call can be made without specifying an object on the left hand side (as in your example), which really implies all calls have this-> implicitly present on the left. The very same rules apply in this case as well. If you just call bar(), it stands for this->bar() and the call is dispatched virtually. If you call B::bar(), it stands for this->B::bar() and the call is dispatched non-virtually. Everything else will only depend on the optimization capabilities of the compiler.

What you are trying to say by "because, once you are inside B::foo(), it's sure that this is of type B* and not D*" is totally unclear to me. This statement misses the point. Virtual dispatch depends on the dynamic type of the object. Note: it depends on the type of *this, not on the type of this. It doesn't matter at all what the type of this is. What matters is the dynamic type of *this. When you are inside B::foo, it is still perfectly possible that the dynamic type of *this is D or something else. For which reason, the call to bar() has to be resolved dynamically.

Body:
struct B
{
  virtual void bar () {}
  virtual void foo () { bar(); }
};

struct D : B
{
  virtual void bar () {}
  virtual void foo () {}
};

Now we call foo() using an object of B as,

B obj;
obj.foo();  // calls B::bar()

Question:
Should bar() will be resolved through virtual dispatch or it will be resolved using the static type of the object (i.e. B).

Body:
Answer: from the language point of view call to bar() inside B::foo() is resolved through virtual dispatch.

The bottom line is that from the point of view of C++ language, virtual dispatch always happens when you call a virtual method using its non-qualified name. When you use a qualified name of the method, the virtual dispatch does not happen. That means that the only way to suppress virtual dispatch in C++ is to use this syntax

some_object_ptr->SomeClass::some_method();
some_object.SomeClass::some_method();

In this case the dynamic type if the object on the left-hand side is ignored and the specific method is called directly.

In all other cases virtual dispatch does happen, as far as the language is concerned. I.e. the call is resolved in accordance with the dynamic type of the object. In other words, from the formal point of view, every time you call a virtual method through an immediate object, as in

B obj;
obj.foo();

the method is called through the "virtual dispatch" mechanism, regardless of context ("within a virtual method" or not - doesn't matter).

That's how it is in C++ language. Everything else is just optimizations made by compilers. As you probably know, most (if not all) compilers will generate a non-virtual call to a virtual method, when the call is performed through an immediate object. This is, of course, an obvious optimization, since the compiler knows that the static type of the object is the same as its dynamic type. Again, it doesn't depend on the context ("within a virtual method" or not - doesn't matter).

Inside a virtual method, the call can be made without specifying an object on the left hand side (as in your example), which really implies all calls have this-> implicitly present on the left. The very same rules apply in this case as well. If you just call bar(), it stands for this->bar() and the call is dispatched virtually. If you call B::bar(), it stands for this->B::bar() and the call is dispatched non-virtually. Everything else will only depend on the optimization capabilities of the compiler.

What you are trying to say by "because, once you are inside B::foo(), it's sure that this is of type B* and not D*" is totally unclear to me. This statement misses the point. Virtual dispatch depends on the dynamic type of the object. Note: it depends on the type of *this, not on the type of this. It doesn't matter at all what the type of this is. What matters is the dynamic type of *this. When you are inside B::foo, it is still perfectly possible that the dynamic type of *this is D or something else. For which reason, the call to bar() has to be resolved dynamically.


问:
How to find DIV inside div-structure by InnerText or Class
I have a div with multiple children which have a large div structure in them. Now I want to find an element with an specific innerText.
How can I do this?
I have heard something from document.evaluate but is this the right thing?

First Label

Second Label

Third Label

```

I have main and want to find the Label with the text Second Label without multiple Loops.

答:
You can do this via a document.querySelectorAll ("label")

```<code>const len = document.querySelectorAll("label");

for (let i = 1; i < len.length+1; i++) {
  var el = document.querySelector("label[classname=text"+i+"]");
  console.log(el.innerText);
  if(el.innerText == "First Label") {
    console.log("Found!");
  }
}
</code>```

Body:
I have a div with multiple children which have a large div structure in them. Now I want to find an element with an specific innerText.
How can I do this?
I have heard something from document.evaluate but is this the right thing?

First Label

Second Label

Third Label

I have main and want to find the Label with the text Second Label without multiple Loops.

Body:
You can do this via a document.querySelectorAll ("label")

const len = document.querySelectorAll("label");

for (let i = 1; i < len.length+1; i++) {
  var el = document.querySelector("label[classname=text"+i+"]");
  console.log(el.innerText);
  if(el.innerText == "First Label") {
    console.log("Found!");
  }
}


问:
Undo Abort Merge Conflict Gitlab
I accidentally abort my conflict changes, so all files disappeared. Can i undo that process? or recovery my file? I really need my file because all my works has gone. Hufft...</p>

答:
It depends on how the abort was done:

locally, in a local cloned repository of the GitLab remote repository (git merge --abort)

remotely, through the GitLab web GUI (see "GitLab Merge conflicts")

In both cases, what could be lost is the resolution work done so far.

But no new file should have been erased. Local modifications are, however, removed: see "Local changes lost after aborting merge in GIT".

Only git merge --quit would remove everything.

Worst case, you redo the merge, and resolve the conflicts again.

Title: Undo Abort Merge Conflict Gitlab


问:
Its giving me a module not found error but its installed but if i run a different code it does not give a error
When I run this code it gives a module not found error for line 2 but it is installed
on my computer. I know that because when i type pip list in the command prompt it is in the list

```<code>from tkinter import *
from tkvideoplayer import TkinterVideo
from tkinter.filedialog import askopenfile
window = Tk()
window.title("My Video Player")
window.geometry("500x500")
window.config(bg="Turquoise")
heading = Label(window, text="My Video Player", bg="Orange Red", fg="white", font="4 none bold")
heading.config(anchor=CENTER)

def openFile():
    file = askopenfile(mode="r", filetypes=[('Video Files', '*.mp4', '*.mov')])
    if file is not None:
        global filename
        filename = file.name
        global videoPlayer
        videoPlayer = TkinterVideo(master=window, scaled=True, pre_load=False)
        videoPlayer.load(r"{}".format(filename))
        videoPlayer.pack(expand=True, fill="both")
        videoPlayer.play()

def playFile():
    videoPlayer.play()

def stopFile():
    videoPlayer.stop()

def pauseFile():
    videoPlayer.pause()

openbtn = Button(window, text="Open", command=lambda: openFile())
stopbtn = Button(window, text="Stop", command=lambda: stopFile())
playbtn = Button(window, text="Play", command=lambda: playFile())
pausebtn = Button(window, text="Pause", command=lambda: pauseFile())

openbtn.pack(side=TOP, pady=2)
stopbtn.pack(side=TOP, pady=4)
playbtn.pack(side=TOP, pady=3)
pausebtn.pack(side=TOP, pady=5)
heading.pack()

window.mainloop()
</code>```

However on the same computer if I run this code

```<code>import datetime
import tkinter as tk
from tkinter import filedialog
from tkVideoPlayer import TkinterVideo

def update_duration(event):
    """ updates the duration after finding the duration """
    end_time["text"] = str(datetime.timedelta(seconds=vid_player.duration()))
    progress_slider["to"] = vid_player.duration()

def update_scale(event):
    """ updates the scale value """
    progress_slider.set(vid_player.current_duration())

def load_video():
    """ loads the video """
    file_path = filedialog.askopenfilename()

    if file_path:
        vid_player.load(file_path)

        progress_slider.config(to=0, from_=0)
        progress_slider.set(0)
        play_pause_btn["text"] = "Play"

def seek(value):
    """ used to seek a specific timeframe """
    vid_player.seek(int(value))

def skip(value: int):
    """ skip seconds """
    vid_player.skip_sec(value)
    progress_slider.set(progress_slider.get() + value)

def play_pause():
    """ pauses and plays """
    if vid_player.is_paused():
        vid_player.play()
        play_pause_btn["text"] = "Pause"

    else:
        vid_player.pause()
        play_pause_btn["text"] = "Play"

def video_ended(event):
    """ handle video ended """
    progress_slider.set(progress_slider["to"])
    play_pause_btn["text"] = "Play"

root = tk.Tk()
root.title("Video Player")
root.geometry("700x600")

load_btn = tk.Button(root, text="Load", command=load_video)
load_btn.pack()

vid_player = TkinterVideo(scaled=True, pre_load=False, master=root)
vid_player.pack(expand=True, fill="both")

play_pause_btn = tk.Button(root, text="Play", command=play_pause)
play_pause_btn.pack()

skip_plus_5sec = tk.Button(root, text="Skip -5 sec", command=lambda: skip(-5))
skip_plus_5sec.pack(side="left")

start_time = tk.Label(root, text=str(datetime.timedelta(seconds=0)))
start_time.pack(side="left")

progress_slider = tk.Scale(root, from_=0, to=0, orient="horizontal", command=seek)
progress_slider.pack(side="left", fill="x", expand=True)

end_time = tk.Label(root, text=str(datetime.timedelta(seconds=0)))
end_time.pack(side="left")

vid_player.bind("<<Duration>>", update_duration)
vid_player.bind("<<SecondChanged>>", update_scale)
vid_player.bind("<<Ended>>", video_ended)

skip_plus_5sec = tk.Button(root, text="Skip +5 sec", command=lambda: skip(5))
skip_plus_5sec.pack(side="left")

root.mainloop()
</code>```

It works and does not give an error.
But I am using the same code line.
Can anyone explain that?

答:
But I am using the same code line.

No, you're not. The working version spells tkVideoPlayer in camelCase (and indeed, according to the package's docs that's the correct casing):

```<code>from tkVideoPlayer import TkinterVideo
</code>```

and the version that doesn't work spells it in all lowercase.

```<code>from tkvideoplayer import TkinterVideo
</code>```

You'll have to be careful about character case; in general programming languages and environments aren't forgiving about that.

Body:
When I run this code it gives a module not found error for line 2 but it is installed
on my computer. I know that because when i type pip list in the command prompt it is in the list

from tkinter import *
from tkvideoplayer import TkinterVideo
from tkinter.filedialog import askopenfile
window = Tk()
window.title("My Video Player")
window.geometry("500x500")
window.config(bg="Turquoise")
heading = Label(window, text="My Video Player", bg="Orange Red", fg="white", font="4 none bold")
heading.config(anchor=CENTER)

def openFile():
    file = askopenfile(mode="r", filetypes=[('Video Files', '*.mp4', '*.mov')])
    if file is not None:
        global filename
        filename = file.name
        global videoPlayer
        videoPlayer = TkinterVideo(master=window, scaled=True, pre_load=False)
        videoPlayer.load(r"{}".format(filename))
        videoPlayer.pack(expand=True, fill="both")
        videoPlayer.play()

def playFile():
    videoPlayer.play()

def stopFile():
    videoPlayer.stop()

def pauseFile():
    videoPlayer.pause()

openbtn = Button(window, text="Open", command=lambda: openFile())
stopbtn = Button(window, text="Stop", command=lambda: stopFile())
playbtn = Button(window, text="Play", command=lambda: playFile())
pausebtn = Button(window, text="Pause", command=lambda: pauseFile())

openbtn.pack(side=TOP, pady=2)
stopbtn.pack(side=TOP, pady=4)
playbtn.pack(side=TOP, pady=3)
pausebtn.pack(side=TOP, pady=5)
heading.pack()

window.mainloop()

However on the same computer if I run this code

import datetime
import tkinter as tk
from tkinter import filedialog
from tkVideoPlayer import TkinterVideo

def update_duration(event):
    """ updates the duration after finding the duration """
    end_time["text"] = str(datetime.timedelta(seconds=vid_player.duration()))
    progress_slider["to"] = vid_player.duration()

def update_scale(event):
    """ updates the scale value """
    progress_slider.set(vid_player.current_duration())

def load_video():
    """ loads the video """
    file_path = filedialog.askopenfilename()

    if file_path:
        vid_player.load(file_path)

        progress_slider.config(to=0, from_=0)
        progress_slider.set(0)
        play_pause_btn["text"] = "Play"

def seek(value):
    """ used to seek a specific timeframe """
    vid_player.seek(int(value))

def skip(value: int):
    """ skip seconds """
    vid_player.skip_sec(value)
    progress_slider.set(progress_slider.get() + value)

def play_pause():
    """ pauses and plays """
    if vid_player.is_paused():
        vid_player.play()
        play_pause_btn["text"] = "Pause"

    else:
        vid_player.pause()
        play_pause_btn["text"] = "Play"

def video_ended(event):
    """ handle video ended """
    progress_slider.set(progress_slider["to"])
    play_pause_btn["text"] = "Play"

root = tk.Tk()
root.title("Video Player")
root.geometry("700x600")

load_btn = tk.Button(root, text="Load", command=load_video)
load_btn.pack()

vid_player = TkinterVideo(scaled=True, pre_load=False, master=root)
vid_player.pack(expand=True, fill="both")

play_pause_btn = tk.Button(root, text="Play", command=play_pause)
play_pause_btn.pack()

skip_plus_5sec = tk.Button(root, text="Skip -5 sec", command=lambda: skip(-5))
skip_plus_5sec.pack(side="left")

start_time = tk.Label(root, text=str(datetime.timedelta(seconds=0)))
start_time.pack(side="left")

progress_slider = tk.Scale(root, from_=0, to=0, orient="horizontal", command=seek)
progress_slider.pack(side="left", fill="x", expand=True)

end_time = tk.Label(root, text=str(datetime.timedelta(seconds=0)))
end_time.pack(side="left")

vid_player.bind("<<Duration>>", update_duration)
vid_player.bind("<<SecondChanged>>", update_scale)
vid_player.bind("<<Ended>>", video_ended)

skip_plus_5sec = tk.Button(root, text="Skip +5 sec", command=lambda: skip(5))
skip_plus_5sec.pack(side="left")

root.mainloop()

It works and does not give an error.
But I am using the same code line.
Can anyone explain that?

Body:
But I am using the same code line.

No, you're not. The working version spells tkVideoPlayer in camelCase (and indeed, according to the package's docs that's the correct casing):

from tkVideoPlayer import TkinterVideo

and the version that doesn't work spells it in all lowercase.

from tkvideoplayer import TkinterVideo

You'll have to be careful about character case; in general programming languages and environments aren't forgiving about that.


问:
Looking for a way to know when to run OPTIMIZE on Delta Lake tables
have Databricks running on AWS, I'm looking for a way to know when is a good time to run optimize on partitioned tables. Taking into account that it's an expensive process, especially on big tables, how could I know if it's a good time to run it and on which tables? Maybe I can create a script that executes once a week and run optimize command only when is needed.

Also, it's a recommended practice to run OPTIMIZE for each partition on the same job that I create the partition?

So I create 2022-02-12 partition then run optimize where partition=2022-02-12, I'm not sure about this because of the "eventual consistency" of S3, maybe the files are not ready as soon as they are loaded and something can become corrupted.

Thanks!

答:
If you have data partitioned by day, then you can run the optimization script daily, maybe one hour after day closes (eventual consistency of S3 doesn't play a role here).  It could be as simple as (consider addign ZORDER BY on the most often queried columns):

OPTIIMZE
 WHERE partition = (current_date() - interval 1 days)
```

If your workload is append-only, then you don't need to worry about conflicts.

Besides OPTIMIZE, you also need to have a job that will do VACUUM, because OPTIMIZE will leave old files in-place, and they will occupy space.

Body:
If you have data partitioned by day, then you can run the optimization script daily, maybe one hour after day closes (eventual consistency of S3 doesn't play a role here).  It could be as simple as (consider addign ZORDER BY on the most often queried columns):

OPTIIMZE <table> WHERE partition = (current_date() - interval 1 days)

If your workload is append-only, then you don't need to worry about conflicts.

Besides OPTIMIZE, you also need to have a job that will do VACUUM, because OPTIMIZE will leave old files in-place, and they will occupy space.


问:
how to merge the response of webClient call after calling 5 times and save the complete response in DB
i have scenario like:
i have to check the table if entry is available in DB then if available i need to call the same external api n times using webclient, collect all the response and save them in DB. if entry is not available in DB call the old flow.

here is my implementation. need suggestions to improve it. without for-each

```<code>public Mono<List<ResponseObject>> getdata(String id, Req obj) {
    return isEntryInDB(id) //checking the entry in DB
        .flatMap(
            x -> {
              final List<Mono<ResponseObject>> responseList = new ArrayList<>();
              IntStream.range(0, obj.getQuantity()) // quantity decides how many times api call t happen
                  .forEach(
                      i -> {
                        Mono<ResponseObject> responseMono =
                            webClientCall(
                                    id,
                                    req.getType())
                                .map(
                                  res ->
                                        MapperForMappingDataToDesriedFormat(res));
                        responseList.add(responseMono);
                      });
              return saveToDb(responseList);
            })
        .switchIfEmpty(oldFlow(id, req));  //if DB entry is not there take this existing flow.
</code>```

need some suggestions to improve it without using foreach.

答:
I would avoid using IntStream and rather use native operator to reactor called Flux in this case.

You can replace, InsStream.range with Flux.range. Something like this:

return isEntryPresent("123")
                .flatMapMany(s -> Flux.range(0, obj.getQuantity())
                        .flatMap(this::callApi))
                .collectList()
                .flatMap(this::saveToDb)
                .switchIfEmpty(Mono.defer(() ->oldFlow(id, req)));

    private Mono<Object> saveToDb(List<String> stringList){
        return Mono.just("done");
    }
    private Mono<String> callApi(int id) {
        return Mono.just("iterating" + id);
    }

    private Mono<String> isEntryPresent(String id) {
        return Mono.just("string");
    }
```

Body:
i have scenario like:
i have to check the table if entry is available in DB then if available i need to call the same external api n times using webclient, collect all the response and save them in DB. if entry is not available in DB call the old flow.

here is my implementation. need suggestions to improve it. without for-each

public Mono<List<ResponseObject>> getdata(String id, Req obj) {
    return isEntryInDB(id) //checking the entry in DB
        .flatMap(
            x -> {
              final List<Mono<ResponseObject>> responseList = new ArrayList<>();
              IntStream.range(0, obj.getQuantity()) // quantity decides how many times api call t happen
                  .forEach(
                      i -> {
                        Mono<ResponseObject> responseMono =
                            webClientCall(
                                    id,
                                    req.getType())
                                .map(
                                  res ->
                                        MapperForMappingDataToDesriedFormat(res));
                        responseList.add(responseMono);
                      });
              return saveToDb(responseList);
            })
        .switchIfEmpty(oldFlow(id, req));  //if DB entry is not there take this existing flow.

need some suggestions to improve it without using foreach.


问:
elasticSearch count multiple
I have two profiles, "A" and "B" both have events in the elastic

this is the elastic data for ex:

```<code> {hits: [
   {tag:"A"},
   {tag:"B"},
   {tag:B}
 ]}
</code>```

I want to count how much events tag "a" have and and how much "B" in one request

Ive tried this but it counts them total as 3 and I want A:1 and B:2

```<code>GET forensics/_count
{
"query": {
 "terms": {
   "waas_tag": ["A","B"]
 }
}
}

</code>```

答:
You can use filters aggregation to get the count for each tag in a single query without using _msearch endpoint. This query should work:</p>
```<code>{
  "size": 0,
  "aggs": {
    "counts": {
      "filters": {
        "filters": {
          "CountA": {
            "term": {
              "waas_tag": "A"
            }
          },
          "CountB": {
            "term": {
              "waas_tag": "B"
            }
          }
        }
      }
    }
  }
}
</code>```

Title: elasticSearch count multiple

Body:
I have two profiles, "A" and "B" both have events in the elastic

this is the elastic data for ex:

 {hits: [
   {tag:"A"},
   {tag:"B"},
   {tag:B}
 ]}

I want to count how much events tag "a" have and and how much "B" in one request

Ive tried this but it counts them total as 3 and I want A:1 and B:2

GET forensics/_count
{
"query": {
 "terms": {
   "waas_tag": ["A","B"]
 }
}
}

Body:
You can use filters aggregation to get the count for each tag in a single query without using _msearch endpoint. This query should work:

{
  "size": 0,
  "aggs": {
    "counts": {
      "filters": {
        "filters": {
          "CountA": {
            "term": {
              "waas_tag": "A"
            }
          },
          "CountB": {
            "term": {
              "waas_tag": "B"
            }
          }
        }
      }
    }
  }
}


答:
For data:

```<code>services = [{"Mobile": [{"name": "CompanyA", 'id': 0, 'address': "XYZ"},
                        {"name": 'CompanyB', 'id': 1, 'address': "QWE"},
                        {"name": 'CompanyC', 'id': 2, 'address': "TYU"}]
            },
            {"Computer": [{"name": "CompanyD", 'id': 3, 'address': "PPP"},
                          {"name": 'CompanyD', 'id': 4, 'address': "UYU"},
                          {"name": 'CompanyE', 'id': 5, 'address': "NMB"}]
            }]
</code>```

A combination of list and dictionary comprehensions will achieve the result you're looking for.

```<code>[{k: [{'name': v2['name']} for v2 in v] for k, v in d.items()} for d in services]
</code>```

Result:

```<code>[{'Mobile':   [{'name': 'CompanyA'}, {'name': 'CompanyB'}, {'name': 'CompanyC'}]},
 {'Computer': [{'name': 'CompanyD'}, {'name': 'CompanyD'}, {'name': 'CompanyE'}]}]
</code>```

Title: How to iterate through a nested list of dictionaries in Python

Body:
I have a bit complicated json response from a server, below is my  json:

    services = [{"Mobile": [{"name": "CompanyA", id: 0, address: "XYZ"},
    {"name": CompanyB, id: 1, address: "QWE"},
    {"name": CompanyC, id: 2, address: "TYU"}]
    },
    {"Computer": [{"name": "CompanyD", id: 3, address: "PPP"},
    {"name": CompanyD, id: 4, address: "UYU"},
    {"name": CompanyE, id: 5, address: "NMB"}]
    }]

I need to construct new dictionary which only holds bellow data:

services = [{"Mobile": [{"name": "CompanyA"},{"name": "CompanyB"},
          {"name": "CompanyC"}]},
          {"Computer": [{"name": "CompanyD"},{"name": "CompanyD"},
          {"name": "CompanyE"}]
    }]

in other words, delete id and address fields.

Body:
For data:

services = [{"Mobile": [{"name": "CompanyA", 'id': 0, 'address': "XYZ"},
                        {"name": 'CompanyB', 'id': 1, 'address': "QWE"},
                        {"name": 'CompanyC', 'id': 2, 'address': "TYU"}]
            },
            {"Computer": [{"name": "CompanyD", 'id': 3, 'address': "PPP"},
                          {"name": 'CompanyD', 'id': 4, 'address': "UYU"},
                          {"name": 'CompanyE', 'id': 5, 'address': "NMB"}]
            }]

A combination of list and dictionary comprehensions will achieve the result you're looking for.

[{k: [{'name': v2['name']} for v2 in v] for k, v in d.items()} for d in services]

Result:

[{'Mobile':   [{'name': 'CompanyA'}, {'name': 'CompanyB'}, {'name': 'CompanyC'}]},
 {'Computer': [{'name': 'CompanyD'}, {'name': 'CompanyD'}, {'name': 'CompanyE'}]}]


问:
How do I install Rails?
I am completely new to Ruby and need to use Ruby on Rails 2.3.5 on Windows. I have downloaded the zip from GitHub here: https://github.com/rails/rails/tree/v2.3.5

There was no gem file so I tried rake in the extracted directory. It took ages and seemed to have a lot of errors, but it did eventually complete.

There was no gem file created (is this even what rake is supposed to do?). And rails is not installed (typing rails at the command prompt doesnt work).

Any ideas what I am doing wrong?

EDIT: I cannot use gem install rails -v "2.3.5" as I am behind a firewall. I think I need to install it from local files.

答:
You first need to download and install Ruby 1.8.7.

You can then install Rails via gem install rails -v=2.3.5

Title: How do I install Rails?


问:
Cloud2Edge package: error connectivity:connection.id.enforcement.failed
I'm testing a deployment of the Eclipse IoT Cloud2Edge package and have followed the instructions here https://www.eclipse.org/packages/packages/cloud2edge/tour/ to test. After creating the new tenant and device, and configuring the connection between Hono and Ditto, I can send telemetry to the new device via the Hono http adapter as shown here:

```<code>curl -i -u my-auth-id-1@my-tenant:my-password -H 'application/json' --data-binary '{
  "topic": "my-tenant/org.acme:my-device-1/things/twin/commands/modify",
  "headers": {},
  "path": "/features/temperature/properties/value",
  "value": 53
}' http://${HTTP_ADAPTER_IP}:${HTTP_ADAPTER_PORT_HTTP}/telemetry
HTTP/1.1 202 Accepted
vary: origin
content-length: 0
</code>```

and expected to see this property value updated in Ditto. The updated device property value does not update in Ditto, and when I check the Ditto logs I see the following entries:

```<code>2022-02-13 20:11:35,265 INFO  [] o.e.d.c.s.m.a.AmqpConsumerActor akka://ditto-cluster/system/sharding/connection/3/hono-connection-for-my-tenant/pa/$a/c1/amqpConsumerActor-0-telemetry%2Fmy-tenant-010 - Received message from AMQP 1.0 with externalMessageHeaders: {orig_adapter=hono-http, qos=0, device_id=org.acme:my-device-1, creation-time=1644783095260, message-id=ID:AMQP_NO_PREFIX:GenericSenderLink-12, content-type=application/x-www-form-urlencoded, to=telemetry/my-tenant, orig_address=/telemetry}
2022-02-13 20:11:35,271 INFO  [81c41f10-4d59-435b-8ae1-bf5194dcf6bf] o.e.d.c.s.m.InboundDispatchingSink  - onMapped mappedHeaders ImmutableDittoHeaders [{ditto-entity-id=thing:my-tenant:org.acme:my-device-1, ditto-inbound-payload-mapper=default, content-type=application/x-www-form-urlencoded, hono-device-id=org.acme:my-device-1, ditto-reply-target=0, ditto-expected-response-types=["response","error"], ditto-origin=hono-connection-for-my-tenant, ditto-auth-context={"type":"pre-authenticated-connection","subjects":["pre-authenticated:hono-connection"]}, correlation-id=81c41f10-4d59-435b-8ae1-bf5194dcf6bf}]
2022-02-13 20:11:35,278 INFO  [b3b11410-6df8-4bfc-a940-fafa87d65be2] o.e.d.c.s.m.InboundDispatchingSink  - Got exception <connectivity:connection.id.enforcement.failed> when processing external message with mapper <default>: <The configured filters could not be matched against the given target with ID 'org.acme:my-device-1'.>
2022-02-13 20:11:35,278 INFO  [b3b11410-6df8-4bfc-a940-fafa87d65be2] o.e.d.c.s.m.InboundDispatchingSink  - Resolved mapped headers of ImmutableDittoHeaders [{ditto-inbound-payload-mapper=default, ditto-entity-id=thing:my-tenant:org.acme:my-device-1, response-required=false, content-type=application/x-www-form-urlencoded, hono-device-id=org.acme:my-device-1, ditto-reply-target=0, ditto-expected-response-types=["response","error"], ditto-origin=hono-connection-for-my-tenant, ditto-auth-context={"type":"pre-authenticated-connection","subjects":["pre-authenticated:hono-connection"]}, correlation-id=b3b11410-6df8-4bfc-a940-fafa87d65be2}] : with HeaderMapping Optional[ImmutableHeaderMapping [mapping={hono-device-id={{ header:device_id }}, content-type={{ header:content-type }}}]] : and external headers {orig_adapter=hono-http, qos=0, device_id=org.acme:my-device-1, creation-time=1644783095260, message-id=ID:AMQP_NO_PREFIX:GenericSenderLink-12, content-type=application/x-www-form-urlencoded, to=telemetry/my-tenant, orig_address=/telemetry}
2022-02-13 20:11:35,283 INFO  [] o.e.d.c.s.m.a.AmqpConsumerActor akka://ditto-cluster/system/sharding/connection/3/hono-connection-for-my-tenant/pa/$a/c1/amqpConsumerActor-0-telemetry%2Fmy-tenant-010 - Acking <ID:AMQP_NO_PREFIX:GenericSenderLink-12> with original external message headers=<{orig_adapter=hono-http, qos=0, device_id=org.acme:my-device-1, creation-time=1644783095260, message-id=ID:AMQP_NO_PREFIX:GenericSenderLink-12, content-type=application/x-www-form-urlencoded, to=telemetry/my-tenant, orig_address=/telemetry}>, isSuccess=<true>, ackType=<1 accepted>
</code>```

I think the problem is the "connectivity:connection.id.enforcement.failed" error but I don't know how to troubleshoot. Any advice appreciated.

答:
What you configured is the Connection source enforcement which makes sure that a Hono device (identified via the AMQP header device_id) may only updates the twin with the same "thing id" in Ditto.

That enforcement fails as your thingId you set in the Ditto Protocol JSON is my-tenant:org.acme:my-device-1 - the topic's first segment is the namespace, the second segment the name - combined those 2 segments become the "thing ID", see also: Protocol topic specification.

So you probably want to send the following message instead:

{
  "topic": "org.acme/my-device-1/things/twin/commands/modify",
...
}
```

Body:
I'm testing a deployment of the Eclipse IoT Cloud2Edge package and have followed the instructions here https://www.eclipse.org/packages/packages/cloud2edge/tour/ to test. After creating the new tenant and device, and configuring the connection between Hono and Ditto, I can send telemetry to the new device via the Hono http adapter as shown here:

curl -i -u my-auth-id-1@my-tenant:my-password -H 'application/json' --data-binary '{
  "topic": "my-tenant/org.acme:my-device-1/things/twin/commands/modify",
  "headers": {},
  "path": "/features/temperature/properties/value",
  "value": 53
}' http://${HTTP_ADAPTER_IP}:${HTTP_ADAPTER_PORT_HTTP}/telemetry
HTTP/1.1 202 Accepted
vary: origin
content-length: 0

and expected to see this property value updated in Ditto. The updated device property value does not update in Ditto, and when I check the Ditto logs I see the following entries:

2022-02-13 20:11:35,265 INFO  [] o.e.d.c.s.m.a.AmqpConsumerActor akka://ditto-cluster/system/sharding/connection/3/hono-connection-for-my-tenant/pa/$a/c1/amqpConsumerActor-0-telemetry%2Fmy-tenant-010 - Received message from AMQP 1.0 with externalMessageHeaders: {orig_adapter=hono-http, qos=0, device_id=org.acme:my-device-1, creation-time=1644783095260, message-id=ID:AMQP_NO_PREFIX:GenericSenderLink-12, content-type=application/x-www-form-urlencoded, to=telemetry/my-tenant, orig_address=/telemetry}
2022-02-13 20:11:35,271 INFO  [81c41f10-4d59-435b-8ae1-bf5194dcf6bf] o.e.d.c.s.m.InboundDispatchingSink  - onMapped mappedHeaders ImmutableDittoHeaders [{ditto-entity-id=thing:my-tenant:org.acme:my-device-1, ditto-inbound-payload-mapper=default, content-type=application/x-www-form-urlencoded, hono-device-id=org.acme:my-device-1, ditto-reply-target=0, ditto-expected-response-types=["response","error"], ditto-origin=hono-connection-for-my-tenant, ditto-auth-context={"type":"pre-authenticated-connection","subjects":["pre-authenticated:hono-connection"]}, correlation-id=81c41f10-4d59-435b-8ae1-bf5194dcf6bf}]
2022-02-13 20:11:35,278 INFO  [b3b11410-6df8-4bfc-a940-fafa87d65be2] o.e.d.c.s.m.InboundDispatchingSink  - Got exception <connectivity:connection.id.enforcement.failed> when processing external message with mapper <default>: <The configured filters could not be matched against the given target with ID 'org.acme:my-device-1'.>
2022-02-13 20:11:35,278 INFO  [b3b11410-6df8-4bfc-a940-fafa87d65be2] o.e.d.c.s.m.InboundDispatchingSink  - Resolved mapped headers of ImmutableDittoHeaders [{ditto-inbound-payload-mapper=default, ditto-entity-id=thing:my-tenant:org.acme:my-device-1, response-required=false, content-type=application/x-www-form-urlencoded, hono-device-id=org.acme:my-device-1, ditto-reply-target=0, ditto-expected-response-types=["response","error"], ditto-origin=hono-connection-for-my-tenant, ditto-auth-context={"type":"pre-authenticated-connection","subjects":["pre-authenticated:hono-connection"]}, correlation-id=b3b11410-6df8-4bfc-a940-fafa87d65be2}] : with HeaderMapping Optional[ImmutableHeaderMapping [mapping={hono-device-id={{ header:device_id }}, content-type={{ header:content-type }}}]] : and external headers {orig_adapter=hono-http, qos=0, device_id=org.acme:my-device-1, creation-time=1644783095260, message-id=ID:AMQP_NO_PREFIX:GenericSenderLink-12, content-type=application/x-www-form-urlencoded, to=telemetry/my-tenant, orig_address=/telemetry}
2022-02-13 20:11:35,283 INFO  [] o.e.d.c.s.m.a.AmqpConsumerActor akka://ditto-cluster/system/sharding/connection/3/hono-connection-for-my-tenant/pa/$a/c1/amqpConsumerActor-0-telemetry%2Fmy-tenant-010 - Acking <ID:AMQP_NO_PREFIX:GenericSenderLink-12> with original external message headers=<{orig_adapter=hono-http, qos=0, device_id=org.acme:my-device-1, creation-time=1644783095260, message-id=ID:AMQP_NO_PREFIX:GenericSenderLink-12, content-type=application/x-www-form-urlencoded, to=telemetry/my-tenant, orig_address=/telemetry}>, isSuccess=<true>, ackType=<1 accepted>

I think the problem is the "connectivity:connection.id.enforcement.failed" error but I don't know how to troubleshoot. Any advice appreciated.


问:
Is there any better way to carry out partial match of a vector of strings in c++?
I have two vectors of strings and want to carry out a partial/full match between each string i.e

```<code>std::vector<std::string> A = { "AA", "ABC" }
std::vector<std::string> B = { "AABB", "AABC", "ABC", "BC"}
</code>```

I want to perform a partial/full match for all strings in A with all strings in B.
Simplest approach would be just run loops over both vectors

```<code>for(const string& s1 : A)
{
  for(const string& s2 : B)
  {
    if(s2.find(s1) != std::string::npos)
      std::cout << "partial matched" << std::endl;
  }
}
</code>```

Results for the above example would be:

```<code>"AA"  --> "AABB", "AABC"
"ABC" --> "AABC", "ABC"
</code>```

Is there any better way to do this?

答:
std::set_intersection might be what you want since you do not elaborate on the context.

Assumes the request is:

The match means "full string comparison".

Need to know the partial matched result.

The time complexity of std::intersection is at most 2*(a.size() + b.size()) while the brute force method is O(n^2).
Reference here

Note: you might want to know the construction and insertion time of std::set as well.

Note 2: you may also use sorted std::vector which is cache friendly for std::set_intersection

#include <set>
#include <string>
#include <iostream>
#include <algorithm>

int main()
{
    std::set<std::string> a{"bar","foo"};
    std::set<std::string> b{"oof","foo","par"};
    std::vector<std::string> intersect;
    std::set_intersection(b.begin(),b.end(),
                        a.begin(),a.end(),
                        std::back_inserter(intersect));

    std::cout << intersect.size() << std::endl;
    for(std::string& matched: intersect){
        std::cout << matched << std::endl;
    }
    return 0;

}
```

Live Demo

Body:
I have two vectors of strings and want to carry out a partial/full match between each string i.e

std::vector<std::string> A = { "AA", "ABC" }
std::vector<std::string> B = { "AABB", "AABC", "ABC", "BC"}

I want to perform a partial/full match for all strings in A with all strings in B.
Simplest approach would be just run loops over both vectors

for(const string& s1 : A)
{
  for(const string& s2 : B)
  {
    if(s2.find(s1) != std::string::npos)
      std::cout << "partial matched" << std::endl;
  }
}

Results for the above example would be:

"AA"  --> "AABB", "AABC"
"ABC" --> "AABC", "ABC"

Is there any better way to do this?

Body:
std::set_intersection might be what you want since you do not elaborate on the context.

Assumes the request is:

The match means "full string comparison".

Need to know the partial matched result.

The time complexity of std::intersection is at most 2*(a.size() + b.size()) while the brute force method is O(n^2).
Reference here

Note: you might want to know the construction and insertion time of std::set as well.

Note 2: you may also use sorted std::vector which is cache friendly for std::set_intersection

#include <set>
#include <string>
#include <iostream>
#include <algorithm>

int main()
{
    std::set<std::string> a{"bar","foo"};
    std::set<std::string> b{"oof","foo","par"};
    std::vector<std::string> intersect;
    std::set_intersection(b.begin(),b.end(),
                        a.begin(),a.end(),
                        std::back_inserter(intersect));

    std::cout << intersect.size() << std::endl;
    for(std::string& matched: intersect){
        std::cout << matched << std::endl;
    }
    return 0;

}

Live Demo


问:
What is the time complexity of f(n)=(1-(1-k)^(n+1))? where k is constant
I solved a given recurrence solution to determine f(n) and finally got f(n)=(1-(1-k)^(n+1)). Now, what can the possible time complexity for f(n)=(1-(1-k)^(n+1))?</p>

答:
(Turning the comments into an answer)

It transpires from the Michaylor's clarifying comments, that this Question really is about the complexity of computing the formula

f(n, k) = 1 - (1 - k)(n + 1)

where "n" is a positive integer and "k" is a real number in the range 0 to 1.

First of all, it should be stated that formulae don't have execution times as they are not algorithms.  However, since there are no particular requirements for the precision of the result, we can compute it using the following Java code.

```<code>double f(int n, double k) {
    return 1 - Math.pow(1 - k, n + 1);
}
</code>```

where Math is the standard java.lang.Math class.  The computational complexity of Math.pow in modern Java implementations is O(1) (see Java Math.pow(a,b) time complexity) so the complexity of the algorithm (expressed in Java) is also O(1).

Notes:

It is not a given that the implementation of the equivalent of pow(a, b) will be O(1) in all programming languages' runtime libraries.

If better precision than double provides was required, the algorithm would need to use BigDecimal or equivalent.  It wouldn't necessarily be O(1).

To illustrate the distinction between formulae and algorithms, if we were to replace Math.pow(1 - k, n + 1) with a naive for loop, that version would have a complexity of O(n)!

Body:
(Turning the comments into an answer)

It transpires from the Michaylor's clarifying comments, that this Question really is about the complexity of computing the formula

f(n, k) = 1 - (1 - k)(n + 1)

where "n" is a positive integer and "k" is a real number in the range 0 to 1.

First of all, it should be stated that formulae don't have execution times as they are not algorithms.  However, since there are no particular requirements for the precision of the result, we can compute it using the following Java code.

double f(int n, double k) {
    return 1 - Math.pow(1 - k, n + 1);
}

where Math is the standard java.lang.Math class.  The computational complexity of Math.pow in modern Java implementations is O(1) (see Java Math.pow(a,b) time complexity) so the complexity of the algorithm (expressed in Java) is also O(1).

Notes:

It is not a given that the implementation of the equivalent of pow(a, b) will be O(1) in all programming languages' runtime libraries.

If better precision than double provides was required, the algorithm would need to use BigDecimal or equivalent.  It wouldn't necessarily be O(1).

To illustrate the distinction between formulae and algorithms, if we were to replace Math.pow(1 - k, n + 1) with a naive for loop, that version would have a complexity of O(n)!


问:
Attempting to make my own loader, but cannot implement the data section
I am trying to implement my own binary loader for learning purposes, but cannot figure out the data segment.

```<code>section .data
    helloworld db "hello world", 10

section .text
    global _start

test: ;just for testing
    ret

_start:
    call test

    mov rax, 1
    mov rbx, 1
    mov rcx, helloworld
    mov rdx, 11
    syscall

    mov rax, 60
    mov rdi, 0
    syscall
</code>```

This is my assembly program that I am trying to run. I compiled with nasm -f elf64 test.s -o test.o && ld test.o -o test.bin

My loader looks like this:

```<code>int main(int argc, char** argv) {
  char* bin = argv[1];
  struct ElfLib lib = read_elf(bin); //just reading the elf library into the default structures (Elf64_Ehdr, Elf64_Phdr, etc...)

  unsigned char* exec = mmap(NULL, DEFAULT_MEM_SIZ, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); //allocating the virtual memory
  memset(exec, 0, DEFAULT_MEM_SIZ);

  for (int i = 0; i < lib.elf_header.e_phnum; i++) {
        Elf64_Phdr phdr = lib.program_headers[i];

        fseek(lib.execfile, phdr.p_offset, SEEK_SET);
        switch (phdr.p_type) {
            case PT_LOAD: {
                //load the memory at the file offset into the virtual address of exec
                fread(exec + phdr.p_vaddr, sizeof(unsigned char), phdr.p_memsz, lib.execfile);
                break;
            }
        }

        int flags = PROT_NONE;

        #define HASFLAG(flag) if (phdr.p_flags & flag) flags|=flag

        HASFLAG(PROT_EXEC); //execute flag on
        HASFLAG(PROT_WRITE); //write flag on
        HASFLAG(PROT_READ); //read flag on

        mprotect(exec + phdr.p_vaddr, phdr.p_memsz, flags);
  }

  void (*ex)() = (void*)(exec + lib.elf_header.e_entry);
  ex(); //call the _start function in the virtual memory
}
</code>```

But when I run it, nothing gets printed.

I tried running it under GDB, and the program promptly exits after the exit syscall, with mov rax, 60 and mov rdi, 0, so I know the system call part works. I think that the issue is in the address of helloworld in the hello world program. GDB says that it is still under address 0x402000, which probably is not the same address under the virtual memory. Surprisingly, the test function is at 0x401000 with objdump, but at a completely different one when running with GDB, which does get called. Does anyone have an idea on how to go about implementing this?

I'm not sure how much this will help, but I'm running using x64 Linux under intel.

答:
```<code>nasm -f elf64 test.s -o test.o
ld test.o -o test.bin
</code>```

Unfortunately, I don't have NASM, but if I use GNU assembler instead of NASM, the lines above result in a position-dependent file.

This means that phdr.p_vaddr does not specify a value that is relative to the variable exec, but phdr.p_vaddr specifies an absolute address that must not be changed.

Assuming the symbol helloworld is located at the start of the data segment, the instruction mov rcx, helloworld will simply load the value phdr.p_vaddr into the register rcx - and not the value exec + phdr.p_vaddr.

However, because the address phdr.p_vaddr may already be used, you cannot simply load your code there!

The only possibility that you have if you want to load code from an already running program is so-called "position independent code" that can be loaded at different addresses in memory...

By the way:

64-bit x86 Linux does not take the parameters in rbx, rcx and rdx, but in rdi, rsi and rdx.

Body:
I am trying to implement my own binary loader for learning purposes, but cannot figure out the data segment.

section .data
    helloworld db "hello world", 10

section .text
    global _start

test: ;just for testing
    ret

_start:
    call test

    mov rax, 1
    mov rbx, 1
    mov rcx, helloworld
    mov rdx, 11
    syscall

    mov rax, 60
    mov rdi, 0
    syscall

This is my assembly program that I am trying to run. I compiled with nasm -f elf64 test.s -o test.o && ld test.o -o test.bin

My loader looks like this:

int main(int argc, char** argv) {
  char* bin = argv[1];
  struct ElfLib lib = read_elf(bin); //just reading the elf library into the default structures (Elf64_Ehdr, Elf64_Phdr, etc...)

  unsigned char* exec = mmap(NULL, DEFAULT_MEM_SIZ, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); //allocating the virtual memory
  memset(exec, 0, DEFAULT_MEM_SIZ);

  for (int i = 0; i < lib.elf_header.e_phnum; i++) {
        Elf64_Phdr phdr = lib.program_headers[i];

        fseek(lib.execfile, phdr.p_offset, SEEK_SET);
        switch (phdr.p_type) {
            case PT_LOAD: {
                //load the memory at the file offset into the virtual address of exec
                fread(exec + phdr.p_vaddr, sizeof(unsigned char), phdr.p_memsz, lib.execfile);
                break;
            }
        }

        int flags = PROT_NONE;

        #define HASFLAG(flag) if (phdr.p_flags & flag) flags|=flag

        HASFLAG(PROT_EXEC); //execute flag on
        HASFLAG(PROT_WRITE); //write flag on
        HASFLAG(PROT_READ); //read flag on

        mprotect(exec + phdr.p_vaddr, phdr.p_memsz, flags);
  }

  void (*ex)() = (void*)(exec + lib.elf_header.e_entry);
  ex(); //call the _start function in the virtual memory
}

But when I run it, nothing gets printed.

I tried running it under GDB, and the program promptly exits after the exit syscall, with mov rax, 60 and mov rdi, 0, so I know the system call part works. I think that the issue is in the address of helloworld in the hello world program. GDB says that it is still under address 0x402000, which probably is not the same address under the virtual memory. Surprisingly, the test function is at 0x401000 with objdump, but at a completely different one when running with GDB, which does get called. Does anyone have an idea on how to go about implementing this?

I'm not sure how much this will help, but I'm running using x64 Linux under intel.

Body:
nasm -f elf64 test.s -o test.o
ld test.o -o test.bin

Unfortunately, I don't have NASM, but if I use GNU assembler instead of NASM, the lines above result in a position-dependent file.

This means that phdr.p_vaddr does not specify a value that is relative to the variable exec, but phdr.p_vaddr specifies an absolute address that must not be changed.

Assuming the symbol helloworld is located at the start of the data segment, the instruction mov rcx, helloworld will simply load the value phdr.p_vaddr into the register rcx - and not the value exec + phdr.p_vaddr.

However, because the address phdr.p_vaddr may already be used, you cannot simply load your code there!

The only possibility that you have if you want to load code from an already running program is so-called "position independent code" that can be loaded at different addresses in memory...

By the way:

64-bit x86 Linux does not take the parameters in rbx, rcx and rdx, but in rdi, rsi and rdx.


问:
why 0.3 + 0 == 0.3 and 0.1 + 0 == 0.1 in javascript when 0.3 and 0.1 cannot be represented exactly in binary
First question:
In java script if we do.

```<code>0.2 + 0.1
=>0.30000000000000004
</code>```

Because 0.2,0.1 are rounded off to different number and also the sum is rounded of to a different number  This article explains the behind the scenes.I understand this but
when i do.

```<code>0.3 + 0
=>0.3
</code>```

It shows 0.3 why? 0.3 cannot be exactly represented in binary right  and it should be rounded to different number right?.

so the value of 0.3 + 0 should be the rounded number of 0.3 and the sum if needed?.

Second question:

when we do.

```<code>let x = 0.3
</code>```

As 0.3 cannot be represented exactly in binary it is rounded of to another number but when you access the variable x why does it show 0.3 but not the actual rounded of number.

```<code>x
=>0.3
</code>```

But when you do.

```<code>0.2 + 0.1
=>0.30000000000000004
</code>```

It shows the actual rounded of number but not 0.3.
Kindly explain.

答:
The value you get when you add 0.1 and 0.2 and the value you get from the literal 0.3 are different values (neither of which is exactly 3/10ths, both of which are very near it). (Adding 0 doesn't matter, it doesn't change the value of the number it's being added to.) The one you get from 0.1 + 0.2 is just barely over 3/10ths; the one you get from 0.3 (if I recall correctly) is just under 3/10ths.

When converting a floating point number to text, JavaScript follows the common practice of only including as many digits as are required to differentiate it from the next nearest value the format can represent (details in the spec and in the academic paper the spec references). 0.3 doesn't need any additional digits for that differentiation, but 0.30000000000000004 does (I'm guessing to differentiate it from 0.3, but I don't actually know that).

Body:
First question:
In java script if we do.

0.2 + 0.1
=>0.30000000000000004

Because 0.2,0.1 are rounded off to different number and also the sum is rounded of to a different number  This article explains the behind the scenes.I understand this but
when i do.

0.3 + 0
=>0.3

It shows 0.3 why? 0.3 cannot be exactly represented in binary right  and it should be rounded to different number right?.

so the value of 0.3 + 0 should be the rounded number of 0.3 and the sum if needed?.

Second question:

when we do.

let x = 0.3

As 0.3 cannot be represented exactly in binary it is rounded of to another number but when you access the variable x why does it show 0.3 but not the actual rounded of number.

x
=>0.3

But when you do.

0.2 + 0.1
=>0.30000000000000004

It shows the actual rounded of number but not 0.3.
Kindly explain.